mirror of
https://github.com/GothenburgBitFactory/task-timewarrior-hook.git
synced 2025-09-05 23:17:21 +02:00
Add main function to on_modify.py
Make it unit-testable
This commit is contained in:
parent
431db0fb84
commit
056d239f35
1 changed files with 30 additions and 26 deletions
30
on_modify.py
30
on_modify.py
|
@ -42,11 +42,6 @@ try:
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
input_stream = sys.stdin
|
input_stream = sys.stdin
|
||||||
|
|
||||||
# Make no changes to the task, simply observe.
|
|
||||||
old = json.loads(input_stream.readline().decode("utf-8", errors="replace"))
|
|
||||||
new = json.loads(input_stream.readline().decode("utf-8", errors="replace"))
|
|
||||||
print(json.dumps(new))
|
|
||||||
|
|
||||||
|
|
||||||
def extract_tags_from(json_obj):
|
def extract_tags_from(json_obj):
|
||||||
# Extract attributes for use as tags.
|
# Extract attributes for use as tags.
|
||||||
|
@ -69,23 +64,25 @@ def extract_annotation_from(json_obj):
|
||||||
return json_obj['annotations'][0]['description']
|
return json_obj['annotations'][0]['description']
|
||||||
|
|
||||||
|
|
||||||
start_or_stop = ''
|
def main(old, new):
|
||||||
|
|
||||||
# Started task.
|
start_or_stop = ''
|
||||||
if 'start' in new and 'start' not in old:
|
|
||||||
|
# Started task.
|
||||||
|
if 'start' in new and 'start' not in old:
|
||||||
start_or_stop = 'start'
|
start_or_stop = 'start'
|
||||||
|
|
||||||
# Stopped task.
|
# Stopped task.
|
||||||
elif ('start' not in new or 'end' in new) and 'start' in old:
|
elif ('start' not in new or 'end' in new) and 'start' in old:
|
||||||
start_or_stop = 'stop'
|
start_or_stop = 'stop'
|
||||||
|
|
||||||
if start_or_stop:
|
if start_or_stop:
|
||||||
tags = extract_tags_from(new)
|
tags = extract_tags_from(new)
|
||||||
|
|
||||||
subprocess.call(['timew', start_or_stop] + tags + [':yes'])
|
subprocess.call(['timew', start_or_stop] + tags + [':yes'])
|
||||||
|
|
||||||
# Modifications to task other than start/stop
|
# Modifications to task other than start/stop
|
||||||
elif 'start' in new and 'start' in old:
|
elif 'start' in new and 'start' in old:
|
||||||
old_tags = extract_tags_from(old)
|
old_tags = extract_tags_from(old)
|
||||||
new_tags = extract_tags_from(new)
|
new_tags = extract_tags_from(new)
|
||||||
|
|
||||||
|
@ -98,3 +95,10 @@ elif 'start' in new and 'start' in old:
|
||||||
|
|
||||||
if old_annotation != new_annotation:
|
if old_annotation != new_annotation:
|
||||||
subprocess.call(['timew', 'annotate', '@1', new_annotation])
|
subprocess.call(['timew', 'annotate', '@1', new_annotation])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
old = json.loads(input_stream.readline().decode("utf-8", errors="replace"))
|
||||||
|
new = json.loads(input_stream.readline().decode("utf-8", errors="replace"))
|
||||||
|
print(json.dumps(new))
|
||||||
|
main(old, new)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue