-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagger.py
76 lines (57 loc) · 1.74 KB
/
tagger.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
70
71
72
73
74
import os
import glob
tagfs_linkmap = {}
occupancy_map = {}
class Tagger():
def __init__(self, root):
linkmap = {}
links = glob.glob(root + "/*/*")
for link in links:
(tag_dir, link_name) = os.path.split(link)
tag = os.path.basename(tag_dir)
if tag[0] == ".":
continue
realpath = os.path.realpath(link)
if "@" in link_name:
(filename, sep, link_index) = link_name.partition("@")
link_index = int(link_index)
else:
filename = link_name
link_index = 1
if (filename, tag) in occupancy_map:
occupancy_map[(filename, tag)] = max(link_index, occupancy_map[(filename, tag)])
else:
occupancy_map[(filename, tag)] = link_index
if realpath not in linkmap:
linkmap[realpath] = [(tag, realpath)]
else:
linkmap[realpath].append((tag, realpath))
self.linkmap = linkmap
self.occupancy_map = occupancy_map
self.root = root
def update_tags(self, filepath, tags):
# No need to update tagfs_linkmap, each filepath will only be processed once
if filepath == "":
return
filename = os.path.basename(filepath)
links = self.linkmap.get(filepath, [])
old_tags = set([])
new_links = set([])
for (tag, realpath) in links:
if tag not in tags:
os.remove(link)
continue
else:
old_tags.add(tag)
for tag in tags - old_tags:
tag_dir = self.root + "/" + tag
if not os.path.exists(tag_dir):
os.mkdir(tag_dir)
if (filename, tag) in occupancy_map:
os.symlink(filepath, tag_dir + "/" + filename + "@" + str(self.occupancy_map[(filename, tag)]))
self.occupancy_map[(filename, tag)] += 1
else:
os.symlink(filepath, tag_dir + "/" + filename)
self.occupancy_map[(filename, tag)] = 1
# Just a small precautionary measure
tagfs_linkmap[filepath] = None