Skip to content

Commit 6a3077f

Browse files
chrisblechWhyNotHugo
authored andcommitted
add pre_deletion_hook
closes pimutils#1107
1 parent 42c5dba commit 6a3077f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

docs/config.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ Local
378378
fileext = "..."
379379
#encoding = "utf-8"
380380
#post_hook = null
381+
#pre_deletion_hook = null
381382
#fileignoreext = ".tmp"
382383

383384
Can be used with `khal <http://lostpackets.de/khal/>`_. See :doc:`vdir` for
@@ -399,6 +400,8 @@ Local
399400
:param post_hook: A command to call for each item creation and
400401
modification. The command will be called with the path of the
401402
new/updated file.
403+
:param pre_deletion_hook: A command to call for each item deletion.
404+
The command will be called with the path of the deleted file.
402405
:param fileeignoreext: The file extention to ignore. It is only useful
403406
if fileext is set to the empty string. The default is ``.tmp``.
404407

vdirsyncer/storage/filesystem.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def __init__(
2929
fileext,
3030
encoding="utf-8",
3131
post_hook=None,
32+
pre_deletion_hook=None,
3233
fileignoreext=".tmp",
3334
**kwargs,
3435
):
@@ -40,6 +41,7 @@ def __init__(
4041
self.fileext = fileext
4142
self.fileignoreext = fileignoreext
4243
self.post_hook = post_hook
44+
self.pre_deletion_hook = pre_deletion_hook
4345

4446
@classmethod
4547
async def discover(cls, path, **kwargs):
@@ -166,6 +168,9 @@ async def delete(self, href, etag):
166168
actual_etag = get_etag_from_file(fpath)
167169
if etag != actual_etag:
168170
raise exceptions.WrongEtagError(etag, actual_etag)
171+
if self.pre_deletion_hook:
172+
self._run_pre_deletion_hook(fpath)
173+
169174
os.remove(fpath)
170175

171176
def _run_post_hook(self, fpath):
@@ -175,6 +180,13 @@ def _run_post_hook(self, fpath):
175180
except OSError as e:
176181
logger.warning(f"Error executing external hook: {str(e)}")
177182

183+
def _run_pre_deletion_hook(self, fpath):
184+
logger.info(f"Calling pre_deletion_hook={self.pre_deletion_hook} with argument={fpath}")
185+
try:
186+
subprocess.call([self.pre_deletion_hook, fpath])
187+
except OSError as e:
188+
logger.warning(f"Error executing external hook: {str(e)}")
189+
178190
async def get_meta(self, key):
179191
fpath = os.path.join(self.path, key)
180192
try:

0 commit comments

Comments
 (0)