Expand ``~``-style usernames in strings. This is similar to :func:`os.path.expanduser`, but it computes and returns extra information that will be useful if the input was being used in computing completions, and you wish to return the completions with the original '~' instead of its
(path: str)
| 369 | |
| 370 | |
| 371 | def expand_user(path: str) -> tuple[str, bool, str]: |
| 372 | """Expand ``~``-style usernames in strings. |
| 373 | |
| 374 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
| 375 | extra information that will be useful if the input was being used in |
| 376 | computing completions, and you wish to return the completions with the |
| 377 | original '~' instead of its expanded value. |
| 378 | |
| 379 | Parameters |
| 380 | ---------- |
| 381 | path : str |
| 382 | String to be expanded. If no ~ is present, the output is the same as the |
| 383 | input. |
| 384 | |
| 385 | Returns |
| 386 | ------- |
| 387 | newpath : str |
| 388 | Result of ~ expansion in the input path. |
| 389 | tilde_expand : bool |
| 390 | Whether any expansion was performed or not. |
| 391 | tilde_val : str |
| 392 | The value that ~ was replaced with. |
| 393 | """ |
| 394 | # Default values |
| 395 | tilde_expand = False |
| 396 | tilde_val = '' |
| 397 | newpath = path |
| 398 | |
| 399 | if path.startswith('~'): |
| 400 | tilde_expand = True |
| 401 | rest = len(path)-1 |
| 402 | newpath = os.path.expanduser(path) |
| 403 | if rest: |
| 404 | tilde_val = newpath[:-rest] |
| 405 | else: |
| 406 | tilde_val = newpath |
| 407 | |
| 408 | return newpath, tilde_expand, tilde_val |
| 409 | |
| 410 | |
| 411 | def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str: |
no outgoing calls
no test coverage detected
searching dependent graphs…