add pre_deletion_hook

closes https://github.com/pimutils/vdirsyncer/issues/1107
This commit is contained in:
chrisblech 2024-01-01 16:55:31 +01:00 committed by Hugo
parent 42c5dba208
commit 6a3077f9dc
2 changed files with 15 additions and 0 deletions

View file

@ -378,6 +378,7 @@ Local
fileext = "..." fileext = "..."
#encoding = "utf-8" #encoding = "utf-8"
#post_hook = null #post_hook = null
#pre_deletion_hook = null
#fileignoreext = ".tmp" #fileignoreext = ".tmp"
Can be used with `khal <http://lostpackets.de/khal/>`_. See :doc:`vdir` for Can be used with `khal <http://lostpackets.de/khal/>`_. See :doc:`vdir` for
@ -399,6 +400,8 @@ Local
:param post_hook: A command to call for each item creation and :param post_hook: A command to call for each item creation and
modification. The command will be called with the path of the modification. The command will be called with the path of the
new/updated file. new/updated file.
:param pre_deletion_hook: A command to call for each item deletion.
The command will be called with the path of the deleted file.
:param fileeignoreext: The file extention to ignore. It is only useful :param fileeignoreext: The file extention to ignore. It is only useful
if fileext is set to the empty string. The default is ``.tmp``. if fileext is set to the empty string. The default is ``.tmp``.

View file

@ -29,6 +29,7 @@ class FilesystemStorage(Storage):
fileext, fileext,
encoding="utf-8", encoding="utf-8",
post_hook=None, post_hook=None,
pre_deletion_hook=None,
fileignoreext=".tmp", fileignoreext=".tmp",
**kwargs, **kwargs,
): ):
@ -40,6 +41,7 @@ class FilesystemStorage(Storage):
self.fileext = fileext self.fileext = fileext
self.fileignoreext = fileignoreext self.fileignoreext = fileignoreext
self.post_hook = post_hook self.post_hook = post_hook
self.pre_deletion_hook = pre_deletion_hook
@classmethod @classmethod
async def discover(cls, path, **kwargs): async def discover(cls, path, **kwargs):
@ -166,6 +168,9 @@ class FilesystemStorage(Storage):
actual_etag = get_etag_from_file(fpath) actual_etag = get_etag_from_file(fpath)
if etag != actual_etag: if etag != actual_etag:
raise exceptions.WrongEtagError(etag, actual_etag) raise exceptions.WrongEtagError(etag, actual_etag)
if self.pre_deletion_hook:
self._run_pre_deletion_hook(fpath)
os.remove(fpath) os.remove(fpath)
def _run_post_hook(self, fpath): def _run_post_hook(self, fpath):
@ -175,6 +180,13 @@ class FilesystemStorage(Storage):
except OSError as e: except OSError as e:
logger.warning(f"Error executing external hook: {str(e)}") logger.warning(f"Error executing external hook: {str(e)}")
def _run_pre_deletion_hook(self, fpath):
logger.info(f"Calling pre_deletion_hook={self.pre_deletion_hook} with argument={fpath}")
try:
subprocess.call([self.pre_deletion_hook, fpath])
except OSError as e:
logger.warning(f"Error executing external hook: {str(e)}")
async def get_meta(self, key): async def get_meta(self, key):
fpath = os.path.join(self.path, key) fpath = os.path.join(self.path, key)
try: try: