-
Notifications
You must be signed in to change notification settings - Fork 30
/
assign_ids.py
42 lines (34 loc) · 1.33 KB
/
assign_ids.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
import os
import sys
import random
import string
ID_LENGTH = 8
def id_file(content_path, tag):
return os.path.join(content_path, 'concepts', tag, 'id.txt')
def random_id():
"""Generate a random ID for a concept. The IDs are arbitrary, apart from the requirement that they be distinct."""
return ''.join([random.choice(string.lowercase + string.digits) for i in range(ID_LENGTH)])
def assign_ids(content_path):
"""Assign unique IDs to all of the concepts which don't already have IDs."""
IGNORE_TAGS = ['.DS_Store', 'ANNOTATED_EXAMPLE']
nodes_path = os.path.join(content_path, 'concepts')
tags = os.listdir(nodes_path)
tags = filter(lambda t: not t in IGNORE_TAGS, tags)
# read in current ID strings
ids = set()
for tag in tags:
if os.path.exists(id_file(content_path, tag)):
node_id = open(id_file(content_path, tag)).read().strip()
ids.add(node_id)
for tag in tags:
if not os.path.exists(id_file(content_path, tag)):
new_id = None
while new_id is None or new_id in ids:
new_id = random_id()
open(id_file(content_path, tag), 'w').write(new_id)
if __name__ == "__main__":
if len(sys.argv) > 1:
content_path = sys.argv[1]
else:
content_path = os.getcwd()
assign_ids(content_path)