on launch and on exit sync hooks
[sastry/taskwarrior_utils.git] / hooks / on-exit.background-sync.py
1 #!/usr/bin/env python
2 # Author: Srikanth Sastry (sastry@csail.mit.edu)
3 #
4 # Description:
5 # This hook runs `task sync` in the background at the end of each task command that mutates
6 # the task db. The list MUTATING_COMMANDS has the list.
7 #
8 # Usage: simply copy this file to ~/.tasks/hooks, or whatever your hooks directory is
9 # as specified in you taskrc file.
10 #
11 # Compatibility: This hook uses the Hooks V2 API (https://taskwarrior.org/docs/hooks2.html)
12 # So, it works with TaskWarrior 2.4.3+
13
14 import sys
15 from subprocess import Popen, PIPE
16
17 MUTATING_COMMANDS = ['add', 'modify', 'done', 'start', 'stop', 'annotate', 'delete']
18 args = sys.argv
19 arg_map = {}
20 for arg in args:
21 arg_pair = arg.split(':')
22 if len(arg_pair) != 2:
23 continue
24 arg_map[arg_pair[0]] = arg_pair[1]
25 process = None
26 if arg_map.get('command', '') in MUTATING_COMMANDS:
27 process = Popen(['task', 'synchronize', 'rc.hooks=off'], stdout=PIPE, stderr=PIPE)
28 print("Synchronizing in the background on exit")
29
30 sys.exit(0)