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)
| 242 | |
| 243 | |
| 244 | def expand_user(path:str) -> Tuple[str, bool, str]: |
| 245 | """Expand ``~``-style usernames in strings. |
| 246 | |
| 247 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
| 248 | extra information that will be useful if the input was being used in |
| 249 | computing completions, and you wish to return the completions with the |
| 250 | original '~' instead of its expanded value. |
| 251 | |
| 252 | Parameters |
| 253 | ---------- |
| 254 | path : str |
| 255 | String to be expanded. If no ~ is present, the output is the same as the |
| 256 | input. |
| 257 | |
| 258 | Returns |
| 259 | ------- |
| 260 | newpath : str |
| 261 | Result of ~ expansion in the input path. |
| 262 | tilde_expand : bool |
| 263 | Whether any expansion was performed or not. |
| 264 | tilde_val : str |
| 265 | The value that ~ was replaced with. |
| 266 | """ |
| 267 | # Default values |
| 268 | tilde_expand = False |
| 269 | tilde_val = '' |
| 270 | newpath = path |
| 271 | |
| 272 | if path.startswith('~'): |
| 273 | tilde_expand = True |
| 274 | rest = len(path)-1 |
| 275 | newpath = os.path.expanduser(path) |
| 276 | if rest: |
| 277 | tilde_val = newpath[:-rest] |
| 278 | else: |
| 279 | tilde_val = newpath |
| 280 | |
| 281 | return newpath, tilde_expand, tilde_val |
| 282 | |
| 283 | |
| 284 | def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str: |
no outgoing calls
no test coverage detected