Spaces:
Running
Running
Update class_diagram_generator.py
Browse files- class_diagram_generator.py +70 -56
class_diagram_generator.py
CHANGED
@@ -207,18 +207,25 @@ def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
207 |
'splines': 'ortho',
|
208 |
'bgcolor': 'white',
|
209 |
'pad': '0.5',
|
210 |
-
'nodesep': '1.
|
211 |
-
'ranksep': '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
}
|
213 |
)
|
214 |
|
215 |
-
base_color = '#19191a'
|
216 |
-
lightening_factor = 0.15
|
217 |
-
|
218 |
classes = data.get('classes', [])
|
219 |
relationships = data.get('relationships', [])
|
220 |
|
221 |
-
for
|
222 |
class_name = cls.get('name')
|
223 |
class_type = cls.get('type', 'class')
|
224 |
attributes = cls.get('attributes', [])
|
@@ -227,28 +234,6 @@ def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
227 |
if not class_name:
|
228 |
raise ValueError(f"Invalid class: {cls}")
|
229 |
|
230 |
-
current_depth = i % 6
|
231 |
-
|
232 |
-
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7:
|
233 |
-
base_color_safe = '#19191a'
|
234 |
-
else:
|
235 |
-
base_color_safe = base_color
|
236 |
-
|
237 |
-
base_r = int(base_color_safe[1:3], 16)
|
238 |
-
base_g = int(base_color_safe[3:5], 16)
|
239 |
-
base_b = int(base_color_safe[5:7], 16)
|
240 |
-
|
241 |
-
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor)
|
242 |
-
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor)
|
243 |
-
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor)
|
244 |
-
|
245 |
-
current_r = min(255, current_r)
|
246 |
-
current_g = min(255, current_g)
|
247 |
-
current_b = min(255, current_b)
|
248 |
-
|
249 |
-
node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
250 |
-
font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black'
|
251 |
-
|
252 |
class_label = ""
|
253 |
|
254 |
if class_type == 'abstract':
|
@@ -258,26 +243,28 @@ def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
258 |
elif class_type == 'enum':
|
259 |
class_label += "<<enumeration>>\\n"
|
260 |
|
261 |
-
class_label += f"{class_name}
|
262 |
|
263 |
if attributes:
|
264 |
-
class_label += "
|
265 |
for attr in attributes:
|
266 |
visibility = attr.get('visibility', '+')
|
267 |
name = attr.get('name', '')
|
268 |
attr_type = attr.get('type', '')
|
269 |
is_static = attr.get('static', False)
|
270 |
|
271 |
-
attr_line = f"{visibility} "
|
272 |
-
if is_static:
|
273 |
-
attr_line += f"<<static>> "
|
274 |
-
attr_line += f"{name}"
|
275 |
if attr_type:
|
276 |
attr_line += f" : {attr_type}"
|
|
|
|
|
277 |
class_label += f"{attr_line}\\l"
|
278 |
|
279 |
if methods:
|
280 |
-
|
|
|
|
|
|
|
281 |
for method in methods:
|
282 |
visibility = method.get('visibility', '+')
|
283 |
name = method.get('name', '')
|
@@ -286,13 +273,7 @@ def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
286 |
is_static = method.get('static', False)
|
287 |
is_abstract = method.get('abstract', False)
|
288 |
|
289 |
-
method_line = f"{visibility} "
|
290 |
-
if is_static:
|
291 |
-
method_line += f"<<static>> "
|
292 |
-
if is_abstract:
|
293 |
-
method_line += f"<<abstract>> "
|
294 |
-
|
295 |
-
method_line += f"{name}("
|
296 |
if parameters:
|
297 |
param_strs = []
|
298 |
for param in parameters:
|
@@ -301,22 +282,32 @@ def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
301 |
param_strs.append(f"{param_name}: {param_type}")
|
302 |
method_line += ", ".join(param_strs)
|
303 |
method_line += f") : {return_type}"
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
class_label += f"{method_line}\\l"
|
305 |
|
306 |
if class_type == 'interface':
|
307 |
style = 'filled,dashed'
|
|
|
|
|
|
|
|
|
308 |
else:
|
309 |
style = 'filled'
|
|
|
310 |
|
311 |
dot.node(
|
312 |
class_name,
|
313 |
-
class_label,
|
314 |
shape='record',
|
315 |
style=style,
|
316 |
-
fillcolor=
|
317 |
-
|
318 |
-
|
319 |
-
fontname='Helvetica'
|
320 |
)
|
321 |
|
322 |
for relationship in relationships:
|
@@ -330,22 +321,45 @@ def generate_class_diagram(json_input: str, output_format: str) -> str:
|
|
330 |
if not all([from_class, to_class]):
|
331 |
raise ValueError(f"Invalid relationship: {relationship}")
|
332 |
|
333 |
-
|
334 |
-
|
335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
336 |
|
337 |
if rel_type == 'inheritance':
|
338 |
-
|
|
|
339 |
elif rel_type == 'composition':
|
340 |
-
|
|
|
|
|
|
|
341 |
elif rel_type == 'aggregation':
|
342 |
-
|
|
|
|
|
|
|
343 |
elif rel_type == 'realization':
|
344 |
-
|
|
|
|
|
345 |
elif rel_type == 'dependency':
|
346 |
-
|
|
|
|
|
347 |
else:
|
348 |
-
|
|
|
|
|
|
|
349 |
|
350 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
351 |
dot.render(tmp.name, format=output_format, cleanup=True)
|
|
|
207 |
'splines': 'ortho',
|
208 |
'bgcolor': 'white',
|
209 |
'pad': '0.5',
|
210 |
+
'nodesep': '1.0',
|
211 |
+
'ranksep': '1.5',
|
212 |
+
'fontname': 'Arial',
|
213 |
+
'dpi': '300'
|
214 |
+
},
|
215 |
+
node_attr={
|
216 |
+
'fontname': 'Arial',
|
217 |
+
'fontsize': '10'
|
218 |
+
},
|
219 |
+
edge_attr={
|
220 |
+
'fontname': 'Arial',
|
221 |
+
'fontsize': '9'
|
222 |
}
|
223 |
)
|
224 |
|
|
|
|
|
|
|
225 |
classes = data.get('classes', [])
|
226 |
relationships = data.get('relationships', [])
|
227 |
|
228 |
+
for cls in classes:
|
229 |
class_name = cls.get('name')
|
230 |
class_type = cls.get('type', 'class')
|
231 |
attributes = cls.get('attributes', [])
|
|
|
234 |
if not class_name:
|
235 |
raise ValueError(f"Invalid class: {cls}")
|
236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
class_label = ""
|
238 |
|
239 |
if class_type == 'abstract':
|
|
|
243 |
elif class_type == 'enum':
|
244 |
class_label += "<<enumeration>>\\n"
|
245 |
|
246 |
+
class_label += f"<B>{class_name}</B>"
|
247 |
|
248 |
if attributes:
|
249 |
+
class_label += "|"
|
250 |
for attr in attributes:
|
251 |
visibility = attr.get('visibility', '+')
|
252 |
name = attr.get('name', '')
|
253 |
attr_type = attr.get('type', '')
|
254 |
is_static = attr.get('static', False)
|
255 |
|
256 |
+
attr_line = f"{visibility} {name}"
|
|
|
|
|
|
|
257 |
if attr_type:
|
258 |
attr_line += f" : {attr_type}"
|
259 |
+
if is_static:
|
260 |
+
attr_line = f"<U>{attr_line}</U>"
|
261 |
class_label += f"{attr_line}\\l"
|
262 |
|
263 |
if methods:
|
264 |
+
if not attributes:
|
265 |
+
class_label += "|"
|
266 |
+
else:
|
267 |
+
class_label += "|"
|
268 |
for method in methods:
|
269 |
visibility = method.get('visibility', '+')
|
270 |
name = method.get('name', '')
|
|
|
273 |
is_static = method.get('static', False)
|
274 |
is_abstract = method.get('abstract', False)
|
275 |
|
276 |
+
method_line = f"{visibility} {name}("
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
if parameters:
|
278 |
param_strs = []
|
279 |
for param in parameters:
|
|
|
282 |
param_strs.append(f"{param_name}: {param_type}")
|
283 |
method_line += ", ".join(param_strs)
|
284 |
method_line += f") : {return_type}"
|
285 |
+
|
286 |
+
if is_static:
|
287 |
+
method_line = f"<U>{method_line}</U>"
|
288 |
+
if is_abstract:
|
289 |
+
method_line = f"<I>{method_line}</I>"
|
290 |
+
|
291 |
class_label += f"{method_line}\\l"
|
292 |
|
293 |
if class_type == 'interface':
|
294 |
style = 'filled,dashed'
|
295 |
+
fillcolor = '#f0f0f0'
|
296 |
+
elif class_type == 'abstract':
|
297 |
+
style = 'filled'
|
298 |
+
fillcolor = '#e8e8e8'
|
299 |
else:
|
300 |
style = 'filled'
|
301 |
+
fillcolor = 'white'
|
302 |
|
303 |
dot.node(
|
304 |
class_name,
|
305 |
+
f"<{class_label}>",
|
306 |
shape='record',
|
307 |
style=style,
|
308 |
+
fillcolor=fillcolor,
|
309 |
+
color='black',
|
310 |
+
penwidth='1'
|
|
|
311 |
)
|
312 |
|
313 |
for relationship in relationships:
|
|
|
321 |
if not all([from_class, to_class]):
|
322 |
raise ValueError(f"Invalid relationship: {relationship}")
|
323 |
|
324 |
+
edge_attrs = {
|
325 |
+
'color': 'black',
|
326 |
+
'fontcolor': 'black',
|
327 |
+
'fontsize': '9'
|
328 |
+
}
|
329 |
+
|
330 |
+
if label:
|
331 |
+
edge_attrs['label'] = label
|
332 |
+
|
333 |
+
if multiplicity_from and multiplicity_to:
|
334 |
+
edge_attrs['headlabel'] = multiplicity_to
|
335 |
+
edge_attrs['taillabel'] = multiplicity_from
|
336 |
|
337 |
if rel_type == 'inheritance':
|
338 |
+
edge_attrs['arrowhead'] = 'empty'
|
339 |
+
edge_attrs['arrowsize'] = '1.2'
|
340 |
elif rel_type == 'composition':
|
341 |
+
edge_attrs['arrowhead'] = 'normal'
|
342 |
+
edge_attrs['arrowtail'] = 'diamond'
|
343 |
+
edge_attrs['dir'] = 'both'
|
344 |
+
edge_attrs['arrowsize'] = '1.0'
|
345 |
elif rel_type == 'aggregation':
|
346 |
+
edge_attrs['arrowhead'] = 'normal'
|
347 |
+
edge_attrs['arrowtail'] = 'odiamond'
|
348 |
+
edge_attrs['dir'] = 'both'
|
349 |
+
edge_attrs['arrowsize'] = '1.0'
|
350 |
elif rel_type == 'realization':
|
351 |
+
edge_attrs['arrowhead'] = 'empty'
|
352 |
+
edge_attrs['style'] = 'dashed'
|
353 |
+
edge_attrs['arrowsize'] = '1.2'
|
354 |
elif rel_type == 'dependency':
|
355 |
+
edge_attrs['arrowhead'] = 'normal'
|
356 |
+
edge_attrs['style'] = 'dashed'
|
357 |
+
edge_attrs['arrowsize'] = '1.0'
|
358 |
else:
|
359 |
+
edge_attrs['arrowhead'] = 'normal'
|
360 |
+
edge_attrs['arrowsize'] = '1.0'
|
361 |
+
|
362 |
+
dot.edge(from_class, to_class, **edge_attrs)
|
363 |
|
364 |
with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
365 |
dot.render(tmp.name, format=output_format, cleanup=True)
|