Changeset 1618:3cb69eca3e41


Ignore:
Timestamp:
Dec 23, 2015, 5:10:41 PM (5 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Add `rest` argument to `FTPHost.open`

Allow a `rest` argument in `FTPHost.open`. Pass this on to
`FTPFile._open` and from there to `self._session.transfercmd`.

So far there's no error handling.
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ftputil/file.py

    r1611 r1618  
    4242
    4343    def _open(self, path, mode, buffering=None, encoding=None, errors=None,
    44               newline=None):
     44              newline=None, rest=None):
    4545        """
    4646        Open the remote file with given path name and mode.
     
    9090        # Get connection and file object.
    9191        with ftputil.error.ftplib_error_to_ftp_io_error:
    92             self._conn = self._session.transfercmd(command)
     92            self._conn = self._session.transfercmd(command, rest)
    9393        # The file object. Under Python 3, this will already be a
    9494        # `BufferedReader` or `BufferedWriter` object.
  • ftputil/host.py

    r1617 r1618  
    175175
    176176    def open(self, path, mode="r", buffering=None, encoding=None, errors=None,
    177              newline=None):
     177             newline=None, rest=None):
    178178        """
    179179        Return an open file(-like) object which is associated with
    180180        this `FTPHost` object.
     181
     182        The arguments `path`, `mode`, `buffering`, `encoding`, `errors`
     183        and `newlines` have the same meaning as for `io.open`. If `rest`
     184        is given as an integer,
     185
     186        - reading will start at the byte (zero-based) `rest`
     187        - writing will overwrite the remote file from byte `rest`
    181188
    182189        This method tries to reuse a child but will generate a new one
     
    209216                    format(effective_dir))
    210217        host._file._open(effective_file, mode=mode, buffering=buffering,
    211                          encoding=encoding, errors=errors, newline=newline)
     218                         encoding=encoding, errors=errors, newline=newline,
     219                         rest=rest)
    212220        if "w" in mode:
    213221            # Invalidate cache entry because size and timestamps will change.
  • test/test_real_ftp.py

    r1615 r1618  
    772772
    773773
     774class TestRestArgument(RealFTPTest):
     775
     776    TEST_FILE_NAME = "rest_test"
     777
     778    def setUp(self):
     779        super(TestRestArgument, self).setUp()
     780        # Write test file.
     781        with self.host.open(self.TEST_FILE_NAME, "wb") as fobj:
     782            fobj.write(b"abcdefghijkl")
     783        self.cleaner.add_file(self.TEST_FILE_NAME)
     784
     785    def test_for_reading(self):
     786        """
     787        If a `rest` argument is passed to `open`, the following read
     788        operation should start at the byte given by `rest`.
     789        """
     790        with self.host.open(self.TEST_FILE_NAME, "rb", rest=3) as fobj:
     791            data = fobj.read()
     792        self.assertEqual(data, b"defghijkl")
     793
     794    def test_for_writing(self):
     795        """
     796        If a `rest` argument is passed to `open`, the following write
     797        operation should start writing at the byte given by `rest`.
     798        """
     799        with self.host.open(self.TEST_FILE_NAME, "wb", rest=3) as fobj:
     800            fobj.write(b"123")
     801        with self.host.open(self.TEST_FILE_NAME, "rb") as fobj:
     802            data = fobj.read()
     803        self.assertEqual(data, b"abc123")
     804
     805
    774806class TestOther(RealFTPTest):
    775807
Note: See TracChangeset for help on using the changeset viewer.