Changeset 1899:a90d4f585563


Ignore:
Timestamp:
Jan 2, 2020, 9:11:41 PM (3 months ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Split `as_str`

Split `as_str` into a new `as_str` and `as_str_path`. Not all
applications of the previous `as_str` were on paths, so their use was
confusing.

Additionally, even where processing of `PathLike` objects is desired,
the name `as_str` is bad because it doesn't suggest at all that
`PathLike` processing may happen.
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • ftputil/host.py

    r1895 r1899  
    7575        with ftputil.error.ftplib_error_to_ftp_os_error:
    7676            self._cached_current_dir = self.path.normpath(
    77                 ftputil.tool.as_str(self._session.pwd())
     77                ftputil.tool.as_str_path(self._session.pwd())
    7878            )
    7979        # Associated `FTPHost` objects for data transfer.
     
    213213        # Support the same arguments as `open`.
    214214        # pylint: disable=too-many-arguments
    215         path = ftputil.tool.as_str(path)
     215        path = ftputil.tool.as_str_path(path)
    216216        host = self._available_child()
    217217        if host is None:
     
    493493        the callback was called.
    494494        """
    495         target = ftputil.tool.as_str(target)
     495        target = ftputil.tool.as_str_path(target)
    496496        source_file, target_file = self._upload_files(source, target)
    497497        ftputil.file_transfer.copy_file(
     
    513513        the callback was called.
    514514        """
    515         target = ftputil.tool.as_str(target)
     515        target = ftputil.tool.as_str_path(target)
    516516        source_file, target_file = self._upload_files(source, target)
    517517        return ftputil.file_transfer.copy_file(
     
    542542        the callback was called.
    543543        """
    544         source = ftputil.tool.as_str(source)
     544        source = ftputil.tool.as_str_path(source)
    545545        source_file, target_file = self._download_files(source, target)
    546546        ftputil.file_transfer.copy_file(
     
    563563        the callback was called.
    564564        """
    565         source = ftputil.tool.as_str(source)
     565        source = ftputil.tool.as_str_path(source)
    566566        source_file, target_file = self._download_files(source, target)
    567567        return ftputil.file_transfer.copy_file(
     
    634634    def chdir(self, path):
    635635        """Change the directory on the host."""
    636         path = ftputil.tool.as_str(path)
     636        path = ftputil.tool.as_str_path(path)
    637637        with ftputil.error.ftplib_error_to_ftp_os_error:
    638638            self._session.cwd(path)
     
    651651        `os.mkdir`.
    652652        """
    653         path = ftputil.tool.as_str(path)
     653        path = ftputil.tool.as_str_path(path)
    654654
    655655        def command(self, path):
     
    674674        but otherwise ignored.
    675675        """
    676         path = ftputil.tool.as_str(path)
     676        path = ftputil.tool.as_str_path(path)
    677677        path = self.path.abspath(path)
    678678        directories = path.split(self.sep)
     
    715715        is no longer supported.
    716716        """
    717         path = ftputil.tool.as_str(path)
     717        path = ftputil.tool.as_str_path(path)
    718718        path = self.path.abspath(path)
    719719        if self.listdir(path):
     
    736736        (e. g. timeout).
    737737        """
    738         path = ftputil.tool.as_str(path)
     738        path = ftputil.tool.as_str_path(path)
    739739        path = self.path.abspath(path)
    740740        # Though `isfile` includes also links to files, `islink`
     
    784784        in Python 2.4 and adapted to ftputil.
    785785        """
    786         path = ftputil.tool.as_str(path)
     786        path = ftputil.tool.as_str_path(path)
    787787        # The following code is an adapted version of Python 2.4's
    788788        # `shutil.rmtree` function.
     
    828828    def rename(self, source, target):
    829829        """Rename the source on the FTP host to target."""
    830         source = ftputil.tool.as_str(source)
    831         target = ftputil.tool.as_str(target)
     830        source = ftputil.tool.as_str_path(source)
     831        target = ftputil.tool.as_str_path(target)
    832832        # The following code is in spirit similar to the code in the
    833833        # method `_robust_ftp_command`, though we do _not_ do
     
    896896        """
    897897        original_path = path
    898         path = ftputil.tool.as_str(path)
     898        path = ftputil.tool.as_str_path(path)
    899899        items = self._stat._listdir(path)
    900900        return [ftputil.tool.same_string_type_as(original_path, item) for item in items]
     
    912912        _not_ intended for use by ftputil clients.)
    913913        """
    914         path = ftputil.tool.as_str(path)
     914        path = ftputil.tool.as_str_path(path)
    915915        return self._stat._lstat(path, _exception_for_missing_path)
    916916
     
    928928        _not_ intended for use by ftputil clients.)
    929929        """
    930         path = ftputil.tool.as_str(path)
     930        path = ftputil.tool.as_str_path(path)
    931931        return self._stat._stat(path, _exception_for_missing_path)
    932932
     
    937937        function (see https://docs.python.org/library/os.html#os.walk ).
    938938        """
    939         top = ftputil.tool.as_str(top)
     939        top = ftputil.tool.as_str_path(top)
    940940        # The following code is copied from `os.walk` in Python 2.4
    941941        # and adapted to ftputil.
     
    972972        causes a `PermanentError`.
    973973        """
    974         path = ftputil.tool.as_str(path)
     974        path = ftputil.tool.as_str_path(path)
    975975        path = self.path.abspath(path)
    976976
  • ftputil/path.py

    r1883 r1899  
    5151        """Return an absolute path."""
    5252        original_path = path
    53         path = ftputil.tool.as_str(path)
     53        path = ftputil.tool.as_str_path(path)
    5454        if not self.isabs(path):
    5555            path = self.join(self._host.getcwd(), path)
     
    112112            stat_function = stat.S_ISREG
    113113        #
    114         path = ftputil.tool.as_str(path)
     114        path = ftputil.tool.as_str_path(path)
    115115        #  Workaround if we can't go up from the current directory.
    116116        #  The result from `getcwd` should already be normalized.
     
    157157        instead return `False`.
    158158        """
    159         path = ftputil.tool.as_str(path)
     159        path = ftputil.tool.as_str_path(path)
    160160        try:
    161161            lstat_result = self._host.lstat(path, _exception_for_missing_path=False)
     
    187187        to accumulate statistics.  Passing None for arg is common.
    188188        """
    189         top = ftputil.tool.as_str(top)
     189        top = ftputil.tool.as_str_path(top)
    190190        # This code (and the above documentation) is taken from
    191191        # `posixpath.py`, with slight modifications.
  • ftputil/tool.py

    r1898 r1899  
    1010
    1111
    12 __all__ = ["same_string_type_as", "as_str"]
     12__all__ = ["same_string_type_as", "as_str", "as_str_path"]
    1313
    1414
     
    4848
    4949
    50 def as_str(path):
     50def as_str(string):
     51    """
     52    Return the argument `string` converted to a unicode string if it's
     53    a `bytes` object. Otherwise just return the string.
     54
     55    If `string` is neither `str` nor `bytes`, raise a `TypeError`.
     56    """
     57    if isinstance(string, bytes):
     58        return string.decode(LOSSLESS_ENCODING)
     59    elif isinstance(string, str):
     60        return string
     61    else:
     62        raise TypeError("`as_str` argument must be `bytes` or `str`")
     63
     64
     65def as_str_path(path):
    5166    """
    5267    Return the argument `path` converted to a unicode string if it's
     
    6176    """
    6277    path = os.fspath(path)
    63     return same_string_type_as("", path)
     78    return as_str(path)
  • test/test_tool.py

    r1898 r1899  
    5454
    5555as_str = ftputil.tool.as_str
     56as_str_path = ftputil.tool.as_str_path
    5657
    5758
     
    5960    def test_from_bytes(self):
    6061        assert as_str(b"abc") == "abc"
     62        assert as_str_path(b"abc") == "abc"
    6163
    6264    def test_from_str(self):
    6365        assert as_str("abc") == "abc"
     66        assert as_str_path("abc") == "abc"
    6467
    6568    def test_from_bytes_path(self):
    66         assert as_str(Path(b"abc")) == "abc"
     69        assert as_str_path(Path(b"abc")) == "abc"
    6770
    6871    def test_from_str_path(self):
    69         assert as_str(Path("abc")) == "abc"
     72        assert as_str_path(Path("abc")) == "abc"
    7073
    7174    def test_type_error(self):
Note: See TracChangeset for help on using the changeset viewer.