mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Extract Location into separate file
This commit is contained in:
@ -78,41 +78,6 @@ def prompt_yes_no(question: str, default: Optional[bool] = None) -> bool:
|
||||
print(wrong_reply)
|
||||
|
||||
|
||||
class ResolveException(Exception):
|
||||
"""An exception while resolving a file."""
|
||||
|
||||
|
||||
class Location:
|
||||
"""
|
||||
An object that has an inherent path.
|
||||
"""
|
||||
|
||||
def __init__(self, path: Path):
|
||||
self._path = path.resolve()
|
||||
|
||||
@property
|
||||
def path(self) -> Path:
|
||||
"""
|
||||
This object's location.
|
||||
"""
|
||||
|
||||
return self._path
|
||||
|
||||
def resolve(self, target: Path) -> Path:
|
||||
"""
|
||||
Resolve a file relative to the path of this location.
|
||||
|
||||
Raises a [ResolveException] if the file is outside the given directory.
|
||||
"""
|
||||
absolute_path = self.path.joinpath(target).resolve()
|
||||
|
||||
# TODO Make this less inefficient
|
||||
if self.path not in absolute_path.parents:
|
||||
raise ResolveException(f"Path {target} is not inside directory {self.path}")
|
||||
|
||||
return absolute_path
|
||||
|
||||
|
||||
class PrettyLogger:
|
||||
"""
|
||||
A logger that prints some specially formatted log messages in color.
|
||||
|
Reference in New Issue
Block a user