-
Notifications
You must be signed in to change notification settings - Fork 0
/
oldtree.py
220 lines (177 loc) · 6.89 KB
/
oldtree.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import json
# my tree implementation
class Tree:
nodes={}
idf=None # id field, e.g. 'name' or 'id', must be unique
pf=None # parent field e.g. 'parent'. child pf must be equal to idf of parent
pathf=None # path element, must be unique among children of parent
valuef=None
separator=':'
d=None #tree as dict
def __init__(self, idf='id', pf='parent',pathf='name',separator=':',valuef='value'):
self.idf=idf
self.pf=pf
self.pathf=pathf
self.separator=separator
self.valuef=valuef
self.d=None
def getdict(self,parentid=None):
# if we already have dict, return it
def resolvedict(d,path='',depth=5):
if depth==0:
return None
# walk this path
if path:
subd=d
for pelem in path.split(self.separator):
if pelem in subd:
subd = subd[pelem]
else:
return dict()
else:
subd=d
# now subd is part
dd={}
# first - speccommands
for k in subd:
if k.startswith('@'):
spec = k.split(' ')
if spec[0]=='@include':
# get subdict
isubd = resolvedict(d,spec[1],depth-1)
if isubd is not None:
#print "isubd:",isubd
#print json.dumps(isubd,indent=4)
for kk in isubd:
dd[kk]=isubd[kk]
elif spec[0]=='@access':
pass
else:
print "!!! unknown special command '{}'".format(spec[0])
# simulate string
dd[k] = subd[k]
# now other keys
for k in subd:
if isinstance(subd[k],dict):
# calc subpath
if path:
subpath = path+self.separator+k
else:
subpath = k
dd[k]=resolvedict(d,subpath,depth) #resolve same depth
elif isinstance(subd[k],basestring):
dd[k]=subd[k]
elif isinstance(subd[k],type(None)):
dd[k]=None
return dd
# end of resolvedict
# main part of getdict
if self.d:
return self.d
d={}
for ch in self.children(parentid):
if getattr(ch,self.valuef):
# key = value
d[getattr(ch,self.pathf)]=getattr(ch,self.valuef)
else:
# no value => subtree
d[getattr(ch,self.pathf)]=self.getdict(getattr(ch,self.idf))
if d:
if parentid is None:
# resolve dict
#print "dict before resolve",json.dumps(d,indent=4)
dd = resolvedict(d)
#print "dict after resolve",json.dumps(dd,indent=4)
self.d = dd
return dd
else:
return d
else:
return ''
def dump(self,parent=None,prefix=''):
#print "tree dump parent: {} (id: '{}', parent: '{}', prefix: '{}')".format(parent,self.idf,self.pf,prefix)
# find this node
print "dump old"
for nid,n in self.nodes.items():
#np = getattr(n,self.pf)
#print "try node {}, parent: {}".format(n,np)
if getattr(n,self.pf) == parent:
print prefix+getattr(n,self.pathf)+'='+getattr(n,self.valuef)
self.dump(nid,prefix+'. ')
def nodebyparent(self,parent_id=None):
for nid,n in self.nodes.items():
if getattr(n,self.pf) == parent_id:
return n
return None
def rootnode(self):
return self.nodebyparent()
def children(self,pid):
for nid,n in self.nodes.items():
if getattr(n,self.pf) == pid:
yield n
def add(self,node):
# check if node has required fields
if not hasattr(node,self.idf):
print "ERROR. must have id field '{}'".format(self.idf)
if not hasattr(node,self.pf):
print "ERROR. must have parent field '{}'".format(self.pf)
if not hasattr(node,self.pathf):
print "ERROR. must have path field '{}'".format(self.pathf)
self.nodes[getattr(node,self.idf)]=node
self.d = None
def getkey(self,path,subdict=None,pathval=None):
# print "TREE getkey '{}'".format(path)
if subdict is None:
d = self.getdict()
else:
d = subdict
# print "FROM dict:",d
if not path:
# return full subdict
return d
if pathval is None:
pathval={}
patha = path.split(self.separator)
rempath = list(patha)
# look for patha[0]
for k in d:
if k in patha:
pathval[k]=d[k]
if len(patha)==1:
# last item
if path in pathval:
return pathval[path]
return None
else:
try:
print "recursive getkey"
keyval = self.getkey(':'.join(patha[1:]),pathval[patha[0]],pathval)
except KeyError:
return None
print "TREE RETURN:",keyval
return keyval
def getkeydb(self,path,incdepth=5):
pathval={}
print "## getkey '{}'".format(path)
patha = path.split(self.separator)
print "patha:",patha
itemname=patha[-1]
print "item: {}".format(itemname)
d=self.mkdict()
print "dict: {}".format(d)
pnodeid=None
for p in patha:
print "will look for path {}".format(p)
for n in self.children(pnodeid):
npath = getattr(n,self.pathf)
print "({}) check npath {}".format(p,npath)
if npath in patha:
print "found npath {}".format(npath)
pathval[npath]=n
if npath==p:
print "Will enter {}".format(p)
pnodeid = getattr(n,self.idf)
print pathval
if itemname in pathval:
return getattr(pathval[itemname],self.valuef)
return None