|
2 | 2 |
|
3 | 3 |
|
4 | 4 | def normalize_path(path: str, strip_extension: bool = False) -> str: |
5 | | - """Normalize a path to the standard API format: path/to/resource, where resource can be a file or a directory.""" |
| 5 | + """Normalize a path to the standard Humanloop API format. |
| 6 | +
|
| 7 | + This function is primarily used when interacting with the Humanloop API to ensure paths |
| 8 | + follow the standard format: 'path/to/resource' without leading/trailing slashes. |
| 9 | + It's used in several contexts: |
| 10 | + 1. When pulling files from Humanloop to local filesystem (see SyncClient.pull) |
| 11 | + 2. When making API calls that reference files by path |
| 12 | + 3. When handling local file operations that need to match API path conventions |
| 13 | +
|
| 14 | + The function: |
| 15 | + - Converts Windows backslashes to forward slashes |
| 16 | + - Normalizes consecutive slashes |
| 17 | + - Optionally strips file extensions (e.g. .prompt, .agent) |
| 18 | + - Removes leading/trailing slashes to match API conventions |
| 19 | +
|
| 20 | + Leading/trailing slashes are stripped because the Humanloop API expects paths in the |
| 21 | + format 'path/to/resource' without them. This is consistent with how the API stores |
| 22 | + and references files, and ensures paths work correctly in both API calls and local |
| 23 | + filesystem operations. |
| 24 | +
|
| 25 | + Args: |
| 26 | + path: The path to normalize. Can be a Windows or Unix-style path. |
| 27 | + strip_extension: If True, removes the file extension (e.g. .prompt, .agent) |
| 28 | +
|
| 29 | + Returns: |
| 30 | + Normalized path string in the format 'path/to/resource' |
| 31 | +
|
| 32 | + Examples: |
| 33 | + >>> normalize_path("path/to/file.prompt") |
| 34 | + 'path/to/file.prompt' |
| 35 | + >>> normalize_path("path/to/file.prompt", strip_extension=True) |
| 36 | + 'path/to/file' |
| 37 | + >>> normalize_path("\\windows\\style\\path.prompt") |
| 38 | + 'windows/style/path.prompt' |
| 39 | + >>> normalize_path("/leading/slash/path/") |
| 40 | + 'leading/slash/path' |
| 41 | + >>> normalize_path("multiple//slashes//path") |
| 42 | + 'multiple/slashes/path' |
| 43 | + """ |
6 | 44 | # Handle backslashes for Windows paths before passing to PurePosixPath |
7 | 45 | # This is needed because some backslash sequences are treated as escape chars |
8 | 46 | path = path.replace("\\", "/") |
|
0 commit comments