|
| 1 | +# PrivatePath – `vcspull._internal.private_path` |
| 2 | + |
| 3 | +:::{warning} |
| 4 | +`PrivatePath` is an internal helper. Its import path and behavior may change |
| 5 | +without notice. File an issue if you rely on it downstream so we can discuss a |
| 6 | +supported API. |
| 7 | +::: |
| 8 | + |
| 9 | +`PrivatePath` subclasses `pathlib.Path` and normalizes every textual rendering |
| 10 | +(`str()`/`repr()`) so the current user’s home directory is collapsed to `~`. |
| 11 | +The class behaves exactly like the standard path object for filesystem ops; it |
| 12 | +only alters how the path is displayed. This keeps CLI logs, JSON/NDJSON output, |
| 13 | +and tests from leaking usernames while preserving full absolute paths for |
| 14 | +internal logic. |
| 15 | + |
| 16 | +```python |
| 17 | +from vcspull._internal.private_path import PrivatePath |
| 18 | + |
| 19 | +home_repo = PrivatePath("~/code/vcspull") |
| 20 | +print(home_repo) # -> ~/code/vcspull |
| 21 | +print(repr(home_repo)) # -> "PrivatePath('~/code/vcspull')" |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage guidelines |
| 25 | + |
| 26 | +- Wrap any path destined for user-facing output (logs, console tables, JSON |
| 27 | + payloads) in `PrivatePath` before calling `str()`. |
| 28 | +- The helper is safe to instantiate with `pathlib.Path` objects or strings; it |
| 29 | + does not touch relative paths that lack a home prefix. |
| 30 | +- Prefer storing raw `pathlib.Path` objects (or strings) in configuration |
| 31 | + models, then convert to `PrivatePath` at the presentation layer. This keeps |
| 32 | + serialization and equality checks deterministic while still masking the home |
| 33 | + directory when needed. |
| 34 | + |
| 35 | +## Why not `contract_user_home`? |
| 36 | + |
| 37 | +The previous `contract_user_home()` helper duplicated the tilde-collapsing logic |
| 38 | +in multiple modules and required callers to remember to run it themselves. By |
| 39 | +centralizing the behavior in a `pathlib.Path` subclass we get: |
| 40 | + |
| 41 | +- Built-in protection—`str()` and `repr()` automatically apply the privacy |
| 42 | + filter. |
| 43 | +- Consistent behavior across every CLI command and test fixture. |
| 44 | +- Easier mocking in tests, because `PrivatePath` respects monkeypatched |
| 45 | + `Path.home()` implementations. |
| 46 | + |
| 47 | +If you need alternative redaction behavior, consider composing your own helper |
| 48 | +around `PrivatePath` instead of reintroducing ad hoc string munging. |
| 49 | + |
| 50 | +```{eval-rst} |
| 51 | +.. automodule:: vcspull._internal.private_path |
| 52 | + :members: |
| 53 | + :show-inheritance: |
| 54 | + :undoc-members: |
| 55 | +``` |
0 commit comments