This method returns a string containing two lines of
output. The format is described in Section 5, “softlinks.py: Find soft links in a directory tree”. The class variables goodPrefix and badPrefix should
be the same length; the former is used for links to
existing files, the latter for broken links.
# - - - L i n k I n f o . _ _ s t r _ _ - - -
goodPrefix = " "
brokenPrefix = " #### "
def __str__ ( self ):
"""Convert a LinkInfo instance to a string.
"""
First we convert the link's absolute path name to a
relative path name by removing self.__basePath from the front of it. This
relies on the precondition that self.__basePath is a directory above self.path.
#-- 1 --
# [ fullPath := self's absolute path without links
# replaced
# targetPath := self's target path ]
fullPath = self.absPath()
targetPath = self.realPath()
#-- 2 --
# [ self.__basePath is a directory above fullPath ->
# relPath := path to fullPath relative to
# self.__basePath ]
relPath = fullPath [ len(self.__basePath) + 1 : ]
Next we use os.path.exists() to find out
whether the link is broken or not, and set prefix to indicate whether it is broken.
Finally we format the two-line report string and return
it.
#-- 2 --
# [ if targetPath exists ->
# prefix := self.goodPrefix
# else ->
# prefix := self.brokenPrefix ]
if os.path.exists ( targetPath ):
prefix = self.goodPrefix
else:
prefix = self.brokenPrefix
#-- 3 --
return ( "%s ->\n%s%s" %
(relPath, prefix, targetPath) )