|
36 | 36 |
|
37 | 37 | import codecs
|
38 | 38 | import json
|
39 |
| -import sys |
40 | 39 | import uuid
|
41 | 40 | from copy import deepcopy
|
| 41 | +from future.utils import python_2_unicode_compatible, iteritems |
42 | 42 |
|
43 | 43 | try:
|
44 | 44 | from StringIO import StringIO
|
|
51 | 51 | __author__ = 'chenxm'
|
52 | 52 |
|
53 | 53 |
|
54 |
| -def python_2_unicode_compatible(klass): |
55 |
| - """ |
56 |
| - (slightly modified from: http://django.readthedocs.org/en/latest/_modules/django/utils/encoding.html) |
57 |
| -
|
58 |
| - A decorator that defines __unicode__ and __str__ methods under Python 2. |
59 |
| - Under Python 3 it does nothing. |
60 |
| -
|
61 |
| - To support Python 2 and 3 with a single code base, define a __str__ method |
62 |
| - returning text and apply this decorator to the class. |
63 |
| - """ |
64 |
| - if sys.version_info[0] == 2: |
65 |
| - if '__str__' not in klass.__dict__: |
66 |
| - raise ValueError("@python_2_unicode_compatible cannot be applied " |
67 |
| - "to %s because it doesn't define __str__()." % |
68 |
| - klass.__name__) |
69 |
| - klass.__unicode__ = klass.__str__ |
70 |
| - klass.__str__ = lambda self: self.__unicode__().encode('utf-8') |
71 |
| - return klass |
72 |
| - |
73 |
| - |
74 | 54 | @python_2_unicode_compatible
|
75 | 55 | class Tree(object):
|
76 | 56 | """Tree objects are made of Node(s) stored in _nodes dictionary."""
|
@@ -102,7 +82,7 @@ def __init__(self, tree=None, deep=False, node_class=None, identifier=None):
|
102 | 82 |
|
103 | 83 | if tree is not None:
|
104 | 84 | self.root = tree.root
|
105 |
| - for nid, node in tree.nodes.items(): |
| 85 | + for nid, node in iteritems(tree.nodes): |
106 | 86 | new_node = deepcopy(node) if deep else node
|
107 | 87 | self._nodes[nid] = new_node
|
108 | 88 | if tree.identifier != self._identifier:
|
@@ -154,10 +134,6 @@ def __len__(self):
|
154 | 134 | """Return len(_nodes)"""
|
155 | 135 | return len(self._nodes)
|
156 | 136 |
|
157 |
| - def __setitem__(self, key, item): |
158 |
| - """Set _nodes[key]""" |
159 |
| - self._nodes.update({key: item}) |
160 |
| - |
161 | 137 | def __str__(self):
|
162 | 138 | self._reader = ""
|
163 | 139 |
|
@@ -240,20 +216,17 @@ def filter_(node):
|
240 | 216 | return self.__get_iter(nid, level, filter_, key, reverse, dt, [])
|
241 | 217 |
|
242 | 218 | def __get_iter(self, nid, level, filter_, key, reverse, dt, is_last):
|
243 |
| - dt_vline, dt_line_box, dt_line_cor = dt |
244 |
| - |
245 |
| - nid = self.root if (nid is None) else nid |
246 |
| - if not self.contains(nid): |
247 |
| - raise NodeIDAbsentError("Node '%s' is not in the tree" % nid) |
| 219 | + dt_vertical_line, dt_line_box, dt_line_corner = dt |
248 | 220 |
|
| 221 | + nid = self.root if nid is None else nid |
249 | 222 | node = self[nid]
|
250 | 223 |
|
251 | 224 | if level == self.ROOT:
|
252 | 225 | yield "", node
|
253 | 226 | else:
|
254 |
| - leading = ''.join(map(lambda x: dt_vline + ' ' * 3 |
| 227 | + leading = ''.join(map(lambda x: dt_vertical_line + ' ' * 3 |
255 | 228 | if not x else ' ' * 4, is_last[0:-1]))
|
256 |
| - lasting = dt_line_cor if is_last[-1] else dt_line_box |
| 229 | + lasting = dt_line_corner if is_last[-1] else dt_line_box |
257 | 230 | yield leading + lasting, node
|
258 | 231 |
|
259 | 232 | if filter_(node) and node.expanded:
|
@@ -662,7 +635,7 @@ def paste(self, nid, new_tree, deep=False):
|
662 | 635 | if set_joint:
|
663 | 636 | raise ValueError('Duplicated nodes %s exists.' % list(map(text, set_joint)))
|
664 | 637 |
|
665 |
| - for cid, node in new_tree.nodes.items(): |
| 638 | + for cid, node in iteritems(new_tree.nodes): |
666 | 639 | if deep:
|
667 | 640 | node = deepcopy(new_tree[node])
|
668 | 641 | self._nodes.update({cid: node})
|
@@ -930,7 +903,7 @@ def update_node(self, nid, **attrs):
|
930 | 903 | :return: None
|
931 | 904 | """
|
932 | 905 | cn = self[nid]
|
933 |
| - for attr, val in attrs.items(): |
| 906 | + for attr, val in iteritems(attrs): |
934 | 907 | if attr == 'identifier':
|
935 | 908 | # Updating node id meets following contraints:
|
936 | 909 | # * Update node identifier property
|
|
0 commit comments