Next / Previous / Contents / Shipman's homepage

6.14. PathInfo.__permFlags(): Format all the permissions

This method formats the permission bits in self.mode using the time-honored format of the Unix “ls” command.

pathinfo.py
# - - -   P a t h I n f o . _ _ p e r m F l a g s   - - -

    def __permFlags ( self ):
        """Format self.mode's permissions as 'rwxrwxrwx'.
        """

Each set of three permission bits is formatted in the same way, so we call method self.__rwx() to format them. This method takes three arguments. The first argument is 0 if there is no read permission, nonzero otherwise. The second and third arguments are the write and execute permission with the same convention.

To get the values for each permission, we use a Boolean “and” (&) operator on self.mode and the mask values from the stat module to discard all but the bit of interest. Mask stat.S_IRUSR has a one bit in the position of the owner read permission of the mode word, and zeroes in the other positions. Mask stat.S_IWUSR is a mask for the owner write permission, and so on.

pathinfo.py
        return ( "%s%s%s" %
                 (self.__rwx ( self.mode & stat.S_IRUSR,
                               self.mode & stat.S_IWUSR,
                               self.mode & stat.S_IXUSR ),
                  self.__rwx ( self.mode & stat.S_IRGRP,
                               self.mode & stat.S_IWGRP,
                               self.mode & stat.S_IXGRP ),
                  self.__rwx ( self.mode & stat.S_IROTH,
                               self.mode & stat.S_IWOTH,
                               self.mode & stat.S_IXOTH ) ) )