-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.py
573 lines (469 loc) · 15.7 KB
/
tree.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import json
import copy
import string
#
# key and dirname can have '@'
# key name cannot have ':'
# key value can have : and/or @
#
#
class TreeKey(object):
name = None
fullname = None # fullname is path+sepchar+name
path = None
value = None # None for subdir, '' or value for string
tree = None
# return name as int if possible, or as string
def intname(self):
try:
return int(self.name)
except ValueError as e:
return self.name
# generator. returns all children of this treekey
def children(self):
return self.tree.treekeys(self.fullname)
def nchildren(self):
return self.tree.nchildren(self.fullname)
def __repr__(self):
return "'"+self.name+"'"
def link(self):
try:
if self.name.startswith('@include'):
return self.name.split(' ',1)[1]
else:
return None
except IndexError:
return None
def isdir(self):
if self.value is None or isinstance(self.value,dict):
# bad approach! dir can start with @, e.g. @access
#if self.name.startswith('@'):
# special @ directive, not a subdir
# return False
# no value and not directive, so this is DIR
return True
else:
# has value => this is not a DIR
return False
class Tree(object):
# d = dict()
sepchar=':' # if override, this will break smartsplit()
def __init__(self):
self.d = dict()
#
# remove @anything keys from any dict
#
def noat(self,curd=None):
if curd is None:
curd = self.d
newd = {}
if isinstance(curd,str):
return curd
if isinstance(curd,dict):
for k in curd:
if not k.startswith('@'):
newd[k]=curd[k]
else:
pass
return newd
@staticmethod
#
# keyname must have allowed : for includes
# to allow to '@include ...' keys
# because two '@include' keys share same name and overwrite each other
#
# space allowed in string key (not dirname) for '@include[space]path'
#
# * is required for @include conf:web:*
# . is very needed for hostnames as dirnames, e.g. domain.com
#
# ' ', '*' and ':' IS NOT RECOMMENDED
#
def keyname_valid(key,value=None):
#key_re="^[@a-zA-Z\-\_]*$"
valid = string.ascii_letters + string.digits + '-_@:.'
if isinstance(value,str) and key.startswith('@'):
# this is dir
valid += ' *'
# check if this is valid directive
if key.startswith('@include '):
if len(key) == len('@include '):
return False
else:
# starts with @ but not known directive
return False
#print "valid: '{}' key: '{}' val: '{}'".format(valid,key,value)
return all(x in valid for x in key)
#
# returns dict with processed arrays
#
# uses self.d for includes
#
def doinclude(self, curd=None, depth=10):
if curd is None:
curd = self.d
newd = {}
if isinstance(curd,str):
return curd
for k in curd:
if isinstance(curd[k],dict):
# print "Dive into", k
newd[k]=self.doinclude(curd[k],depth-1 if depth else 0)
else:
# string
if k.startswith('@include') and depth:
#print "process include ",k
(inc, ipath) = k.split(' ')
# ipath = curd[k]
ipatha = ipath.split(self.sepchar)
# print "!! include",ipatha
if ipatha[-1]=='*':
# print "asterisk include"
rawd = self.godeep(ipatha[:-1]).copy()
# print "rawd:",rawd
if isinstance(rawd,dict):
rawd = self.doinclude(rawd,depth-1 if depth else 0)
# must be dict for asterisk include!
# newd = rawd.update(newd)
for k in rawd:
if not k in newd:
newd[k]=rawd[k]
else:
try:
#print "usual include",ipatha
rawd = self.godeep(ipatha)
#print "godeep ok"
rawd = self.doinclude(rawd,depth-1 if depth else 0)
#print "doinclude ok"
# print "rawd:",rawd
newd[ipatha[-1]] = rawd
except KeyError:
# print "cannot include",ipatha, k
pass
# simple string, not include
else:
newd[k]=curd[k]
return newd
#
# return subtree with path
#
def key(self, path, noat=True,include=True):
#!!!
if include:
d = self.doinclude(copy.deepcopy(self.d))
else:
d = copy.deepcopy(self.d)
d = self.godeep(path,d)
if noat:
d = self.noat(d)
else:
pass
return d
#
# key and path is either ':' separated strings or lists
# looks for path+key
# if not found, removes last element from path and repeats
#
# return None if not found at all
#
def fallback(self,path,key):
#print "fallback path:{}, key:'{}'".format(path,key)
if isinstance(path,str):
patha = path.split(self.sepchar)
patha = filter(None,patha)
else:
patha = list(path)
if isinstance(key, str):
keya = key.split(self.sepchar)
else:
keya = list(key)
if len(patha) == 0:
try:
val = self.key(key)
return val
except KeyError as e:
print("key error", str(e))
return None
else:
while True:
trypath = patha + keya
try:
val = self.key(trypath)
return val
except KeyError:
if len(patha):
patha.pop()
else:
return None
print("unreachable code?")
return None
#
# returns part of d (=self.d) by path
#
# path is either ':'-string, or list
#
# can raise KeyError
#
def godeep(self,path=None,d=None):
if d is None:
d=self.d
if isinstance(path,str):
#print "str, split"
patha = path.split(self.sepchar)
#print "patha: ",patha
elif isinstance(path,list):
patha=path
elif isinstance(path,type(None)):
patha=[]
patha = filter(None,patha)
dd=d
for r in patha:
if isinstance(dd,str):
# cannot dive into string
raise KeyError
#print "dd: ",dd
#print "dive",r
dd=dd[r]
return dd
#if len(patha) == 0:
# return d
#print "godeep path '{}' '{}'".format(path,patha[0])
#return self.godeep(patha[1:],d[patha[0]])
#
# add name to path with value
#
# if name is None and value dict, add each key from dict
#
def add(self, path, name, value=None):
# print("tree add path={} name={} value={}".format(path,name,value))
#print "d:",self.d
#
# check if keyname is valid.
# Note: None is valid (initialization), if value is dict
#
if not (name is None and isinstance(value,dict) or Tree.keyname_valid(name,value)):
print("not valid name '{}'".format(name))
raise ValueError
return None
if path is None:
path=''
if value is None:
value=dict()
else:
value = copy.deepcopy(value)
# print("add '{}' to path '{}'".format(name,path))
dd = self.godeep(path,self.d)
#print "got dd:",dd
if name is None and isinstance(value, dict):
for k in value:
dd[k]=value[k]
else:
dd[name]=value
#print "d (after):",self.d
#
# smartsplit
#
# better then split, because aware of keys which starts with '@' and contains ':'
#
@staticmethod
def smartsplit(s):
if '@' in s:
# print "tricky: ",s
if s.startswith('@'):
# no path, starts with key
path = [s]
else:
[p,k] = s.split(':@')
print("p:",p,"k:",k)
path = p.split(':')
path.append('@'+k)
else:
# print "simple: ",s
path = s.split(':')
return path
#
# delete
#
# !!! TODO check mkdir from UI
# check delete from UI, keys/dir, special, usual
#
def delete(self,path,d=None):
if d is None:
d=self.d
if isinstance(path,str) or isinstance(path,unicode):
#patha = Tree.smartsplit(path)
patha = path.split(':')
elif isinstance(path,list):
patha=path
patha = filter(None,patha)
if len(patha)==0:
# no path given
return
dd=d
for r in patha[:-1]:
if isinstance(dd,str):
# cannot dive into string
raise KeyError
dd=dd[r]
if patha[-1] == '*':
# delete all keys from dd
for k in list(dd):
del dd[k]
else:
# delete one key
del dd[patha[-1]]
def getjson(self):
return json.dumps(self.d)
def loadjson(self,jsonkeys):
d = json.loads(jsonkeys)
self.add('',name=None,value=d)
# tree.getkey
#
# wrapper. fallback if fallback
# key if no fallback
#
def getkey(self,path,fallback=True):
fullpath = path.split(':')
keyname=fullpath.pop()
if fallback:
x = self.fallback(fullpath,keyname)
return x
else:
# no fallback, just key
k = self.key(path)
return k
#
# prints self.d with pretty json
#
def dump(self,path=None):
if path is None:
d=self.d
else:
d=self.key(path)
print(json.dumps(d, indent=4, separators=(',', ': ')))
# nchildren
# return number of children. always 0 for non-dirs
def nchildren(self,path=None):
d = self.godeep(path)
if type(d) != dict:
return 0
n=0
for k in d.keys():
n+=1
return n
#
# gives resolved path to
#
def resolve(self,origpath,dd=None):
if dd is None:
dd = self.d
newpath=list()
if isinstance(origpath,str) or isinstance(origpath,unicode):
#print "str, split"
origpatha = origpath.split(self.sepchar)
for pelem in origpatha:
# print "go deep {}".format(pelem)
includes = dict()
for dde in dd:
if not dde.startswith('@include '):
continue
incpath = dde.split(' ')[1]
incname = incpath.split(':')[-1]
includes[incname]=incpath
if pelem in dd:
dd = dd[pelem]
newpath.append(pelem)
else:
# print "no key {}! maybe include?".format(pelem)
if pelem in includes:
newpath = includes[pelem].split(':')
dd = self.godeep(newpath)
else:
# print "NO SUCH KEY"
return None
return ':'.join(newpath)
# treekeys
# iterate over keys
def treekeys(self,path=None):
d = self.godeep(path)
for k,v in d.items():
#print "WALK k:{} v:{}".format(k,v)
tk = TreeKey()
tk.name = k
tk.tree = self
if path:
tk.path=path
tk.fullname=path+self.sepchar+k
else:
tk.path=''
tk.fullname=k
if isinstance(v,str) or isinstance(v,unicode):
tk.value = v
yield tk
def rootkey(self):
tk = TreeKey()
tk.name = ''
tk.tree = self
tk.path=''
tk.fullname=''
return tk
if __name__ == '__main__':
s = {'person1': {'name': 'Jack','age': 30}, 'person2': {'name': 'John'}}
t = Tree()
data = {
'title': 'asdf asdf asdf',
'num' : '123',
'servers': {
'farm1': {
'clusterA': {
'server1': {
'@include checks:ch3:*': ''
}
}
},
'@access': {
'user':'pass'
}
},
'checks': {
'ch1': {
'method': 'heartbeat'
},
'ch2': {
'method': 'OK'
},
'ch3': {
'@include checks:ch1': '',
'@include checks:ch2': ''
}
}
}
jkeys = '''{"@access": {"client": "c3t442xQWI"}, "conf": {"webserver": {"aaa": "bbb"}}, "lib": {"biglog": {"checkmethod": "streqd", "name": "{iname}:biglog", "sequence": {"1": {"path": "/var/log", "command": "DIR"}, "3": {"field": "size", "command": "SORT"}, "2": {"command": "FILTER", "argline": "type=='REG'"}, "5": "TOP 10", "4": "REV", "6": "FORMAT {path} {size}"}}, "tcpports": {"checkmethod": "streqd", "name": "{iname}:opentcp", "sequence": {"10": "CONNECTIONS", "30": "SORT field=port", "20": "FILTER status=='LISTEN' and proto=='tcp' and basename != 'smtpd'", "40": "FORMAT {proto}:{port} {basename}"}}}, "servers": {"server1": {"@include conf:webserver": "", "aaa": "bbb"}}}'''
farm2 = {
'farm2': {
'server1': {
'k': 'v'
}
}
}
#t.add('','aaa','bbb')
#t.add('',name=None,value=data)
t.loadjson(jkeys)
#t.add('servers',name=None,value=farm2)
#t.dump()
#print "SUB:SS:",t.godeep('sub:ss:sss1')
#print t.key('servers:farm1:clusterA:server1:key')
#print t.fallback('servers','@access')
#print "INCLUDES"
#t.delete('title')
#t.delete('servers:farm1')
#t.delete('conf:webserver')
t.add('servers:server1','@access',None)
def printtree(tk,prefix=""):
if tk.isdir():
print(prefix+"[+] {}".format(tk.fullname))
for tki in tk.children():
printtree(tki,prefix+" ")
else:
print(prefix+"E:",tk.name,"=",tk.value)
printtree(t.rootkey())