MCPcopy Create free account
hub / github.com/cocoindex-io/cocoindex-code / _parse_path_mapping

Function _parse_path_mapping

src/cocoindex_code/settings.py:172–197  ·  view source on GitHub ↗

Parse a ``source=target[,source=target...]`` env var. Both source and target must be absolute paths. Returns an empty list when the env var is unset or blank. Raises ``ValueError`` on malformed entries.

(env_var: str)

Source from the content-addressed store, hash-verified

170
171
172def _parse_path_mapping(env_var: str) -> list[PathMapping]:
173 """Parse a ``source=target[,source=target...]`` env var.
174
175 Both source and target must be absolute paths. Returns an empty list when
176 the env var is unset or blank. Raises ``ValueError`` on malformed entries.
177 """
178 raw = os.environ.get(env_var, "")
179 if not raw.strip():
180 return []
181
182 mappings: list[PathMapping] = []
183 for entry in raw.split(","):
184 entry = entry.strip()
185 if not entry:
186 continue
187 parts = entry.split("=", 1)
188 if len(parts) != 2 or not parts[0] or not parts[1]:
189 raise ValueError(f"{env_var}: invalid entry {entry!r}, expected format 'source=target'")
190 source = Path(parts[0])
191 target = Path(parts[1])
192 if not source.is_absolute():
193 raise ValueError(f"{env_var}: source path must be absolute, got {source!r}")
194 if not target.is_absolute():
195 raise ValueError(f"{env_var}: target path must be absolute, got {target!r}")
196 mappings.append(PathMapping(source=source.resolve(), target=target.resolve()))
197 return mappings
198
199
200def _apply_mapping(mappings: list[PathMapping], path: str | Path, reverse: bool = False) -> str:

Callers 3

resolve_db_dirFunction · 0.85
get_db_path_mappingsFunction · 0.85
get_host_path_mappingsFunction · 0.85

Calls 1

PathMappingClass · 0.85

Tested by

no test coverage detected