|
27 | 27 | import sys |
28 | 28 | import argparse |
29 | 29 | from pathlib import Path |
| 30 | +from daqpytools.uml.utils import strip_typehints |
30 | 31 |
|
| 32 | +# this is gonna go into its own little file to get saved in |
31 | 33 |
|
32 | 34 | # ── Colour palette (tweak these to your taste) ────────────────────────────── |
33 | 35 | STYLE = { |
@@ -58,79 +60,6 @@ def run_pyreverse(extra_args, output_dir): |
58 | 60 | return dot_files |
59 | 61 |
|
60 | 62 |
|
61 | | -def _strip_param_types(params_str: str) -> str: |
62 | | - """Strip type hints from a parameter list (the content between parentheses). |
63 | | -
|
64 | | - Handles complex types like ``dict[str, IssueRecord]`` by counting |
65 | | - bracket depth so inner commas are not treated as parameter separators. |
66 | | - """ |
67 | | - if not params_str.strip(): |
68 | | - return params_str |
69 | | - |
70 | | - # Split on top-level commas only (not commas inside [] or {}) |
71 | | - params, current, depth = [], [], 0 |
72 | | - for ch in params_str: |
73 | | - if ch in '[({': |
74 | | - depth += 1 |
75 | | - current.append(ch) |
76 | | - elif ch in '])}': |
77 | | - depth -= 1 |
78 | | - current.append(ch) |
79 | | - elif ch == ',' and depth == 0: |
80 | | - params.append(''.join(current).strip()) |
81 | | - current = [] |
82 | | - else: |
83 | | - current.append(ch) |
84 | | - if current: |
85 | | - params.append(''.join(current).strip()) |
86 | | - |
87 | | - # Keep only the name (everything before the first ':') |
88 | | - stripped = [] |
89 | | - for param in params: |
90 | | - colon = param.find(':') |
91 | | - stripped.append(param[:colon].rstrip() if colon != -1 else param) |
92 | | - return ', '.join(stripped) |
93 | | - |
94 | | - |
95 | | -def strip_typehints(dot_src): |
96 | | - """Remove type annotations from class/attribute/method labels. |
97 | | -
|
98 | | - Three-pass approach applied only to lines that carry an HTML label: |
99 | | -
|
100 | | - 1. Return types – strips ``: ReturnType`` that follows a closing ``)``. |
101 | | - e.g. ``filter(record: logging.LogRecord): bool`` → ``filter(record: logging.LogRecord)`` |
102 | | -
|
103 | | - 2. Parameter types – strips ``: Type`` from each parameter inside ``()``. |
104 | | - e.g. ``filter(record: logging.LogRecord)`` → ``filter(record)`` |
105 | | - Handles complex types such as ``dict[str, IssueRecord]`` correctly. |
106 | | -
|
107 | | - 3. Attribute types – strips `` : Type`` from plain field entries. |
108 | | - e.g. ``initial_threshold : int`` → ``initial_threshold`` |
109 | | - """ |
110 | | - lines = dot_src.splitlines() |
111 | | - out_lines = [] |
112 | | - |
113 | | - for line in lines: |
114 | | - if 'label=<' not in line: |
115 | | - out_lines.append(line) |
116 | | - continue |
117 | | - |
118 | | - # Pass 1 – return types: ): ReturnType<br → )<br |
119 | | - line = re.sub(r'(?<=\)):\s*[^<}]+(?=<br|}>)', '', line) |
120 | | - |
121 | | - # Pass 2 – parameter types inside () |
122 | | - def _replace_params(m): |
123 | | - return '(' + _strip_param_types(m.group(1)) + ')' |
124 | | - line = re.sub(r'\(([^)]*)\)', _replace_params, line) |
125 | | - |
126 | | - # Pass 3 – attribute types: name : Type<br → name<br |
127 | | - line = re.sub(r'\s*:\s*[^<}]+(?=<br|}>)', '', line) |
128 | | - |
129 | | - out_lines.append(line) |
130 | | - |
131 | | - return '\n'.join(out_lines) |
132 | | - |
133 | | - |
134 | 63 | def patch_dot(dot_src, concise=False): |
135 | 64 | """Rewrite .dot source to apply a clean UML style.""" |
136 | 65 | s = STYLE |
|
0 commit comments