MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / _safe_filename

Function _safe_filename

codewiki/mcp/workspace.py:37–53  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

35_WORKSPACE_REL = Path(".codewiki") / "sessions"
36
37
38def _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

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected