/
TreeTag.py
748 lines (639 loc) · 22.2 KB
/
TreeTag.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Rendering object hierarchies as Trees
"""
from binascii import a2b_base64
from binascii import b2a_base64
import json
import zlib
import six
from DocumentTemplate.DT_Util import add_with_prefix
from DocumentTemplate.DT_Util import Eval
from DocumentTemplate.DT_Util import InstanceDict
from DocumentTemplate.DT_Util import name_param
from DocumentTemplate.DT_Util import parse_params
from DocumentTemplate.DT_Util import ParseError
from DocumentTemplate.DT_Util import render_blocks
from DocumentTemplate.DT_Util import simple_name
from DocumentTemplate.DT_Util import ValidationError
from DocumentTemplate.DT_String import String
if six.PY3:
unicode = str
tbl = b''.join([chr(i).encode('latin-1') for i in range(256)])
else:
tbl = b''.join([chr(i) for i in range(256)])
tplus = tbl[:ord('+')] + b'-' + tbl[ord('+') + 1:]
tminus = tbl[:ord('-')] + b'+' + tbl[ord('-') + 1:]
class Tree(object):
name = 'tree'
blockContinuations = ()
expand = None
def __init__(self, blocks):
tname, args, section = blocks[0]
args = parse_params(args,
name=None,
expr=None,
nowrap=1,
expand=None,
leaves=None,
header=None,
footer=None,
branches=None,
branches_expr=None,
sort=None,
reverse=1,
skip_unauthorized=1,
id=None,
single=1,
url=None,
# opened_decoration=None,
# closed_decoration=None,
# childless_decoration=None,
assume_children=1,
urlparam=None, prefix=None)
if '' in args or 'name' in args or 'expr' in args:
name, expr = name_param(args, 'tree', 1)
if expr is not None:
args['expr'] = expr
elif '' in args:
args['name'] = name
else:
name = 'a tree tag'
if 'branches_expr' in args:
if 'branches' in args:
raise ParseError(
'branches and and branches_expr given', 'tree')
args['branches_expr'] = Eval(args['branches_expr']).eval
elif 'branches' not in args:
args['branches'] = 'tpValues'
if 'id' not in args:
args['id'] = 'tpId'
if 'url' not in args:
args['url'] = 'tpURL'
if 'childless_decoration' not in args:
args['childless_decoration'] = ''
prefix = args.get('prefix')
if prefix and not simple_name(prefix):
raise ParseError(
'prefix is not a simple name', 'tree')
self.__name__ = name
self.section = section.blocks
self.args = args
def render(self, md):
args = self.args
if 'name' in args:
v = md[args['name']]
elif 'expr' in args:
v = args['expr'].eval(md)
else:
v = md.this
return tpRender(v, md, self.section, self.args)
__call__ = render
String.commands['tree'] = Tree
pyid = id # Copy builtin
simple_types = {str: 1, unicode: 1, int: 1, float: 1,
tuple: 1, list: 1, dict: 1}
def try_call_attr(ob, attrname, simple_types=simple_types):
attr = getattr(ob, attrname)
if type(attr) in simple_types:
return attr
try:
return attr()
except TypeError:
return attr
def tpRender(self, md, section, args,
try_call_attr=try_call_attr):
"""Render data organized as a tree.
We keep track of open nodes using a cookie. The cookie stored the
tree state. State should be a tree represented like:
[] # all closed
['eagle'], # eagle is open
['eagle'], ['jeep', [1983, 1985]] # eagle, jeep, 1983 jeep and 1985 jeep
where the items are object ids. The state will be pickled to a
compressed and base64ed string that gets unencoded, uncompressed,
and unpickled on the other side.
Note that ids used in state need not be connected to urls, since
state manipulation is internal to rendering logic.
Note that to make unpickling safe, we use the MiniPickle module,
that only creates safe objects
"""
data = []
idattr = args['id']
if hasattr(self, idattr):
id = try_call_attr(self, idattr)
elif hasattr(self, '_p_oid'):
id = oid(self)
else:
id = pyid(self)
try:
# see if we are being run as a sub-document
root = md['tree-root-url']
url = md['tree-item-url']
state = md['tree-state']
diff = md['tree-diff']
substate = md['-tree-substate-']
colspan = md['tree-colspan']
level = md['tree-level']
except KeyError:
# OK, we are a top-level invocation
level = -1
if 'collapse_all' in md:
state = [id, []],
elif 'expand_all' in md:
if 'branches' in args:
def get_items(node, branches=args['branches'], md=md):
get = md.guarded_getattr
if get is None:
get = getattr
items = get(node, branches)
return items()
elif 'branches_expr' in args:
def get_items(node, branches_expr=args['branches_expr'],
md=md):
md._push(InstanceDict(node, md))
items = branches_expr(md)
md._pop()
return items
state = [id, tpValuesIds(self, get_items, args)],
else:
if 'tree-s' in md:
state = md['tree-s']
state = decode_seq(state)
try:
if state[0][0] != id:
state = [id, []],
except IndexError:
state = [id, []],
else:
state = [id, []],
if 'tree-e' in md:
diff = decode_seq(md['tree-e'])
apply_diff(state, diff, 1)
if 'tree-c' in md:
diff = decode_seq(md['tree-c'])
apply_diff(state, diff, 0)
colspan = tpStateLevel(state)
substate = state
diff = []
url = ''
root = md['URL']
l = root.rfind('/')
if l >= 0:
root = root[l + 1:]
treeData = {'tree-root-url': root,
'tree-colspan': colspan,
'tree-state': state}
prefix = args.get('prefix')
if prefix:
for k, v in treeData.items():
treeData[prefix + k[4:].replace('-', '_')] = v
md._push(InstanceDict(self, md))
md._push(treeData)
try:
tpRenderTABLE(
self, id, root, url, state, substate, diff, data, colspan,
section, md, treeData, level, args)
finally:
md._pop(2)
if state is substate and not ('single' in args and args['single']):
state = state or ([id],)
state = encode_seq(state)
md['RESPONSE'].setCookie('tree-s', state)
return ''.join(data)
def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
colspan, section, md, treeData, level=0, args=None,
try_call_attr=try_call_attr,
):
"Render a tree as a table"
exp = 0
if level >= 0:
urlattr = args['url']
if urlattr and hasattr(self, urlattr):
tpUrl = try_call_attr(self, urlattr)
url = (url and ('%s/%s' % (url, tpUrl))) or tpUrl
root_url = root_url or tpUrl
ptreeData = add_with_prefix(treeData, 'tree', args.get('prefix'))
ptreeData['tree-item-url'] = url
ptreeData['tree-level'] = level
ptreeData['tree-item-expanded'] = 0
idattr = args['id']
output = data.append
items = None
if ('assume_children' in args and args['assume_children'] and
substate is not state):
# We should not compute children unless we have to.
# See if we've been asked to expand our children.
for i in range(len(substate)):
sub = substate[i]
if sub[0] == id:
exp = i + 1
break
if not exp:
items = 1
get = md.guarded_getattr
if get is None:
get = getattr
if items is None:
if 'branches' in args and hasattr(self, args['branches']):
items = get(self, args['branches'])
items = items()
elif 'branches_expr' in args:
items = args['branches_expr'](md)
if not items and 'leaves' in args:
items = 1
if items and items != 1:
getitem = getattr(md, 'guarded_getitem', None)
if getitem is not None:
unauth = []
for index in range(len(items)):
try:
getitem(items, index)
except ValidationError:
unauth.append(index)
if unauth:
if 'skip_unauthorized' in args and args['skip_unauthorized']:
items = list(items)
unauth.reverse()
for index in unauth:
del items[index]
else:
raise ValidationError(unauth)
if 'sort' in args:
# Faster/less mem in-place sort
if isinstance(items, tuple):
items = list(items)
sort = args['sort']
size = range(len(items))
for i in size:
v = items[i]
k = getattr(v, sort)
try:
k = k()
except Exception:
pass
items[i] = (k, v)
items.sort()
for i in size:
items[i] = items[i][1]
if 'reverse' in args:
items = list(items) # Copy the list
items.reverse()
diff.append(id)
_td_colspan = '<td colspan="%s" style="white-space: nowrap"></td>'
_td_single = '<td width="16" style="white-space: nowrap"></td>'
sub = None
if substate is state:
output('<table cellspacing="0">\n')
sub = substate[0]
exp = items
else:
# Add prefix
output('<tr>\n')
# Add +/- icon
if items:
if level:
if level > 3:
output(_td_colspan % (level - 1))
elif level > 1:
output(_td_single * (level - 1))
output(_td_single)
output('\n')
output('<td width="16" valign="top" style="white-space: nowrap">')
for i in range(len(substate)):
sub = substate[i]
if sub[0] == id:
exp = i + 1
break
s = encode_str(compress(json.dumps(diff))) # bytes in ASCII enc.
script = md['BASEPATH1']
# Propagate extra args through tree.
if 'urlparam' in args:
param = args['urlparam']
param = "%s&" % param
else:
param = ""
if exp:
ptreeData['tree-item-expanded'] = 1
output('<a name="%s" href="%s?%stree-c=%s#%s">'
'<img src="%s/p_/mi" alt="-" border="0" /></a>' %
(id, root_url, param, s, id, script))
else:
output('<a name="%s" href="%s?%stree-e=%s#%s">'
'<img src="%s/p_/pl" alt="+" border="0" /></a>' %
(id, root_url, param, s, id, script))
output('</td>\n')
else:
if level > 2:
output(_td_colspan % level)
elif level > 0:
output(_td_single * level)
output(_td_single)
output('\n')
# add item text
dataspan = colspan - level
output('<td%s%s valign="top" align="left">' %
((dataspan > 1 and (' colspan="%s"' % dataspan) or ''),
('nowrap' in args and
args['nowrap'] and ' style="white-space: nowrap"' or ''))
)
output(render_blocks(section, md))
output('</td>\n</tr>\n')
if exp:
level = level + 1
dataspan = colspan - level
if level > 2:
h = _td_colspan % level
elif level > 0:
h = _td_single * level
else:
h = ''
if 'header' in args:
doc = args['header']
if doc in md:
doc = md.getitem(doc, 0)
else:
doc = None
if doc is not None:
output(doc(
None, md,
standard_html_header=(
'<tr>%s'
'<td width="16" style="white-space: nowrap"></td>'
'<td%s valign="top">'
% (h,
(dataspan > 1 and (' colspan="%s"' % dataspan) or
''))),
standard_html_footer='</td></tr>',
))
if items == 1:
# leaves
if 'leaves' in args:
doc = args['leaves']
if doc in md:
doc = md.getitem(doc, 0)
else:
doc = None
if doc is not None:
treeData['-tree-substate-'] = sub
ptreeData['tree-level'] = level
md._push(treeData)
try:
output(doc(
None, md,
standard_html_header=(
'<tr>%s<td '
'width="16" style="white-space: nowrap"></td>'
'<td%s valign="top">'
% (h,
(dataspan > 1 and
(' colspan="%s"' % dataspan) or ''))),
standard_html_footer='</td></tr>',
))
finally:
md._pop(1)
elif 'expand' in args:
doc = args['expand']
if doc in md:
doc = md.getitem(doc, 0)
else:
doc = None
if doc is not None:
treeData['-tree-substate-'] = sub
ptreeData['tree-level'] = level
md._push(treeData)
try:
output(doc(
None, md,
standard_html_header=(
'<tr>%s<td '
'width="16" style="white-space: nowrap"></td>'
'<td%s valign="top">'
% (h,
(dataspan > 1 and
(' colspan="%s"' % dataspan) or ''))),
standard_html_footer='</td></tr>',
))
finally:
md._pop(1)
else:
__traceback_info__ = sub, args, state, substate
ids = {}
for item in items:
if hasattr(item, idattr):
id = try_call_attr(item, idattr)
elif hasattr(item, '_p_oid'):
id = oid(item)
else:
id = pyid(item)
if len(sub) == 1:
sub.append([])
substate = sub[1]
ids[id] = 1
md._push(InstanceDict(item, md))
try:
data = tpRenderTABLE(
item, id, root_url, url, state, substate, diff, data,
colspan, section, md, treeData, level, args)
finally:
md._pop()
if not sub[1]:
del sub[1]
ids = ids.__contains__
for i in range(len(substate) - 1, -1):
if not ids(substate[i][0]):
del substate[i]
if 'footer' in args:
doc = args['footer']
if doc in md:
doc = md.getitem(doc, 0)
else:
doc = None
if doc is not None:
output(doc(
None, md,
standard_html_header=(
'<tr>%s<td '
'width="16" style="white-space: nowrap"></td>'
'<td%s valign="top">'
% (h,
(dataspan > 1 and (' colspan="%s"' % dataspan) or
''))),
standard_html_footer='</td></tr>',
))
del diff[-1]
if not diff:
output('</table>\n')
return data
def apply_diff(state, diff, expand):
if not diff:
return
s = [None, state]
diff.reverse()
__traceback_info__ = s, diff
while diff:
id = diff[-1]
del diff[-1]
if len(s) == 1:
s.append([])
s = s[1]
if isinstance(s, tuple):
s = list(s)
loc = -1
for i in range(len(s)):
if s[i][0] == id:
loc = i
break
if loc >= 0:
if not diff and not expand:
del s[loc]
else:
s = s[loc]
elif diff or expand:
s.append([id, []])
s = s[-1][1]
while diff:
id = diff[-1]
del diff[-1]
if diff or expand:
s.append([id, []])
s = s[-1][1]
def encode_seq(state):
"Convert a sequence to an encoded string"
state = compress(json.dumps(state))
l = len(state)
if l > 57:
states = []
for i in range(0, l, 57):
states.append(b2a_base64(state[i:i + 57])[:-1])
state = b''.join(states)
else:
state = b2a_base64(state)[:-1]
l = state.find(b'=')
if l >= 0:
state = state[:l]
state = state.translate(tplus)
if six.PY3:
state = state.decode('ascii')
return state
def encode_str(state):
"""Convert bytes to a base64-encoded bytes.
'state' should be bytes
"""
if not isinstance(state, bytes):
raise ValueError("state should be bytes")
l = len(state)
if l > 57:
states = []
for i in range(0, l, 57):
states.append(b2a_base64(state[i:i + 57])[:-1])
state = b''.join(states)
else:
state = b2a_base64(state)[:-1]
# state is still bytes, but all in 'ascii' encoding.
l = state.find(b'=')
if l >= 0:
state = state[:l]
state = state.translate(tplus)
return state
def decode_seq(state):
"Convert an encoded string to a sequence"
state = state.translate(tminus)
l = len(state)
if l > 76:
states = []
j = 0
for i in range(l // 76):
k = j + 76
states.append(a2b_base64(state[j:k]))
j = k
if j < l:
state = state[j:]
l = len(state)
k = l % 4
if k:
state = state + '=' * (4 - k)
states.append(a2b_base64(state))
state = ''.join(states)
else:
l = len(state)
k = l % 4
if k:
state = state + '=' * (4 - k)
state = a2b_base64(state)
state = decompress(state)
try:
return json.loads(state)
except Exception:
return []
def compress(input):
"""Compress text to bytes.
'input' should be text.
"""
if not isinstance(input, six.string_types):
raise ValueError("Input should be text")
if not isinstance(input, bytes):
input = input.encode('utf-8')
return zlib.compress(input)
def decompress(input):
"""Decompress bytes to text.
'input' should be bytes.
"""
if not isinstance(input, bytes):
raise ValueError("Input should be bytes")
return zlib.decompress(input).decode('utf-8')
def tpStateLevel(state, level=0):
for sub in state:
if len(sub) == 2:
level = max(level, 1 + tpStateLevel(sub[1]))
else:
level = max(level, 1)
return level
def tpValuesIds(self, get_items, args,
try_call_attr=try_call_attr,
):
# get_item(node) is a function that returns the subitems of node
# This should build the ids of subitems which are
# expandable (non-empty). Leaves should never be
# in the state - it will screw the colspan counting.
r = []
idattr = args['id']
try:
try:
items = get_items(self)
except AttributeError:
items = ()
for item in items:
try:
if get_items(item):
if hasattr(item, idattr):
id = try_call_attr(item, idattr)
elif hasattr(item, '_p_oid'):
id = oid(item)
else:
id = pyid(item)
e = tpValuesIds(item, get_items, args)
if e:
id = [id, e]
else:
id = [id]
r.append(id)
except Exception:
pass
except Exception:
pass
return r
def oid(self):
value = b2a_base64(self._p_oid)[:-1]
if six.PY3:
value = value.decode('ascii')
return value