diff --git a/CHANGELOG.md b/CHANGELOG.md index f7b1dadf7c6df210938475156c0805b8c9c48b62..9d4a487d2d3d0e300afcb969d48a2eb80de396ba 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 ebafbbada34738e5ad34d8738559a1b011023ba3..0ae5d7b737e24a25dc7fe6842f9dd13416a0fb38 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 9d72afdac93a564a97d2faeaf35e6c0c5163787e..edf0f4fc4f2f786bb2b3e3783bf0631be86c70a2 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 d0adccdafdfb9d0a9c4dd8b99c8a5122b86947c3..d637cf720e919414eb0e6c30d7b4f298419c60ad 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')