asciitree 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- asciitree/__init__.py +76 -0
- asciitree/drawing.py +106 -0
- asciitree/traversal.py +46 -0
- asciitree/util.py +4 -0
- asciitree-0.4.0.dist-info/METADATA +48 -0
- asciitree-0.4.0.dist-info/RECORD +9 -0
- asciitree-0.4.0.dist-info/WHEEL +5 -0
- asciitree-0.4.0.dist-info/licenses/LICENSE +19 -0
- asciitree-0.4.0.dist-info/top_level.txt +1 -0
asciitree/__init__.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
|
|
3
|
+
from .drawing import BoxStyle, Style
|
|
4
|
+
from .traversal import DictTraversal, Traversal
|
|
5
|
+
from .util import KeyArgsConstructor
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class LeftAligned(KeyArgsConstructor):
|
|
9
|
+
"""Creates a renderer for a left-aligned tree.
|
|
10
|
+
|
|
11
|
+
Any attributes of the resulting class instances can be set using
|
|
12
|
+
constructor arguments."""
|
|
13
|
+
|
|
14
|
+
draw = BoxStyle()
|
|
15
|
+
"The draw style used. See :class:`~asciitree.drawing.Style`."
|
|
16
|
+
traverse = DictTraversal()
|
|
17
|
+
"Traversal method. See :class:`~asciitree.traversal.Traversal`."
|
|
18
|
+
|
|
19
|
+
def render(self, node):
|
|
20
|
+
"""Renders a node. This function is used internally, as it returns
|
|
21
|
+
a list of lines. Use :func:`~asciitree.LeftAligned.__call__` instead.
|
|
22
|
+
"""
|
|
23
|
+
lines = []
|
|
24
|
+
|
|
25
|
+
children = self.traverse.get_children(node)
|
|
26
|
+
lines.append(self.draw.node_label(self.traverse.get_text(node)))
|
|
27
|
+
|
|
28
|
+
for n, child in enumerate(children):
|
|
29
|
+
child_tree = self.render(child)
|
|
30
|
+
|
|
31
|
+
if n == len(children) - 1:
|
|
32
|
+
# last child does not get the line drawn
|
|
33
|
+
lines.append(self.draw.last_child_head(child_tree.pop(0)))
|
|
34
|
+
lines.extend(self.draw.last_child_tail(line) for line in child_tree)
|
|
35
|
+
else:
|
|
36
|
+
lines.append(self.draw.child_head(child_tree.pop(0)))
|
|
37
|
+
lines.extend(self.draw.child_tail(line) for line in child_tree)
|
|
38
|
+
|
|
39
|
+
return lines
|
|
40
|
+
|
|
41
|
+
def __call__(self, tree):
|
|
42
|
+
"""Render the tree into string suitable for console output.
|
|
43
|
+
|
|
44
|
+
:param tree: A tree."""
|
|
45
|
+
return "\n".join(self.render(self.traverse.get_root(tree)))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# legacy support below
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class LegacyStyle(Style):
|
|
52
|
+
def node_label(self, text):
|
|
53
|
+
return text
|
|
54
|
+
|
|
55
|
+
def child_head(self, label):
|
|
56
|
+
return " +--" + label
|
|
57
|
+
|
|
58
|
+
def child_tail(self, line):
|
|
59
|
+
return " |" + line
|
|
60
|
+
|
|
61
|
+
def last_child_head(self, label):
|
|
62
|
+
return " +--" + label
|
|
63
|
+
|
|
64
|
+
def last_child_tail(self, line):
|
|
65
|
+
return " " + line
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def draw_tree(node, child_iter=lambda n: n.children, text_str=str):
|
|
69
|
+
"""Support asciitree 0.2 API.
|
|
70
|
+
|
|
71
|
+
This function solely exist to not break old code (using asciitree 0.2).
|
|
72
|
+
Its use is deprecated."""
|
|
73
|
+
return LeftAligned(
|
|
74
|
+
traverse=Traversal(get_text=text_str, get_children=child_iter),
|
|
75
|
+
draw=LegacyStyle(),
|
|
76
|
+
)(node)
|
asciitree/drawing.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
from .util import KeyArgsConstructor
|
|
2
|
+
|
|
3
|
+
BOX_LIGHT = {
|
|
4
|
+
"UP_AND_RIGHT": "\u2514",
|
|
5
|
+
"HORIZONTAL": "\u2500",
|
|
6
|
+
"VERTICAL": "\u2502",
|
|
7
|
+
"VERTICAL_AND_RIGHT": "\u251c",
|
|
8
|
+
} #: Unicode box-drawing glyphs, light style
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
BOX_HEAVY = {
|
|
12
|
+
"UP_AND_RIGHT": "\u2517",
|
|
13
|
+
"HORIZONTAL": "\u2501",
|
|
14
|
+
"VERTICAL": "\u2503",
|
|
15
|
+
"VERTICAL_AND_RIGHT": "\u2523",
|
|
16
|
+
} #: Unicode box-drawing glyphs, heavy style
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
BOX_DOUBLE = {
|
|
20
|
+
"UP_AND_RIGHT": "\u255a",
|
|
21
|
+
"HORIZONTAL": "\u2550",
|
|
22
|
+
"VERTICAL": "\u2551",
|
|
23
|
+
"VERTICAL_AND_RIGHT": "\u2560",
|
|
24
|
+
} #: Unicode box-drawing glyphs, double-line style
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
BOX_ASCII = {
|
|
28
|
+
"UP_AND_RIGHT": "+",
|
|
29
|
+
"HORIZONTAL": "-",
|
|
30
|
+
"VERTICAL": "|",
|
|
31
|
+
"VERTICAL_AND_RIGHT": "+",
|
|
32
|
+
} #: Unicode box-drawing glyphs, using only ascii ``|+-`` characters.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
BOX_BLANK = {
|
|
36
|
+
"UP_AND_RIGHT": " ",
|
|
37
|
+
"HORIZONTAL": " ",
|
|
38
|
+
"VERTICAL": " ",
|
|
39
|
+
"VERTICAL_AND_RIGHT": " ",
|
|
40
|
+
} #: Unicode box-drawing glyphs, using only spaces.
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Style(KeyArgsConstructor):
|
|
44
|
+
"""Rendering style for trees."""
|
|
45
|
+
|
|
46
|
+
label_format = "{}" #: Format for labels.
|
|
47
|
+
|
|
48
|
+
def node_label(self, text):
|
|
49
|
+
"""Render a node text into a label."""
|
|
50
|
+
return self.label_format.format(text)
|
|
51
|
+
|
|
52
|
+
def child_head(self, label):
|
|
53
|
+
"""Render a node label into final output."""
|
|
54
|
+
return label
|
|
55
|
+
|
|
56
|
+
def child_tail(self, line):
|
|
57
|
+
"""Render a node line that is not a label into final output."""
|
|
58
|
+
return line
|
|
59
|
+
|
|
60
|
+
def last_child_head(self, label):
|
|
61
|
+
"""Like :func:`~asciitree.drawing.Style.child_head` but only called
|
|
62
|
+
for the last child."""
|
|
63
|
+
return label
|
|
64
|
+
|
|
65
|
+
def last_child_tail(self, line):
|
|
66
|
+
"""Like :func:`~asciitree.drawing.Style.child_tail` but only called
|
|
67
|
+
for the last child."""
|
|
68
|
+
return line
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class BoxStyle(Style):
|
|
72
|
+
"""A rendering style that uses box draw characters and a common layout."""
|
|
73
|
+
|
|
74
|
+
gfx = BOX_ASCII #: Glyhps to use.
|
|
75
|
+
label_space = 1 #: Space between glyphs and label.
|
|
76
|
+
horiz_len = 2 #: Length of horizontal lines
|
|
77
|
+
indent = 1 #: Indent for subtrees
|
|
78
|
+
|
|
79
|
+
def child_head(self, label):
|
|
80
|
+
return (
|
|
81
|
+
" " * self.indent
|
|
82
|
+
+ self.gfx["VERTICAL_AND_RIGHT"]
|
|
83
|
+
+ self.gfx["HORIZONTAL"] * self.horiz_len
|
|
84
|
+
+ " " * self.label_space
|
|
85
|
+
+ label
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def child_tail(self, line):
|
|
89
|
+
return " " * self.indent + self.gfx["VERTICAL"] + " " * self.horiz_len + line
|
|
90
|
+
|
|
91
|
+
def last_child_head(self, label):
|
|
92
|
+
return (
|
|
93
|
+
" " * self.indent
|
|
94
|
+
+ self.gfx["UP_AND_RIGHT"]
|
|
95
|
+
+ self.gfx["HORIZONTAL"] * self.horiz_len
|
|
96
|
+
+ " " * self.label_space
|
|
97
|
+
+ label
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
def last_child_tail(self, line):
|
|
101
|
+
return (
|
|
102
|
+
" " * self.indent
|
|
103
|
+
+ " " * len(self.gfx["VERTICAL"])
|
|
104
|
+
+ " " * self.horiz_len
|
|
105
|
+
+ line
|
|
106
|
+
)
|
asciitree/traversal.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from .util import KeyArgsConstructor
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Traversal(KeyArgsConstructor):
|
|
5
|
+
"""Traversal method.
|
|
6
|
+
|
|
7
|
+
Used by the tree rendering functions like :class:`~asciitree.LeftAligned`.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def get_children(self, node):
|
|
11
|
+
"""Return a list of children of a node."""
|
|
12
|
+
raise NotImplementedError
|
|
13
|
+
|
|
14
|
+
def get_root(self, tree):
|
|
15
|
+
"""Return a node representing the tree root from the tree."""
|
|
16
|
+
return tree
|
|
17
|
+
|
|
18
|
+
def get_text(self, node):
|
|
19
|
+
"""Return the text associated with a node."""
|
|
20
|
+
return str(node)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class DictTraversal(Traversal):
|
|
24
|
+
"""Traversal suitable for a dictionary. Keys are tree labels, all values
|
|
25
|
+
must be dictionaries as well."""
|
|
26
|
+
|
|
27
|
+
def get_children(self, node):
|
|
28
|
+
return list(node[1].items())
|
|
29
|
+
|
|
30
|
+
def get_root(self, tree):
|
|
31
|
+
return list(tree.items())[0]
|
|
32
|
+
|
|
33
|
+
def get_text(self, node):
|
|
34
|
+
return node[0]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class AttributeTraversal(Traversal):
|
|
38
|
+
"""Attribute traversal.
|
|
39
|
+
|
|
40
|
+
Uses an attribute of a node as its list of children.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
attribute = "children" #: Attribute to use.
|
|
44
|
+
|
|
45
|
+
def get_children(self, node):
|
|
46
|
+
return getattr(node, self.attribute)
|
asciitree/util.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: asciitree
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Draws ASCII trees.
|
|
5
|
+
Author-email: Marc Brinkmann <git@marcbrinkmann.de>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/mbr/asciitree
|
|
8
|
+
Project-URL: Documentation, https://asciitree.readthedocs.io/en/stable/
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# ASCII Trees
|
|
14
|
+
|
|
15
|
+
```text
|
|
16
|
+
asciitree
|
|
17
|
+
+-- sometimes
|
|
18
|
+
| +-- you
|
|
19
|
+
+-- just
|
|
20
|
+
| +-- want
|
|
21
|
+
| +-- to
|
|
22
|
+
| +-- draw
|
|
23
|
+
+-- trees
|
|
24
|
+
+-- in
|
|
25
|
+
+-- your
|
|
26
|
+
+-- terminal
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from asciitree import LeftAligned
|
|
31
|
+
|
|
32
|
+
tree = {
|
|
33
|
+
"asciitree": {
|
|
34
|
+
"sometimes": {"you": {}},
|
|
35
|
+
"just": {"want": {"to": {}, "draw": {}}},
|
|
36
|
+
"trees": {},
|
|
37
|
+
"in": {"your": {"terminal": {}}},
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
tr = LeftAligned()
|
|
42
|
+
print(tr(tree))
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Since Python 3.7, dictionaries preserve insertion order. On older versions, use
|
|
46
|
+
`collections.OrderedDict` to preserve the tree's display order.
|
|
47
|
+
|
|
48
|
+
Read the documentation at <https://asciitree.readthedocs.io/en/stable/>.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
asciitree/__init__.py,sha256=gb_wsi-4YiJKvb2Z8JszUpky7qpxHy521rpNZI_T2mE,2316
|
|
2
|
+
asciitree/drawing.py,sha256=yrjq52Pu8F4R52TkMRaTGCH2DHZaqYxeR57QsELK6_Y,2851
|
|
3
|
+
asciitree/traversal.py,sha256=NEERizljL40by7o96Ce2kwLtGlPjSS4W8kYnH9jVCHI,1145
|
|
4
|
+
asciitree/util.py,sha256=mGTP0CcemVFum7rfqA-Inccy7TS-Ex5ACibysnVwTuQ,128
|
|
5
|
+
asciitree-0.4.0.dist-info/licenses/LICENSE,sha256=8z8F-omd7Yenm3YxLuXk-_JDzam6E2rgze1llf7OBFQ,1063
|
|
6
|
+
asciitree-0.4.0.dist-info/METADATA,sha256=oyc9K69-tUs2phUtsxZx6GR5VrKX1MNOATpfVziJctk,1036
|
|
7
|
+
asciitree-0.4.0.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
|
|
8
|
+
asciitree-0.4.0.dist-info/top_level.txt,sha256=_raGTksZX3UafaY0sEceT6XxVcYitoHTsu_Nh9x-tdo,10
|
|
9
|
+
asciitree-0.4.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2015-2026 Marc Brinkmann
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
|
4
|
+
copy of this software and associated documentation files (the "Software"),
|
|
5
|
+
to deal in the Software without restriction, including without limitation
|
|
6
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
7
|
+
and/or sell copies of the Software, and to permit persons to whom the
|
|
8
|
+
Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
18
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
19
|
+
DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
asciitree
|