def save(self, on_new_path: Union[str, Path] = None, mode=None, exist_ok=False, overwrite=False, encoding=None, **kwargs): if (self._last_update is not None and overwrite is False) and not self._is_deleted: if self._last_update != self.updated_at: raise AssertionError( f"File change detected (last change {self.updated_at}), if you want to overwrite " f"set overwrite=True") if on_new_path is not None: self.set_path(on_new_path) assert exist_ok or not self.path.exists, FileExistsError( "File exists. If you want override, please send 'exist_ok=True'") assert self._only_read is False, f"{self.name} is only read." mode = mode or self._get_mode('w') assert 'w' in mode, f"{mode} for write isn't valid." encoding = None if self._is_byte else 'utf-8' with tempfile.TemporaryDirectory() as tmpdirname: tmp_file = Path(tmpdirname).join(self.name) if not self._newline and not self._is_byte: kwargs.update({'newline': ''}) try: with open(tmp_file, mode=mode, encoding=encoding, **kwargs) as fp: self._save_fp(fp) except Exception as err: raise IOError(f'Error writing to the file system file: {err}') tmp_file.cp(self.path)
def save(self, on_new_path: Union[str, Path] = None, exist_ok=False, overwrite=False, force=False, **kwargs): with tempfile.TemporaryDirectory() as tmpdirname: tmp_file = Path(tmpdirname).join(self.name) try: with ZipFile(tmp_file.path, mode='w', compression=ZIP_DEFLATED) as myzip: for i in self.data: myzip.write(i) except Exception as err: raise IOError(f'Error writing to the file system file: {err}') tmp_file.cp(self.path) self._last_update = self.updated_at