17
17
18
18
"""Utilities for creating VCG and Dot diagrams"""
19
19
20
+ import itertools
20
21
import os
21
22
23
+ import astroid
24
+ from astroid import modutils
25
+
22
26
from pylint .pyreverse .diagrams import (
23
27
ClassDiagram ,
24
28
ClassEntity ,
29
+ DiagramEntity ,
25
30
PackageDiagram ,
26
31
PackageEntity ,
27
32
)
@@ -38,6 +43,29 @@ def __init__(self, config):
38
43
self .printer_class = get_printer_for_filetype (self .config .output_format )
39
44
self .printer = None # defined in set_printer
40
45
self .file_name = "" # defined in set_printer
46
+ self .depth = self .config .max_color_depth
47
+ self .available_colors = itertools .cycle (
48
+ [
49
+ "aliceblue" ,
50
+ "antiquewhite" ,
51
+ "aquamarine" ,
52
+ "burlywood" ,
53
+ "cadetblue" ,
54
+ "chartreuse" ,
55
+ "chocolate" ,
56
+ "coral" ,
57
+ "cornflowerblue" ,
58
+ "cyan" ,
59
+ "darkgoldenrod" ,
60
+ "darkseagreen" ,
61
+ "dodgerblue" ,
62
+ "forestgreen" ,
63
+ "gold" ,
64
+ "hotpink" ,
65
+ "mediumspringgreen" ,
66
+ ]
67
+ )
68
+ self .used_colors = {}
41
69
42
70
def write (self , diadefs ):
43
71
"""write files for <project> according to <diadefs>"""
@@ -108,12 +136,11 @@ def set_printer(self, file_name: str, basename: str) -> None:
108
136
self .printer = self .printer_class (basename )
109
137
self .file_name = file_name
110
138
111
- @staticmethod
112
- def get_package_properties (obj : PackageEntity ) -> NodeProperties :
139
+ def get_package_properties (self , obj : PackageEntity ) -> NodeProperties :
113
140
"""get label and shape for packages."""
114
141
return NodeProperties (
115
142
label = obj .title ,
116
- color = "black" ,
143
+ color = self . get_shape_color ( obj ) if self . config . colorized else "black" ,
117
144
)
118
145
119
146
def get_class_properties (self , obj : ClassEntity ) -> NodeProperties :
@@ -123,10 +150,26 @@ def get_class_properties(self, obj: ClassEntity) -> NodeProperties:
123
150
attrs = obj .attrs if not self .config .only_classnames else None ,
124
151
methods = obj .methods if not self .config .only_classnames else None ,
125
152
fontcolor = "red" if is_exception (obj .node ) else "black" ,
126
- color = "black" ,
153
+ color = self . get_shape_color ( obj ) if self . config . colorized else "black" ,
127
154
)
128
155
return properties
129
156
157
+ def get_shape_color (self , obj : DiagramEntity ) -> str :
158
+ """get shape color"""
159
+ qualified_name = obj .node .qname ()
160
+ if modutils .is_standard_module (qualified_name .split ("." , maxsplit = 1 )[0 ]):
161
+ return "grey"
162
+ if isinstance (obj .node , astroid .ClassDef ):
163
+ package = qualified_name .rsplit ("." , maxsplit = 2 )[0 ]
164
+ elif obj .node .package :
165
+ package = qualified_name
166
+ else :
167
+ package = qualified_name .rsplit ("." , maxsplit = 1 )[0 ]
168
+ base_name = "." .join (package .split ("." , self .depth )[: self .depth ])
169
+ if base_name not in self .used_colors :
170
+ self .used_colors [base_name ] = next (self .available_colors )
171
+ return self .used_colors [base_name ]
172
+
130
173
def save (self ) -> None :
131
174
"""write to disk"""
132
175
self .printer .generate (self .file_name )
0 commit comments