calcurse-caldav: Add hook support
Introduce pre-sync and post-sync hooks which need to be located under ~/.calcurse/caldav/hooks/ and are executed before/after synchronization with a CalDAV server. Also, add an example post-sync hook and change the example post-save hook such that it does not create tiny commits during synchronization. Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
parent
e40646d30f
commit
e5ba06ff5d
@ -350,6 +350,13 @@ def pull_objects(conn, syncdb, etagdict):
|
|||||||
return (added, deleted)
|
return (added, deleted)
|
||||||
|
|
||||||
|
|
||||||
|
def run_hook(name):
|
||||||
|
hook_path = hookdir + '/' + name
|
||||||
|
if not os.path.exists(hook_path):
|
||||||
|
return
|
||||||
|
subprocess.call(hook_path, shell=True)
|
||||||
|
|
||||||
|
|
||||||
# Initialize the XML namespace map.
|
# Initialize the XML namespace map.
|
||||||
nsmap = {"D": "DAV:", "C": "urn:ietf:params:xml:ns:caldav"}
|
nsmap = {"D": "DAV:", "C": "urn:ietf:params:xml:ns:caldav"}
|
||||||
|
|
||||||
@ -357,6 +364,7 @@ nsmap = {"D": "DAV:", "C": "urn:ietf:params:xml:ns:caldav"}
|
|||||||
configfn = os.path.expanduser("~/.calcurse/caldav/config")
|
configfn = os.path.expanduser("~/.calcurse/caldav/config")
|
||||||
lockfn = os.path.expanduser("~/.calcurse/caldav/lock")
|
lockfn = os.path.expanduser("~/.calcurse/caldav/lock")
|
||||||
syncdbfn = os.path.expanduser("~/.calcurse/caldav/sync.db")
|
syncdbfn = os.path.expanduser("~/.calcurse/caldav/sync.db")
|
||||||
|
hookdir = os.path.expanduser("~/.calcurse/caldav/hooks/")
|
||||||
|
|
||||||
# Parse command line arguments.
|
# Parse command line arguments.
|
||||||
parser = argparse.ArgumentParser('calcurse-caldav')
|
parser = argparse.ArgumentParser('calcurse-caldav')
|
||||||
@ -372,6 +380,9 @@ parser.add_argument('--lockfile', action='store', dest='lockfn',
|
|||||||
parser.add_argument('--syncdb', action='store', dest='syncdbfn',
|
parser.add_argument('--syncdb', action='store', dest='syncdbfn',
|
||||||
default=syncdbfn,
|
default=syncdbfn,
|
||||||
help='path to the calcurse-caldav sync DB')
|
help='path to the calcurse-caldav sync DB')
|
||||||
|
parser.add_argument('--hookdir', action='store', dest='hookdir',
|
||||||
|
default=hookdir,
|
||||||
|
help='path to the calcurse-caldav hooks directory')
|
||||||
parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
|
parser.add_argument('-v', '--verbose', action='store_true', dest='verbose',
|
||||||
default=False,
|
default=False,
|
||||||
help='print status messages to stdout')
|
help='print status messages to stdout')
|
||||||
@ -383,6 +394,7 @@ init = args.init is not None
|
|||||||
configfn = args.configfn
|
configfn = args.configfn
|
||||||
lockfn = args.lockfn
|
lockfn = args.lockfn
|
||||||
syncdbfn = args.syncdbfn
|
syncdbfn = args.syncdbfn
|
||||||
|
hookdir = args.hookdir
|
||||||
verbose = args.verbose
|
verbose = args.verbose
|
||||||
debug = args.debug
|
debug = args.debug
|
||||||
|
|
||||||
@ -449,6 +461,9 @@ elif ver < (4, 0, 0, 96):
|
|||||||
die('Incompatible calcurse binary detected. Version >=4.1.0 is required ' +
|
die('Incompatible calcurse binary detected. Version >=4.1.0 is required ' +
|
||||||
'to synchronize with CalDAV servers.')
|
'to synchronize with CalDAV servers.')
|
||||||
|
|
||||||
|
# Run the pre-sync hook.
|
||||||
|
run_hook('pre-sync')
|
||||||
|
|
||||||
# Create lock file.
|
# Create lock file.
|
||||||
if os.path.exists(lockfn):
|
if os.path.exists(lockfn):
|
||||||
die('Leftover lock file detected. If there is no other synchronization ' +
|
die('Leftover lock file detected. If there is no other synchronization ' +
|
||||||
@ -512,6 +527,9 @@ finally:
|
|||||||
# Remove lock file.
|
# Remove lock file.
|
||||||
os.remove(lockfn)
|
os.remove(lockfn)
|
||||||
|
|
||||||
|
# Run the post-sync hook.
|
||||||
|
run_hook('post-sync')
|
||||||
|
|
||||||
# Print a summary to stdout.
|
# Print a summary to stdout.
|
||||||
print("{} items imported, {} items removed locally.".
|
print("{} items imported, {} items removed locally.".
|
||||||
format(local_new, local_del))
|
format(local_new, local_del))
|
||||||
|
12
contrib/caldav/hooks/post-sync
Executable file
12
contrib/caldav/hooks/post-sync
Executable file
@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# Copy this file into ~/.calcurse/caldav/hooks/ and mark it executable to
|
||||||
|
# automatically commit changes after syncing using Git.
|
||||||
|
|
||||||
|
cd "$HOME"/.calcurse/
|
||||||
|
|
||||||
|
git add *
|
||||||
|
if ! git diff-index --quiet --cached HEAD; then
|
||||||
|
git commit -m "Automatic commit by the post-sync hook"
|
||||||
|
fi
|
||||||
|
|
@ -4,6 +4,10 @@
|
|||||||
# automatically commit any changes to the calcurse data files using Git.
|
# automatically commit any changes to the calcurse data files using Git.
|
||||||
|
|
||||||
cd "$HOME"/.calcurse/
|
cd "$HOME"/.calcurse/
|
||||||
|
|
||||||
|
# Do not make commits when synchronizing with a CalDAV server.
|
||||||
|
[ -f caldav/lock ] && exit
|
||||||
|
|
||||||
git add *
|
git add *
|
||||||
if ! git diff-index --quiet --cached HEAD; then
|
if ! git diff-index --quiet --cached HEAD; then
|
||||||
git commit -m "Automatic commit by the post-save hook"
|
git commit -m "Automatic commit by the post-save hook"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user