-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrivialtags
executable file
·92 lines (84 loc) · 2.13 KB
/
trivialtags
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python
import re
import sys
import os
# tags["name of file"] = array of tags for specialized file
tags = {}
def read_tags(tags_string):
global tags
tags_string = tags_string.replace("\n", " ")
section_re = r"\s*([^{]+){\s*([^}]*)}"
matches = re.findall(section_re, tags_string)
for i in matches:
name, tgs = i
name = name.strip()
tags[name] = []
for tag in tgs.split(","):
tag = tag.strip()
tags[name].append(tag)
pass
pass
pass
def load_tags(path):
try:
f = open(path, 'r')
s = f.read()
read_tags(s)
f.close()
except Exception as N:
return
def serialize_tags():
def list_unique(l):
checked = []
for e in l:
if e not in checked:
checked.append(e)
return checked
s = ''
for file in tags.keys():
s += file
s += ' { '
s += ", ".join(list_unique(tags[file]))
s += ' }\n'
pass
return s
def store_tags(path):
f = open(path, 'w')
f.write(serialize_tags())
f.close()
argc = len(sys.argv)
if argc is 1 or argc > 3:
print("""
Usage:
trivialtags <name of file> [tag1, tag2, ...]
When no tag is passed, current tags for file will be printed.
When tag starts with character '-', appropriate tag will be extracted from file
If entire tag name is '-' all tags of file will be removed (of course, it should be the only specialized tag)""")
sys.exit(1)
else:
load_tags("./.tags")
pass
if argc is 2:
if sys.argv[1] not in tags.keys():
print("File " + sys.argv[1] + " is not tagged")
else:
print(", ".join(tags[sys.argv[1]]))
elif argc is 3:
name = sys.argv[1]
if name not in tags.keys():
tags[name] = []
for tag in sys.argv[2].split(","):
tag = tag.strip();
if tag[0] == '-':
tag = tag[1:]
if tag is "":
del tags[name]
else:
tags[name].remove(tag)
else:
tags[name].append(tag)
pass
store_tags("./.tags")
pass
else:
pass