-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaintain_attributes.py
executable file
·70 lines (62 loc) · 3.08 KB
/
maintain_attributes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
from get_attribute import get_one_attribute,get_task_tags,get_task_children
import taskwarrior.taskwarrior as taskwarrior
####################################### attributes values to filter form
def convert_tags_to_filter_form(tags):
tags_in_filter_form = ''
if len(tags):
for tag in tags:
tags_in_filter_form += f"+{tag} "
return tags_in_filter_form
def convert_children_to_filter_form(children):
if len(children) == 0:
return "child:"
children_in_filter_form = f'child:{children[0]}'
children = children[1:]
if len(children):
for child in children:
children_in_filter_form += f",{child}"
return children_in_filter_form
def convert_attribute_to_filter_form(attribute_name,attribute_value):
attributes_to_filter_convert_method = {
"project" : lambda x : f"project:\"{x}\"",
"tag" : convert_tags_to_filter_form,
"child" : convert_children_to_filter_form,
"parents" : lambda x : f"parents:{x}",
"depends" : lambda x : f"depends:{x}"
}
return attributes_to_filter_convert_method[attribute_name](attribute_value)
############################# filter form to normal form ######################################
def convert_tag_filter_to_list(tags_in_filter_form):
tags_and_plussign = tags_in_filter_form.strip().split(' ')
tags = [ tag_and_plus[1:] for tag_and_plus in tags_and_plussign]
if tags[0] == '':
tags = []
return tags
def change_child_filter_to_list(child_in_filter_form):
children = child_in_filter_form.strip().split(',')
if children[0] == '':
children = []
return children
def convert_filter_form_to_attributes(attribute_name,attribute_filter):
filter_to_attributes_convert_method = {
"project" : lambda x : x,
"tag" : convert_tag_filter_to_list,
"child" : change_child_filter_to_list,
"parents" : lambda x : x,
"depends" : lambda x : x
}
return filter_to_attributes_convert_method[attribute_name](attribute_filter)
####################################################################
def maintain_attributes(task_uuid, attributes):
taskwarrior.modify_task_attributes(task_uuid, attributes)
def append_attributes(task_uuid, attribute_name,attribute_value):
append_attributes_to_original = {
"project" : lambda uuid,new : new if len(new) else get_one_attribute("project",f"uuid:{uuid}")[0],
"tag" : lambda uuid,new : get_task_tags(uuid) + [new] if len(new) else get_task_tags(uuid),
"child" : lambda uuid,new : get_task_children(uuid) + [new] if len(new) else get_task_children(uuid),
"parents" : lambda uuid,new : new if len(new) else get_one_attribute("parents",f"uuid:{uuid}")[0],
"depends" : lambda uuid,new : new if len(new) else get_one_attribute("depends",f"uuid:{uuid}")[0]
}
new_attributes = append_attributes_to_original[attribute_name](task_uuid,attribute_value)
maintain_attributes(task_uuid,{attribute_name:new_attributes})