mirror of
https://github.com/sahinakkaya/dotfiles.git
synced 2024-11-10 02:59:37 +01:00
68 lines
2.1 KiB
Python
Executable File
68 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
'''Timewarrior on-modify hook to call timew on start,stop, done commands.'''
|
|
import json
|
|
import shlex
|
|
import subprocess
|
|
import sys
|
|
|
|
def retrieve_args_dict():
|
|
'''Read process arguments and store them in a dictionary.'''
|
|
process_args = sys.argv[1:]
|
|
dictionary = dict()
|
|
for process_arg in process_args:
|
|
splitted = process_arg.split(':')
|
|
if len(splitted) > 1:
|
|
key = splitted[0]
|
|
value = ''.join(splitted[1:])
|
|
dictionary[key] = value
|
|
return dictionary
|
|
|
|
def determine_timew_tags(task):
|
|
'''Determine which timew "tags" should be used for the task.'''
|
|
tags = list()
|
|
|
|
# Keys associated with one value
|
|
for key in ['uuid', 'project', 'description']:
|
|
if key in task:
|
|
tags.append('{}:{}'.format(key, task[key]))
|
|
|
|
# Keys associated with several values
|
|
if 'tags' in task:
|
|
for value in task['tags']:
|
|
tags.append('tag:{}'.format(value))
|
|
|
|
return tags
|
|
|
|
def generate_timew_command(cmd, tags):
|
|
'''Generate an input for subprocess.call.'''
|
|
tags_as_string = ' '.join([shlex.quote(tag) for tag in tags])
|
|
if cmd == 'done':
|
|
cmd = 'stop'
|
|
cmd_string = f'timew {cmd} {tags_as_string} :yes'
|
|
return shlex.split(cmd_string)
|
|
|
|
def main():
|
|
'''Main function of this module.'''
|
|
old_task = json.loads(sys.stdin.readline())
|
|
new_task = json.loads(sys.stdin.readline())
|
|
args = retrieve_args_dict()
|
|
|
|
# Do something.
|
|
feedback = None
|
|
if 'api' in args: # Only do something for known API.
|
|
if args['api'] in ['2']: # APIs that give us a 'command' key.
|
|
cmd = args['command']
|
|
if cmd in ['start', 'stop', 'done']: # Only do something on 'start', 'stop' or 'done'
|
|
timew_tags = determine_timew_tags(new_task)
|
|
timew_cmd = generate_timew_command(cmd, timew_tags)
|
|
subprocess.call(timew_cmd)
|
|
|
|
# Generate output as task expects it.
|
|
print(json.dumps(new_task))
|
|
if feedback is not None:
|
|
print(feedback)
|
|
sys.exit(0)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|