mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2024-09-19 14:48:38 +03:00
3b084a165b
* configure isort to add the future annotations import * apply the new isort setting * remove type ignores for new mypy (#13539) https://pypi.org/project/mypy/0.981/ * another
30 lines
702 B
Python
30 lines
702 B
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Union
|
|
|
|
|
|
def path_from_root(root: Path, path_str: Union[str, Path]) -> Path:
|
|
"""
|
|
If path is relative, prepend root
|
|
If path is absolute, return it directly.
|
|
"""
|
|
root = Path(os.path.expanduser(str(root)))
|
|
path = Path(path_str)
|
|
if not path.is_absolute():
|
|
path = root / path
|
|
return path.resolve()
|
|
|
|
|
|
def make_path_relative(path_str: Union[str, Path], root: Path) -> Path:
|
|
"""
|
|
Try to make the given path relative, given the default root.
|
|
"""
|
|
path = Path(path_str)
|
|
try:
|
|
path = path.relative_to(root)
|
|
except ValueError:
|
|
pass
|
|
return path
|