Next / Previous / Contents / Shipman's homepage

7.10. BigReport.__visitor(): Visitor function for os.path.walk()

When we call os.path.walk(), we pass it this method as the “visitor function”. This function is called once for each directory in the subtree. As discussed in the Python Library Reference section on the os.path module, the visitor function takes three arguments:

  1. arg: The value passed as the third argument to os.path.walk() is passed on to the visitor function. We are not using this value.

  2. dirName: The name of the directory we are currently visiting.

  3. nameList: A list of the names within this directory. This may include regular files, subdirectories, and soft links (and perhaps other creatures that are not of interest to this script). If the directory is empty, this argument will be an empty list.

This method must find all the regular files in nameList, take snapshots of them with the BigInfo constructor, and add those BigInfo instances to the self.__bigList list.

We can ignore subdirectories here, because os.path.walk() will take care of calling the visitor function for them.
# - - -   B i g R e p o r t . _ _ v i s i t o r   - - -

    def __visitor ( self, basePath, dirName, nameList ):
        """Visitor function for os.path.walk.

          [ (basePath is the absolute path name to a directory
            above dirName) and
            (dirName is the name of a directory) and
            (nameList is a list of the names within that 
            directory) ->
              self.__bigList  :=  self.__bigList with BigInfo
                  objects added representing the accessible
                  ordinary files in nameList ]

The first step is to add an entry for the directory itself. It is unlikely that the directory will be inaccessible, but we use a try:/except: block just in case it is, so the script won't crash.
        #-- 1 --
        # [ self.__bigList  :=  self.__bigList with a BigInfo
        #       object added representing dirName ]
            dirInfo  =  BigInfo ( dirName, basePath )
            self.__bigList.append ( dirInfo )
        except OSError, detail:

Next, we iterate through the files in nameList, attempting to pass each one to BigInfo. If we don't have access to the file, that constructor will raise an OSError exception; in that case, we just discard that name and move on to the next one.

Each file's path name must be reconstructed by prepending it with dirName. We use the special os.path.join() function to concatenate them.

Then, we append each BigInfo object to self.__bigList only if it is a regular file.
        #-- 2 --
        for  fileName in nameList:
            #-- 2 body --
            # [ if fileName names an accessible regular file ->
            #     self.__bigList  :=  self.__bigList with a new
            #         BigInfo object representing fileName
            #   else -> I ]

            #-- 2.1 --
            # [ filePath  :=  dirName + fileName ]
            filePath  =  os.path.join ( dirName, fileName )

            #-- 2.2 --
            # [ if filePath is an accessible path to a regular file ->
            #     self.__bigList  :=  self.__bigList + (a BigInfo
            #         showing the status of filePath)
            #   else -> I ]
                bigInfo  =  BigInfo ( filePath, basePath )
                if  bigInfo.isFile():
                    self.__bigList.append ( bigInfo )
            except OSError, detail:

Note the pass statement above. This causes inodes such as block and character device files to be ignored silently.