7
7
from pipdeptree ._models import DistPackage , PackageDAG , ReqPackage
8
8
9
9
10
- def render_text ( # noqa: PLR0913
10
+ def render_text (
11
11
tree : PackageDAG ,
12
12
* ,
13
13
max_depth : float ,
14
14
encoding : str ,
15
15
list_all : bool = True ,
16
- frozen : bool = False ,
17
16
include_license : bool = False ,
18
17
) -> None :
19
18
"""
20
19
Print tree as text on console.
21
20
22
21
:param tree: the package tree
23
- :param list_all: whether to list all the pgks at the root level or only those that are the sub-dependencies
24
- :param frozen: show the names of the pkgs in the output that's favorable to pip --freeze
22
+ :param max_depth: the maximum depth of the dependency tree
23
+ :param encoding: encoding to use (use "utf-8", "utf-16", "utf-32" for unicode or anything else for legacy output)
24
+ :param list_all: whether to list all the pkgs at the root level or only those that are the sub-dependencies
25
+ :param include_license: provide license information
25
26
:returns: None
26
27
28
+ """
29
+ nodes = get_top_level_nodes (tree , list_all = list_all )
30
+
31
+ if encoding in {"utf-8" , "utf-16" , "utf-32" }:
32
+ _render_text_with_unicode (tree , nodes , max_depth , include_license )
33
+ else :
34
+ _render_text_without_unicode (tree , nodes , max_depth , include_license )
35
+
36
+
37
+ def get_top_level_nodes (tree : PackageDAG , * , list_all : bool ) -> list [DistPackage ]:
38
+ """
39
+ Get a list of nodes that will appear at the first depth of the dependency tree.
40
+
41
+ :param tree: the package tree
42
+ :param list_all: whether to list all the pkgs at the root level or only those that are the sub-dependencies
27
43
"""
28
44
tree = tree .sort ()
29
45
nodes = list (tree .keys ())
@@ -32,23 +48,15 @@ def render_text( # noqa: PLR0913
32
48
if not list_all :
33
49
nodes = [p for p in nodes if p .key not in branch_keys ]
34
50
35
- if encoding in {"utf-8" , "utf-16" , "utf-32" }:
36
- _render_text_with_unicode (tree , nodes , max_depth , frozen , include_license )
37
- else :
38
- _render_text_without_unicode (tree , nodes , max_depth , frozen , include_license )
51
+ return nodes
39
52
40
53
41
54
def _render_text_with_unicode (
42
55
tree : PackageDAG ,
43
56
nodes : list [DistPackage ],
44
57
max_depth : float ,
45
- frozen : bool , # noqa: FBT001
46
58
include_license : bool , # noqa: FBT001
47
59
) -> None :
48
- assert not (frozen and include_license )
49
-
50
- use_bullets = not frozen
51
-
52
60
def aux ( # noqa: PLR0913, PLR0917
53
61
node : DistPackage | ReqPackage ,
54
62
parent : DistPackage | ReqPackage | None = None ,
@@ -61,7 +69,7 @@ def aux( # noqa: PLR0913, PLR0917
61
69
parent_is_last_child : bool = False , # noqa: FBT001, FBT002
62
70
) -> list [Any ]:
63
71
cur_chain = cur_chain or []
64
- node_str = node .render (parent , frozen = frozen )
72
+ node_str = node .render (parent , frozen = False )
65
73
next_prefix = ""
66
74
next_indent = indent + 2
67
75
@@ -70,21 +78,14 @@ def aux( # noqa: PLR0913, PLR0917
70
78
if is_last_child :
71
79
bullet = "└── "
72
80
73
- line_char = "│"
74
- if not use_bullets :
75
- line_char = ""
76
- # Add 2 spaces so direct dependencies to a project are indented
77
- bullet = " "
78
-
79
81
if has_grand_parent :
80
82
next_indent -= 1
81
83
if parent_is_last_child :
82
- offset = 0 if len (line_char ) == 1 else 1
83
- prefix += " " * (indent + 1 - offset - depth )
84
+ prefix += " " * (indent + 1 - depth )
84
85
else :
85
- prefix += line_char + " " * (indent - depth )
86
+ prefix += "│" + " " * (indent - depth )
86
87
# Without this extra space, bullets will point to the space just before the project name
87
- prefix += " " if use_bullets else ""
88
+ prefix += " "
88
89
next_prefix = prefix
89
90
node_str = prefix + bullet + node_str
90
91
elif include_license :
@@ -120,13 +121,8 @@ def _render_text_without_unicode(
120
121
tree : PackageDAG ,
121
122
nodes : list [DistPackage ],
122
123
max_depth : float ,
123
- frozen : bool , # noqa: FBT001
124
124
include_license : bool , # noqa: FBT001
125
125
) -> None :
126
- assert not (frozen and include_license )
127
-
128
- use_bullets = not frozen
129
-
130
126
def aux (
131
127
node : DistPackage | ReqPackage ,
132
128
parent : DistPackage | ReqPackage | None = None ,
@@ -135,9 +131,9 @@ def aux(
135
131
depth : int = 0 ,
136
132
) -> list [Any ]:
137
133
cur_chain = cur_chain or []
138
- node_str = node .render (parent , frozen = frozen )
134
+ node_str = node .render (parent , frozen = False )
139
135
if parent :
140
- prefix = " " * indent + ( "- " if use_bullets else "" )
136
+ prefix = " " * indent + "- "
141
137
node_str = prefix + node_str
142
138
elif include_license :
143
139
node_str += " " + node .licenses ()
@@ -154,6 +150,4 @@ def aux(
154
150
print ("\n " .join (lines )) # noqa: T201
155
151
156
152
157
- __all__ = [
158
- "render_text" ,
159
- ]
153
+ __all__ = ["get_top_level_nodes" , "render_text" ]
0 commit comments