diff --git a/PFERD/location.py b/PFERD/location.py new file mode 100644 index 0000000..8ad5cc7 --- /dev/null +++ b/PFERD/location.py @@ -0,0 +1,37 @@ +from pathlib import Path, PurePath + + +class ResolveException(Exception): + """An exception while resolving a file.""" + # TODO take care of this when doing exception handling + + +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: PurePath) -> 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 diff --git a/PFERD/organizer.py b/PFERD/organizer.py index 2494abe..20c507a 100644 --- a/PFERD/organizer.py +++ b/PFERD/organizer.py @@ -9,7 +9,8 @@ import shutil from pathlib import Path from typing import List, Set -from .utils import Location, PrettyLogger, prompt_yes_no +from .location import Location +from .utils import PrettyLogger, prompt_yes_no LOGGER = logging.getLogger(__name__) PRETTY = PrettyLogger(LOGGER) diff --git a/PFERD/pferd.py b/PFERD/pferd.py index 8b3d43e..cda28eb 100644 --- a/PFERD/pferd.py +++ b/PFERD/pferd.py @@ -5,10 +5,10 @@ from .cookie_jar import CookieJar from .ilias import (IliasAuthenticator, IliasCrawler, IliasDirectoryFilter, IliasDownloader, KitShibbolethAuthenticator) from .ilias.download_strategies import DownloadStrategy, download_everything +from .location import Location from .organizer import Organizer from .tmp_dir import TmpDir from .transform import Transform, apply_transform -from .utils import Location # TODO save known-good cookies as soon as possible # TODO print synchronizer name before beginning synchronization diff --git a/PFERD/tmp_dir.py b/PFERD/tmp_dir.py index c634baa..51ade2d 100644 --- a/PFERD/tmp_dir.py +++ b/PFERD/tmp_dir.py @@ -6,7 +6,7 @@ from pathlib import Path from types import TracebackType from typing import Optional, Type -from .utils import Location +from .location import Location LOGGER = logging.getLogger(__name__) diff --git a/PFERD/utils.py b/PFERD/utils.py index 7d32213..d683010 100644 --- a/PFERD/utils.py +++ b/PFERD/utils.py @@ -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.