From 5663990ab0146b59d63f813758f9aee76ea9518c Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 14:02:01 -0500 Subject: [PATCH 01/16] fixes JSON exporter for SymLink Nodes --- anytree/exporter/dictexporter.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/anytree/exporter/dictexporter.py b/anytree/exporter/dictexporter.py index 21886d4..b2b43e1 100644 --- a/anytree/exporter/dictexporter.py +++ b/anytree/exporter/dictexporter.py @@ -89,4 +89,10 @@ def _iter_attr_values(self, node): for k, v in node.__dict__.items(): if k in ('_NodeMixin__children', '_NodeMixin__parent'): continue - yield k, v + elif k == "target": + for key, val in v.__dict__.items(): + if key in ('_NodeMixin__children', '_NodeMixin__parent'): + continue + yield key, val + else: + yield k, v From d6398c9c463e8ab89475a215cc5c89ad3d1a87b5 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 15:50:21 -0500 Subject: [PATCH 02/16] added test for symlink node --- tests/test_dictexporter.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_dictexporter.py b/tests/test_dictexporter.py index 49698fe..3eae64a 100644 --- a/tests/test_dictexporter.py +++ b/tests/test_dictexporter.py @@ -3,6 +3,7 @@ from anytree import AnyNode from anytree import Node from anytree import NodeMixin +from anytree import SymlinkNode from anytree.exporter import DictExporter @@ -132,3 +133,32 @@ def __init__(self, foo, parent=None): ]} ]} ) + +def test_dict_exporter_symlink_node(): + """Dict Exporter.""" + root = Node("root") + s0 = Node("sub0", parent=root) + s0b = Node("sub0B", parent=s0) + s0a = Node("sub0A", parent=s0) + s1 = Node("sub1", parent=root, foo="bar") + s1a = SymlinkNode(target=s0, parent=s1) + s1b = Node("sub1B", parent=s1) + s1c = Node("sub1C", parent=s1) + s1ca = Node("sub1Ca", parent=s1c) + + exporter = DictExporter() + eq_(exporter.export(root), + {'name': 'root', 'children': [ + {'name': 'sub0', 'children': [ + {'name': 'sub0B'}, + {'name': 'sub0A'} + ]}, + {'name': 'sub1', 'foo': 'bar', 'children': [ + {'name': 'sub0'}, + {'name': 'sub1B'}, + {'name': 'sub1C', 'children': [ + {'name': 'sub1Ca'} + ]} + ]} + ]} + ) \ No newline at end of file From 5c5fb1bc7c8772c3a48a01d76c65e885ec702e9f Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 15:51:16 -0500 Subject: [PATCH 03/16] spacing --- tests/test_dictexporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_dictexporter.py b/tests/test_dictexporter.py index 3eae64a..5e3152b 100644 --- a/tests/test_dictexporter.py +++ b/tests/test_dictexporter.py @@ -161,4 +161,4 @@ def test_dict_exporter_symlink_node(): ]} ]} ]} - ) \ No newline at end of file + ) From bed4f542625abc8a8da44783a9592caf4a8236d5 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 16:25:45 -0500 Subject: [PATCH 04/16] extra space --- tests/test_dictexporter.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_dictexporter.py b/tests/test_dictexporter.py index 5e3152b..0dfff58 100644 --- a/tests/test_dictexporter.py +++ b/tests/test_dictexporter.py @@ -134,6 +134,7 @@ def __init__(self, foo, parent=None): ]} ) + def test_dict_exporter_symlink_node(): """Dict Exporter.""" root = Node("root") From a7608be13a2155b916c7b61bca78326d92240668 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 18:44:38 -0500 Subject: [PATCH 05/16] symlink node json exporter test --- tests/test_dictexporter.py | 4 +-- tests/test_jsonexporter.py | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/tests/test_dictexporter.py b/tests/test_dictexporter.py index 0dfff58..5ea74c4 100644 --- a/tests/test_dictexporter.py +++ b/tests/test_dictexporter.py @@ -145,7 +145,7 @@ def test_dict_exporter_symlink_node(): s1a = SymlinkNode(target=s0, parent=s1) s1b = Node("sub1B", parent=s1) s1c = Node("sub1C", parent=s1) - s1ca = Node("sub1Ca", parent=s1c) + s1ca = SymlinkNode(target=s1, parent=s1c) exporter = DictExporter() eq_(exporter.export(root), @@ -158,7 +158,7 @@ def test_dict_exporter_symlink_node(): {'name': 'sub0'}, {'name': 'sub1B'}, {'name': 'sub1C', 'children': [ - {'name': 'sub1Ca'} + {'name': 'sub1Ca', 'foo':'bar'} ]} ]} ]} diff --git a/tests/test_jsonexporter.py b/tests/test_jsonexporter.py index e60bf5f..9bcd73b 100644 --- a/tests/test_jsonexporter.py +++ b/tests/test_jsonexporter.py @@ -5,6 +5,7 @@ from nose.tools import eq_ from anytree import AnyNode +from anytree import SymlinkNode from anytree.exporter import JsonExporter @@ -91,3 +92,62 @@ def test_json_exporter(): finally: os.remove(ref.name) os.remove(gen.name) + + +def test_symlink_json_exporter(): + """Json Exporter.""" + root = AnyNode(id="root") + s0 = AnyNode(id="sub0", parent=root) + AnyNode(id="sub0B", parent=s0) + AnyNode(id="sub0A", parent=s0) + s1 = AnyNode(id="sub1", parent=root) + AnyNode(id="sub1A", parent=s1) + AnyNode(id="sub1B", parent=s1) + s1c = AnyNode(id="sub1C", parent=s1) + AnyNode(id="sub1Ca", parent=s1c) + SymlinkNode(target=s0, parent=s1) + + exporter = JsonExporter(indent=2, sort_keys=True) + exported = exporter.export(root).split("\n") + exported = [e.rstrip() for e in exported] # just a fix for a strange py2x behavior. + lines = [ + '{', + ' "children": [', + ' {', + ' "children": [', + ' {', + ' "id": "sub0B"', + ' },', + ' {', + ' "id": "sub0A"', + ' }', + ' ],', + ' "id": "sub0"', + ' },', + ' {', + ' "children": [', + ' {', + ' "id": "sub1A"', + ' },', + ' {', + ' "id": "sub1B"', + ' },', + ' {', + ' "children": [', + ' {', + ' "id": "sub1Ca"', + ' }', + ' ],', + ' "id": "sub1C"', + ' }', + ' {', + ' "id": "sub0"', + ' },', + ' ],', + ' "id": "sub1"', + ' }', + ' ],', + ' "id": "root"', + '}' + ] + eq_(exported, lines) \ No newline at end of file From 1668d4b43c0851b1cbeeabdc4c1d4bd606b14059 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 18:49:03 -0500 Subject: [PATCH 06/16] white space --- tests/test_jsonexporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_jsonexporter.py b/tests/test_jsonexporter.py index 9bcd73b..7685f74 100644 --- a/tests/test_jsonexporter.py +++ b/tests/test_jsonexporter.py @@ -150,4 +150,4 @@ def test_symlink_json_exporter(): ' "id": "root"', '}' ] - eq_(exported, lines) \ No newline at end of file + eq_(exported, lines) From 9dc3f792109f8c0898f260a0dc6c98c948d5ea22 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 18:57:05 -0500 Subject: [PATCH 07/16] white space --- tests/test_dictexporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_dictexporter.py b/tests/test_dictexporter.py index 5ea74c4..28418a1 100644 --- a/tests/test_dictexporter.py +++ b/tests/test_dictexporter.py @@ -158,7 +158,7 @@ def test_dict_exporter_symlink_node(): {'name': 'sub0'}, {'name': 'sub1B'}, {'name': 'sub1C', 'children': [ - {'name': 'sub1Ca', 'foo':'bar'} + {'name': 'sub1Ca', 'foo': 'bar'} ]} ]} ]} From fbcfb68f6d3eb8f1fdfa823da07dcebe10d11390 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 19:10:16 -0500 Subject: [PATCH 08/16] missed comma --- tests/test_jsonexporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_jsonexporter.py b/tests/test_jsonexporter.py index 7685f74..4b91bae 100644 --- a/tests/test_jsonexporter.py +++ b/tests/test_jsonexporter.py @@ -139,7 +139,7 @@ def test_symlink_json_exporter(): ' }', ' ],', ' "id": "sub1C"', - ' }', + ' },', ' {', ' "id": "sub0"', ' },', From 6c55a07f8bc172be6386101847e0b4715adad3fa Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 19:13:54 -0500 Subject: [PATCH 09/16] remove comma --- tests/test_jsonexporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_jsonexporter.py b/tests/test_jsonexporter.py index 4b91bae..15005cb 100644 --- a/tests/test_jsonexporter.py +++ b/tests/test_jsonexporter.py @@ -142,7 +142,7 @@ def test_symlink_json_exporter(): ' },', ' {', ' "id": "sub0"', - ' },', + ' }', ' ],', ' "id": "sub1"', ' }', From 43ca9c9a91406ddd9c763257fa73aa70e7a8ac3f Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 19:19:31 -0500 Subject: [PATCH 10/16] remove json test --- tests/test_jsonexporter.py | 59 -------------------------------------- 1 file changed, 59 deletions(-) diff --git a/tests/test_jsonexporter.py b/tests/test_jsonexporter.py index 15005cb..4417eff 100644 --- a/tests/test_jsonexporter.py +++ b/tests/test_jsonexporter.py @@ -92,62 +92,3 @@ def test_json_exporter(): finally: os.remove(ref.name) os.remove(gen.name) - - -def test_symlink_json_exporter(): - """Json Exporter.""" - root = AnyNode(id="root") - s0 = AnyNode(id="sub0", parent=root) - AnyNode(id="sub0B", parent=s0) - AnyNode(id="sub0A", parent=s0) - s1 = AnyNode(id="sub1", parent=root) - AnyNode(id="sub1A", parent=s1) - AnyNode(id="sub1B", parent=s1) - s1c = AnyNode(id="sub1C", parent=s1) - AnyNode(id="sub1Ca", parent=s1c) - SymlinkNode(target=s0, parent=s1) - - exporter = JsonExporter(indent=2, sort_keys=True) - exported = exporter.export(root).split("\n") - exported = [e.rstrip() for e in exported] # just a fix for a strange py2x behavior. - lines = [ - '{', - ' "children": [', - ' {', - ' "children": [', - ' {', - ' "id": "sub0B"', - ' },', - ' {', - ' "id": "sub0A"', - ' }', - ' ],', - ' "id": "sub0"', - ' },', - ' {', - ' "children": [', - ' {', - ' "id": "sub1A"', - ' },', - ' {', - ' "id": "sub1B"', - ' },', - ' {', - ' "children": [', - ' {', - ' "id": "sub1Ca"', - ' }', - ' ],', - ' "id": "sub1C"', - ' },', - ' {', - ' "id": "sub0"', - ' }', - ' ],', - ' "id": "sub1"', - ' }', - ' ],', - ' "id": "root"', - '}' - ] - eq_(exported, lines) From 221abd9c40002b19f2862c06e4dda4eff7ce1bf3 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Thu, 10 Feb 2022 19:26:39 -0500 Subject: [PATCH 11/16] fixed dictexport test --- tests/test_dictexporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_dictexporter.py b/tests/test_dictexporter.py index 28418a1..59952e6 100644 --- a/tests/test_dictexporter.py +++ b/tests/test_dictexporter.py @@ -158,7 +158,7 @@ def test_dict_exporter_symlink_node(): {'name': 'sub0'}, {'name': 'sub1B'}, {'name': 'sub1C', 'children': [ - {'name': 'sub1Ca', 'foo': 'bar'} + {'name': 'sub1', 'foo': 'bar'} ]} ]} ]} From f656caff0f9e121279e4a5b9a82ca6c5bc226ed8 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Fri, 11 Feb 2022 17:05:56 -0500 Subject: [PATCH 12/16] reduced addition by calling self --- anytree/exporter/dictexporter.py | 7 ++----- tests/test_jsonexporter.py | 1 - 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/anytree/exporter/dictexporter.py b/anytree/exporter/dictexporter.py index b2b43e1..16e06eb 100644 --- a/anytree/exporter/dictexporter.py +++ b/anytree/exporter/dictexporter.py @@ -89,10 +89,7 @@ def _iter_attr_values(self, node): for k, v in node.__dict__.items(): if k in ('_NodeMixin__children', '_NodeMixin__parent'): continue - elif k == "target": - for key, val in v.__dict__.items(): - if key in ('_NodeMixin__children', '_NodeMixin__parent'): - continue - yield key, val + elif k == "target": #If SymlinkNode(Node(path)) + yield from self._iter_attr_values(v) else: yield k, v diff --git a/tests/test_jsonexporter.py b/tests/test_jsonexporter.py index 4417eff..e60bf5f 100644 --- a/tests/test_jsonexporter.py +++ b/tests/test_jsonexporter.py @@ -5,7 +5,6 @@ from nose.tools import eq_ from anytree import AnyNode -from anytree import SymlinkNode from anytree.exporter import JsonExporter From acb4dbec4af124ca5f7c95c6bc64e2ab4a70c6ba Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Fri, 11 Feb 2022 17:11:06 -0500 Subject: [PATCH 13/16] fix comment placement --- anytree/exporter/dictexporter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/anytree/exporter/dictexporter.py b/anytree/exporter/dictexporter.py index 16e06eb..9f83a15 100644 --- a/anytree/exporter/dictexporter.py +++ b/anytree/exporter/dictexporter.py @@ -89,7 +89,8 @@ def _iter_attr_values(self, node): for k, v in node.__dict__.items(): if k in ('_NodeMixin__children', '_NodeMixin__parent'): continue - elif k == "target": #If SymlinkNode(Node(path)) + elif k == "target": + #If SymlinkNode(Node(path)) yield from self._iter_attr_values(v) else: yield k, v From dbcedde43fadfefdd0a3c47adde44693d1b3b249 Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Fri, 11 Feb 2022 19:40:08 -0500 Subject: [PATCH 14/16] replace yield from w/2.8 compatible --- anytree/exporter/dictexporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anytree/exporter/dictexporter.py b/anytree/exporter/dictexporter.py index 9f83a15..05260e3 100644 --- a/anytree/exporter/dictexporter.py +++ b/anytree/exporter/dictexporter.py @@ -91,6 +91,6 @@ def _iter_attr_values(self, node): continue elif k == "target": #If SymlinkNode(Node(path)) - yield from self._iter_attr_values(v) + for key, val in self._iter_attr_values(v): yield key, val else: yield k, v From ce1f0e7e25199c96f68cdaa3919ad63839acf76f Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Fri, 11 Feb 2022 21:33:42 -0500 Subject: [PATCH 15/16] pycodestyle --- anytree/exporter/dictexporter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/anytree/exporter/dictexporter.py b/anytree/exporter/dictexporter.py index 05260e3..74b5cc2 100644 --- a/anytree/exporter/dictexporter.py +++ b/anytree/exporter/dictexporter.py @@ -90,7 +90,8 @@ def _iter_attr_values(self, node): if k in ('_NodeMixin__children', '_NodeMixin__parent'): continue elif k == "target": - #If SymlinkNode(Node(path)) - for key, val in self._iter_attr_values(v): yield key, val + #If SymlinkNode(Node(path)) + for key, val in self._iter_attr_values(v): + yield key, val else: yield k, v From 4fac651de5e52e93a6e66d317ff858ac46fdb95a Mon Sep 17 00:00:00 2001 From: Jaime Sandoval Date: Sun, 13 Feb 2022 10:26:46 -0500 Subject: [PATCH 16/16] style changes --- anytree/exporter/dictexporter.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/anytree/exporter/dictexporter.py b/anytree/exporter/dictexporter.py index 74b5cc2..ff0ca02 100644 --- a/anytree/exporter/dictexporter.py +++ b/anytree/exporter/dictexporter.py @@ -90,8 +90,7 @@ def _iter_attr_values(self, node): if k in ('_NodeMixin__children', '_NodeMixin__parent'): continue elif k == "target": - #If SymlinkNode(Node(path)) - for key, val in self._iter_attr_values(v): + for key, val in self._iter_attr_values(v): yield key, val else: yield k, v