Ignore:
Timestamp:
Aug 9, 2013, 9:55:55 PM (6 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Removed `mode` argument from `download` and `upload` methods.

From now on, all downloads and uploads are binary.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ftputil/host.py

    r1351 r1354  
    400400    def copyfileobj(self, source, target,
    401401                    max_chunk_size=ftputil.file_transfer.MAX_COPY_CHUNK_SIZE,
    402                     callback=None, **kwargs):
     402                    callback=None):
    403403        """
    404404        Copy data from file-like object `source` to file-like object
    405405        `target`.
    406406        """
    407         if "length" in kwargs:
    408             max_chunk_size = kwargs["length"]
    409             warnings.warn(("Parameter name `length` will be removed in "
    410                            "ftputil 2.6, use `max_chunk_size` instead"),
    411                           DeprecationWarning, stacklevel=2)
    412407        ftputil.file_transfer.copyfileobj(source, target, max_chunk_size,
    413408                                          callback)
    414409
    415     def __get_modes(self, mode):
    416         """Return modes for source and target file."""
    417         #XXX Should we allow mode "a" at all? We don't support appending!
    418         # Invalid mode values are handled when a file object is made.
    419         if mode == "b":
    420             return "rb", "wb"
    421         else:
    422             return "r", "w"
    423 
    424     def _upload_files(self, source_path, target_path, mode):
     410    def _upload_files(self, source_path, target_path):
    425411        """
    426412        Return a `LocalFile` and `RemoteFile` as source and target,
     
    430416        or relative) paths of the local and the remote file, respectively.
    431417        """
    432         source_mode, target_mode = self.__get_modes(mode)
    433         source_file = ftputil.file_transfer.LocalFile(source_path, source_mode)
     418        source_file = ftputil.file_transfer.LocalFile(source_path, "rb")
    434419        # Passing `self` (the `FTPHost` instance) here is correct.
    435         target_file = ftputil.file_transfer.RemoteFile(self, target_path,
    436                                                        target_mode)
     420        target_file = ftputil.file_transfer.RemoteFile(self, target_path, "wb")
    437421        return source_file, target_file
    438422
    439     def upload(self, source, target, mode="", callback=None):
     423    def upload(self, source, target, callback=None):
    440424        """
    441425        Upload a file from the local source (name) to the remote
    442         target (name). The argument `mode` is an empty string or "a" for
    443         text copies, or "b" for binary copies.
     426        target (name).
     427
     428        If a callable `callback` is given, it's called after every
     429        chunk of transferred data. The chunk size is a constant
     430        defined in `file_transfer`. The callback will be called with a
     431        single argument, the data chunk that was transferred before
     432        the callback was called.
    444433        """
    445434        target = ftputil.tool.as_unicode(target)
    446         source_file, target_file = self._upload_files(source, target, mode)
     435        source_file, target_file = self._upload_files(source, target)
    447436        ftputil.file_transfer.copy_file(source_file, target_file,
    448437                                        conditional=False, callback=callback)
    449438
    450     def upload_if_newer(self, source, target, mode="", callback=None):
     439    def upload_if_newer(self, source, target, callback=None):
    451440        """
    452441        Upload a file only if it's newer than the target on the
     
    455444
    456445        If an upload was necessary, return `True`, else return `False`.
     446
     447        If a callable `callback` is given, it's called after every
     448        chunk of transferred data. The chunk size is a constant
     449        defined in `file_transfer`. The callback will be called with a
     450        single argument, the data chunk that was transferred before
     451        the callback was called.
    457452        """
    458453        target = ftputil.tool.as_unicode(target)
    459         source_file, target_file = self._upload_files(source, target, mode)
     454        source_file, target_file = self._upload_files(source, target)
    460455        return ftputil.file_transfer.copy_file(source_file, target_file,
    461456                                               conditional=True,
    462457                                               callback=callback)
    463458
    464     def _download_files(self, source_path, target_path, mode):
     459    def _download_files(self, source_path, target_path):
    465460        """
    466461        Return a `RemoteFile` and `LocalFile` as source and target,
     
    470465        or relative) paths of the remote and the local file, respectively.
    471466        """
    472         source_mode, target_mode = self.__get_modes(mode)
    473         source_file = ftputil.file_transfer.RemoteFile(self, source_path,
    474                                                        source_mode)
    475         target_file = ftputil.file_transfer.LocalFile(target_path, target_mode)
     467        source_file = ftputil.file_transfer.RemoteFile(self, source_path, "rb")
     468        target_file = ftputil.file_transfer.LocalFile(target_path, "wb")
    476469        return source_file, target_file
    477470
    478     def download(self, source, target, mode="", callback=None):
     471    def download(self, source, target, callback=None):
    479472        """
    480473        Download a file from the remote source (name) to the local
    481         target (name). The argument mode is an empty string or "a" for
    482         text copies, or "b" for binary copies.
     474        target (name).
     475
     476        If a callable `callback` is given, it's called after every
     477        chunk of transferred data. The chunk size is a constant
     478        defined in `file_transfer`. The callback will be called with a
     479        single argument, the data chunk that was transferred before
     480        the callback was called.
    483481        """
    484482        source = ftputil.tool.as_unicode(source)
    485         source_file, target_file = self._download_files(source, target, mode)
     483        source_file, target_file = self._download_files(source, target)
    486484        ftputil.file_transfer.copy_file(source_file, target_file,
    487485                                        conditional=False, callback=callback)
    488486
    489     def download_if_newer(self, source, target, mode="", callback=None):
     487    def download_if_newer(self, source, target, callback=None):
    490488        """
    491489        Download a file only if it's newer than the target on the
     
    495493        If a download was necessary, return `True`, else return
    496494        `False`.
     495
     496        If a callable `callback` is given, it's called after every
     497        chunk of transferred data. The chunk size is a constant
     498        defined in `file_transfer`. The callback will be called with a
     499        single argument, the data chunk that was transferred before
     500        the callback was called.
    497501        """
    498502        source = ftputil.tool.as_unicode(source)
    499         source_file, target_file = self._download_files(source, target, mode)
     503        source_file, target_file = self._download_files(source, target)
    500504        return ftputil.file_transfer.copy_file(source_file, target_file,
    501505                                               conditional=True,
Note: See TracChangeset for help on using the changeset viewer.