Sanitize a component ID for use as a filename. Component IDs look like ``src/main.py::MyClass``. We replace any character that is not a word char, hyphen, or dot with ``__``. A short hash suffix is appended to prevent collisions when different component IDs sanitize to the same str
(component_id: str)
| 35 | _WORKSPACE_REL = Path(".codewiki") / "sessions" |
| 36 | |
| 37 | |
| 38 | def _safe_filename(component_id: str) -> str: |
| 39 | """Sanitize a component ID for use as a filename. |
| 40 | |
| 41 | Component IDs look like ``src/main.py::MyClass``. We replace any |
| 42 | character that is not a word char, hyphen, or dot with ``__``. |
| 43 | A short hash suffix is appended to prevent collisions when different |
| 44 | component IDs sanitize to the same string (e.g. ``src/a-b.py`` vs |
| 45 | ``src/a_b.py``). |
| 46 | |
| 47 | The sanitized part is truncated so the filename stays under the |
| 48 | common 255-byte NAME_MAX (separator chars expand to two chars each, |
| 49 | so deep paths overflow it easily); the hash suffix keeps truncated |
| 50 | names unique. |
| 51 | """ |
| 52 | sanitized = re.sub(r"[^\w\-.]", "__", component_id)[:180] |
| 53 | hash_suffix = hashlib.sha1(component_id.encode()).hexdigest()[:8] |
| 54 | return f"{sanitized}_{hash_suffix}.src" |
| 55 | |
| 56 |
no outgoing calls
no test coverage detected