Skip to content

Commit

Permalink
Merge pull request #156 from caesar0301/dev
Browse files Browse the repository at this point in the history
Sync latest patches from dev
  • Loading branch information
leonardbinet authored Mar 2, 2020
2 parents 7a21c50 + b4d8e7c commit 017e891
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 38 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sys
sys.path.insert(0, os.path.abspath('../..'))

from treelib import __version__
from setup import __version__

# -- Project information -----------------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from setuptools import setup

from treelib import __version__
__version__ = '1.6.2'


setup(
name="treelib",
Expand Down
1 change: 0 additions & 1 deletion treelib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
>>> from __future__ import unicode_literals
"""
__version__ = '1.6.1'

from .tree import Tree
from .node import Node
43 changes: 8 additions & 35 deletions treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

import codecs
import json
import sys
import uuid
from copy import deepcopy
from future.utils import python_2_unicode_compatible, iteritems

try:
from StringIO import StringIO
Expand All @@ -51,26 +51,6 @@
__author__ = 'chenxm'


def python_2_unicode_compatible(klass):
"""
(slightly modified from: http://django.readthedocs.org/en/latest/_modules/django/utils/encoding.html)
A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.
To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.
"""
if sys.version_info[0] == 2:
if '__str__' not in klass.__dict__:
raise ValueError("@python_2_unicode_compatible cannot be applied "
"to %s because it doesn't define __str__()." %
klass.__name__)
klass.__unicode__ = klass.__str__
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
return klass


@python_2_unicode_compatible
class Tree(object):
"""Tree objects are made of Node(s) stored in _nodes dictionary."""
Expand Down Expand Up @@ -102,7 +82,7 @@ def __init__(self, tree=None, deep=False, node_class=None, identifier=None):

if tree is not None:
self.root = tree.root
for nid, node in tree.nodes.items():
for nid, node in iteritems(tree.nodes):
new_node = deepcopy(node) if deep else node
self._nodes[nid] = new_node
if tree.identifier != self._identifier:
Expand Down Expand Up @@ -154,10 +134,6 @@ def __len__(self):
"""Return len(_nodes)"""
return len(self._nodes)

def __setitem__(self, key, item):
"""Set _nodes[key]"""
self._nodes.update({key: item})

def __str__(self):
self._reader = ""

Expand Down Expand Up @@ -240,20 +216,17 @@ def filter_(node):
return self.__get_iter(nid, level, filter_, key, reverse, dt, [])

def __get_iter(self, nid, level, filter_, key, reverse, dt, is_last):
dt_vline, dt_line_box, dt_line_cor = dt

nid = self.root if (nid is None) else nid
if not self.contains(nid):
raise NodeIDAbsentError("Node '%s' is not in the tree" % nid)
dt_vertical_line, dt_line_box, dt_line_corner = dt

nid = self.root if nid is None else nid
node = self[nid]

if level == self.ROOT:
yield "", node
else:
leading = ''.join(map(lambda x: dt_vline + ' ' * 3
leading = ''.join(map(lambda x: dt_vertical_line + ' ' * 3
if not x else ' ' * 4, is_last[0:-1]))
lasting = dt_line_cor if is_last[-1] else dt_line_box
lasting = dt_line_corner if is_last[-1] else dt_line_box
yield leading + lasting, node

if filter_(node) and node.expanded:
Expand Down Expand Up @@ -662,7 +635,7 @@ def paste(self, nid, new_tree, deep=False):
if set_joint:
raise ValueError('Duplicated nodes %s exists.' % list(map(text, set_joint)))

for cid, node in new_tree.nodes.items():
for cid, node in iteritems(new_tree.nodes):
if deep:
node = deepcopy(new_tree[node])
self._nodes.update({cid: node})
Expand Down Expand Up @@ -930,7 +903,7 @@ def update_node(self, nid, **attrs):
:return: None
"""
cn = self[nid]
for attr, val in attrs.items():
for attr, val in iteritems(attrs):
if attr == 'identifier':
# Updating node id meets following contraints:
# * Update node identifier property
Expand Down

0 comments on commit 017e891

Please sign in to comment.