|
| 1 | +# ---------------------------------------------------------------------- |
| 2 | +# Path utilities |
| 3 | +# ---------------------------------------------------------------------- |
| 4 | +# Copyright (C) 2007-2025 Gufo Labs |
| 5 | +# See LICENSE for details |
| 6 | +# ---------------------------------------------------------------------- |
| 7 | + |
| 8 | +# Python modules |
| 9 | +from pathlib import Path |
| 10 | +from typing import Optional |
| 11 | +import re |
| 12 | + |
| 13 | +# NOC modules |
| 14 | +from .text import cyr_to_lat |
| 15 | + |
| 16 | +_rx_safe_path = re.compile(r"[^a-z0-9\-\+]+", re.IGNORECASE) |
| 17 | + |
| 18 | + |
| 19 | +def _clean_component(s: str) -> str: |
| 20 | + """ |
| 21 | + Clean path component. |
| 22 | +
|
| 23 | + Args: |
| 24 | + s: Input string. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + Cleaned output. |
| 28 | + """ |
| 29 | + r = cyr_to_lat(s) |
| 30 | + r = _rx_safe_path.sub(" ", r).strip() |
| 31 | + return r.replace(" ", "_") |
| 32 | + |
| 33 | + |
| 34 | +def safe_path(*args: str, sep: Optional[str] = None, suffix: Optional[str] = None) -> Path: |
| 35 | + """ |
| 36 | + Expand path components to safe relative path. |
| 37 | +
|
| 38 | + Suitable for collections. Unreadable or dangerous symbols are replaced |
| 39 | + with safe ones. |
| 40 | +
|
| 41 | + Examples: |
| 42 | +
|
| 43 | + ``` python |
| 44 | + safe_path(name, suffix=".json") |
| 45 | + ``` |
| 46 | +
|
| 47 | + ``` python |
| 48 | + safe_path(name, sep="|", suffix=".json") |
| 49 | + ``` |
| 50 | +
|
| 51 | + ``` python |
| 52 | + safe_path(vendor, profile, suffix=".json") |
| 53 | + ``` |
| 54 | +
|
| 55 | + Args: |
| 56 | + args: Path components. |
| 57 | + sep: Separator, if set, every `args` element is split by `sep`. |
| 58 | + suffix: If set, append suffix to filename. Suffix doesn't include |
| 59 | + dot, by default, so i.e. `.json` form must be used, rather than |
| 60 | + `json`. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + Formatted path component |
| 64 | + """ |
| 65 | + if sep: |
| 66 | + components = [] |
| 67 | + for arg in args: |
| 68 | + components.extend(arg.split(sep)) |
| 69 | + else: |
| 70 | + components = list(args) |
| 71 | + components = [_clean_component(x) for x in components] |
| 72 | + path = Path(*(x for x in components if x)) |
| 73 | + if suffix: |
| 74 | + path = path.with_suffix(suffix) |
| 75 | + return path |
| 76 | + |
| 77 | + |
| 78 | +def safe_json_path(*args: str, sep: Optional[str] = "|", suffix: Optional[str] = ".json") -> Path: |
| 79 | + """ |
| 80 | + Expand path components to JSON file path. |
| 81 | +
|
| 82 | + Args: |
| 83 | + args: Path components. |
| 84 | + sep: Separator, if set, every `args` element is split by `sep`. |
| 85 | + suffix: If set, append suffix to filename. Suffix doesn't include |
| 86 | + dot, by default, so i.e. `.json` form must be used, rather than |
| 87 | + `json`. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + Formatted path component |
| 91 | + """ |
| 92 | + return safe_path(*args, sep=sep, suffix=suffix) |
0 commit comments