on launch and on exit sync hooks master
authorSrikanth Sastry <sastry@csail.mit.edu>
Tue, 8 Jan 2019 20:15:29 +0000 (15:15 -0500)
committerSrikanth Sastry <sastry@csail.mit.edu>
Tue, 8 Jan 2019 20:15:29 +0000 (15:15 -0500)
hooks/on-exit.background-sync.py [new file with mode: 0644]
hooks/on-launch.background-sync.py [new file with mode: 0644]

diff --git a/hooks/on-exit.background-sync.py b/hooks/on-exit.background-sync.py
new file mode 100644 (file)
index 0000000..c36e4ba
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+# Author: Srikanth Sastry (sastry@csail.mit.edu)
+#
+# Description:
+# This hook runs `task sync` in the background at the end of each task command that mutates
+# the task db. The list MUTATING_COMMANDS has the list.
+#
+# Usage: simply copy this file to ~/.tasks/hooks, or whatever your hooks directory is
+# as specified in you taskrc file.
+#
+# Compatibility: This hook uses the Hooks V2 API (https://taskwarrior.org/docs/hooks2.html)
+# So, it works with TaskWarrior 2.4.3+
+
+import sys
+from subprocess import Popen, PIPE
+
+MUTATING_COMMANDS = ['add', 'modify', 'done', 'start', 'stop', 'annotate', 'delete']
+args = sys.argv
+arg_map = {}
+for arg in args:
+    arg_pair = arg.split(':')
+    if len(arg_pair) != 2:
+        continue
+    arg_map[arg_pair[0]] = arg_pair[1]
+process = None
+if arg_map.get('command', '') in MUTATING_COMMANDS:
+    process = Popen(['task', 'synchronize', 'rc.hooks=off'], stdout=PIPE, stderr=PIPE)
+    print("Synchronizing in the background on exit")
+
+sys.exit(0)
diff --git a/hooks/on-launch.background-sync.py b/hooks/on-launch.background-sync.py
new file mode 100644 (file)
index 0000000..02d3946
--- /dev/null
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+# Author: Srikanth Sastry (sastry@csail.mit.edu)
+#
+# Description:
+# This hook runs `task sync` in the background at the start of each task command, except
+# `task sync` itself.
+#
+# Usage: simply copy this file to ~/.tasks/hooks, or whatever your hooks directory is
+# as specified in you taskrc file.
+#
+# Compatibility: This hook uses the Hooks V2 API (https://taskwarrior.org/docs/hooks2.html)
+# So, it works with TaskWarrior 2.4.3+
+
+import sys
+import json
+
+from subprocess import Popen, PIPE
+
+args = sys.argv
+arg_map = {}
+for arg in args:
+    arg_pair = arg.split(':')
+    if len(arg_pair) != 2:
+        continue
+    arg_map[arg_pair[0]] = arg_pair[1]
+process = None
+if arg_map.get('command', '') == 'synchronize':
+    sys.exit(0)
+else:
+    process = Popen(['task', 'synchronize', 'rc.hooks=off'], stdout=PIPE, stderr=PIPE)
+    print("Synchronizing in the background")
+
+sys.exit(0)