From 1e2687e7e9a392960e7fec12862804d118ad9366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Fri, 1 Mar 2024 11:47:16 +0100 Subject: [PATCH] * added AutoflushFile.seek() * added AutoflushFile.truncate() --- CHANGELOG.md | 2 ++ satella/__init__.py | 2 +- satella/files.py | 23 ++++++++++++++++++++++- tests/test_files.py | 2 ++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7b1dadf..9d4a487d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,4 @@ # v2.23.5 +* added AutoflushFile.seek() +* added AutoflushFile.truncate() diff --git a/satella/__init__.py b/satella/__init__.py index ebafbbad..0ae5d7b7 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.23.5a1' +__version__ = '2.23.5a2' diff --git a/satella/files.py b/satella/files.py index 9d72afda..edf0f4fc 100644 --- a/satella/files.py +++ b/satella/files.py @@ -378,6 +378,13 @@ class AutoflushFile(Proxy[io.FileIO]): super().__init__(fle) self.__dict__['pointer'] = fle.tell() + def seek(self, *args, **kwargs) -> int: + """Seek to a provided position within the file""" + fle = self._open_file() + v = fle.seek(*args, **kwargs) + self.__dict__['pointer'] = fle.tell() + return v + def read(self, *args, **kwargs) -> tp.Union[str, bytes]: """ Read a file, returning the read-in data @@ -393,7 +400,12 @@ class AutoflushFile(Proxy[io.FileIO]): def _get_file(self) -> tp.Optional[AutoflushFile]: return self.__dict__.get('_Proxy__obj') - def _open_file(self) -> open: + def readall(self) -> tp.Union[str, bytes]: + """Read all contents into the file""" + file = self._open_file() + return file.readall() + + def _open_file(self) -> io.FileIO: file = self._get_file() if file is None: file = open(*self.con_args, **self.con_kwargs) @@ -428,3 +440,12 @@ class AutoflushFile(Proxy[io.FileIO]): self.__dict__['pointer'] = file.tell() self._close_file() return val + + def truncate(self, __size: tp.Optional[int] = None) -> int: + """Truncate file to __size starting bytes""" + fle = self._open_file() + v = fle.truncate(__size) + self.__dict__['pointer'] = fle.tell() + self._close_file() + return v + diff --git a/tests/test_files.py b/tests/test_files.py index d0adccda..d637cf72 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -26,6 +26,8 @@ class TestFiles(unittest.TestCase): assert read_in_file('test3.txt', encoding='utf-8') == 'test' af.write('test2') assert read_in_file('test3.txt', encoding='utf-8') == 'testtest2' + af.truncate(4) + assert read_in_file('test3.txt', encoding='utf-8') == 'test' finally: af.close() try_unlink('test3.txt') -- GitLab