/
json_gen.py
75 lines (61 loc) · 1.92 KB
/
json_gen.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
75
#!/usr/bin/env python3
#coding=utf8
import json, re
# Destination object
tree = {}
# List of references to all subtrees which contains current guy
subtrees = [None]*100
# Read source file
with open("source.data") as fp:
for line in fp:
# Skip empty and commented lines
v = re.match(r'^\s*#', line)
if len(line) <= 1 or v:
continue
# Count number of tabs
m = re.match(r'^(\s*)(.+)', line)
tabs = len(m.group(1)) // 4
### Read parameters of a guy
groups = line.split('|')
name = '|'.join(groups[0:1]).strip()
line_width = '|'.join(groups[1:2]).strip()
line_color = '|'.join(groups[2:3]).strip()
radius = '|'.join(groups[3:4]).strip()
granica = '|'.join(groups[4:5]).strip()
birth = '|'.join(groups[5:6]).strip()
death = '|'.join(groups[6:7]).strip()
# set invisible parameters
if name == 'invisible':
name = ''
granica = 'white'
# Person info
guy = {
'name' : name,
'radius' : radius,
'line_color' : line_color,
'line_width' : line_width,
'granica' : granica,
'birth' : birth,
'death' : death
}
# Tree root
if tabs == 0:
tree = guy
subtrees[0] = tree
continue
# Add guy to parent's subtree
if 'children' in subtrees[tabs-1]:
subtrees[tabs-1]['children'].append(guy)
else:
subtrees[tabs-1]['children'] = [guy]
# Set current guy as subtree
subtrees[tabs] = subtrees[tabs-1]['children'][-1]
# Serialize tree object as JSON
with open('tree.json', 'w') as fp:
json.dump(
tree,
fp,
sort_keys = False,
indent = None,
ensure_ascii = False
)