summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-x.local/share/task/hooks/on-modify.timewarrior102
1 files changed, 102 insertions, 0 deletions
diff --git a/.local/share/task/hooks/on-modify.timewarrior b/.local/share/task/hooks/on-modify.timewarrior
new file mode 100755
index 0000000..83a1971
--- /dev/null
+++ b/.local/share/task/hooks/on-modify.timewarrior
@@ -0,0 +1,102 @@
1#!/usr/bin/env python
2
3###############################################################################
4#
5# Copyright 2016 - 2020, Thomas Lauf, Paul Beckingham, Federico Hernandez.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a copy
8# of this software and associated documentation files (the "Software"), to deal
9# in the Software without restriction, including without limitation the rights
10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11# copies of the Software, and to permit persons to whom the Software is
12# furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23# SOFTWARE.
24#
25# https://www.opensource.org/licenses/mit-license.php
26#
27###############################################################################
28
29import json
30import subprocess
31import sys
32
33# Hook should extract all of the following for use as Timewarrior tags:
34# UUID
35# Project
36# Tags
37# Description
38# UDAs
39
40try:
41 input_stream = sys.stdin.buffer
42except AttributeError:
43 input_stream = sys.stdin
44
45# Make no changes to the task, simply observe.
46old = json.loads(input_stream.readline().decode("utf-8", errors="replace"))
47new = json.loads(input_stream.readline().decode("utf-8", errors="replace"))
48print(json.dumps(new))
49
50
51def extract_tags_from(json_obj):
52 # Extract attributes for use as tags.
53 tags = [json_obj['description']]
54
55 if 'project' in json_obj:
56 task_tags = json_obj['project']
57 task_tags_list = task_tags.split(".")
58 tags.extend(task_tags_list)
59
60 # if 'tags' in json_obj:
61 # tags.extend(json_obj['tags'])
62
63 return tags
64
65
66def extract_annotation_from(json_obj):
67
68 if 'annotations' not in json_obj:
69 return '\'\''
70
71 return json_obj['annotations'][0]['description']
72
73
74start_or_stop = ''
75
76# Started task.
77if 'start' in new and 'start' not in old:
78 start_or_stop = 'start'
79
80# Stopped task.
81elif ('start' not in new or 'end' in new) and 'start' in old:
82 start_or_stop = 'stop'
83
84if start_or_stop:
85 tags = extract_tags_from(new)
86
87 subprocess.call(['timew', start_or_stop] + tags + [':yes'])
88
89# Modifications to task other than start/stop
90elif 'start' in new and 'start' in old:
91 old_tags = extract_tags_from(old)
92 new_tags = extract_tags_from(new)
93
94 if old_tags != new_tags:
95 subprocess.call(['timew', 'untag', '@1'] + old_tags + [':yes'])
96 subprocess.call(['timew', 'tag', '@1'] + new_tags + [':yes'])
97
98 old_annotation = extract_annotation_from(old)
99 new_annotation = extract_annotation_from(new)
100
101 if old_annotation != new_annotation:
102 subprocess.call(['timew', 'annotate', '@1', new_annotation])