Changeset 1684:a0a8365cdb12


Ignore:
Timestamp:
Oct 28, 2017, 11:15:46 PM (14 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Extract method `_is_file_system_entity`

Extract `_is_file_system_entity` as the common code of
`ftputil.path._Path.isdir` and `ftputil.path._Path.isfile`.

For this change, I'm a bit torn between increasing the complexity by
another indirection vs. removing almost all redundancy between the two
methods. For now, favor the redundancy removal.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ftputil/path.py

    r1683 r1684  
    123123    # by the exception handling in `isfile`, `isdir` and `islink`.
    124124
    125     def isdir(self, path):
    126         """
    127         Return true if the `path` exists and corresponds to a
    128         directory (no link).
    129 
    130         A non-existing path does _not_ cause a `PermanentError`.
    131         """
     125    def _is_file_system_entity(self, path, dir_or_file):
     126        """
     127        Return `True` if `path` represents the file system entity
     128        described by `dir_or_file` ("dir" or "file").
     129
     130        Return `False` if `path` isn't a directory or file,
     131        respectively or if `path` leads to an infinite chain of links.
     132        """
     133        assert dir_or_file in ["dir", "file"]
     134        # Consider differences between directories and files.
     135        if dir_or_file == "dir":
     136            should_look_for_dir = True
     137            stat_function = stat.S_ISDIR
     138        else:
     139            should_look_for_dir = False
     140            stat_function = stat.S_ISREG
     141        #
    132142        path = ftputil.tool.as_unicode(path)
    133         # Workaround if we can't go up from the current directory.
    134         # The result from `getcwd` should already be normalized.
     143        #  Workaround if we can't go up from the current directory.
     144        #  The result from `getcwd` should already be normalized.
    135145        if self.normpath(path) == self._host.getcwd():
    136             return True
     146            return should_look_for_dir
    137147        try:
    138148            stat_result = self._host.stat(
     
    141151            return False
    142152        except ftputil.error.RootDirError:
    143             return True
     153            return should_look_for_dir
    144154        else:
    145155            if stat_result is None:
     
    147157                return False
    148158            else:
    149                 return stat.S_ISDIR(stat_result.st_mode)
     159                return stat_function(stat_result.st_mode)
     160
     161    def isdir(self, path):
     162        """
     163        Return true if the `path` exists and corresponds to a
     164        directory (no link).
     165
     166        A non-existing path does _not_ cause a `PermanentError`.
     167        """
     168        return self._is_file_system_entity(path, "dir")
    150169
    151170    def isfile(self, path):
     
    156175        A non-existing path does _not_ cause a `PermanentError`.
    157176        """
    158         path = ftputil.tool.as_unicode(path)
    159         # Workaround if we can't go up from the current directory.
    160         # The result from `getcwd` should already be normalized.
    161         if self.normpath(path) == self._host.getcwd():
    162             return False
    163         try:
    164             stat_result = self._host.stat(
    165                             path, _exception_for_missing_path=False)
    166         except ftputil.error.RecursiveLinksError:
    167             return False
    168         except ftputil.error.RootDirError:
    169             return False
    170         else:
    171             if stat_result is None:
    172                 # Non-existent path
    173                 return False
    174             else:
    175                 return stat.S_ISREG(stat_result.st_mode)
     177        return self._is_file_system_entity(path, "file")
    176178
    177179    def islink(self, path):
Note: See TracChangeset for help on using the changeset viewer.