From f776186480bd4f1955edbcc54365379ee0478e00 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 29 Apr 2021 16:52:00 +0200 Subject: [PATCH] Use PurePath instead of Path Path should only be used when we need to access the file system. For all other purposes (mainly crawling), we use PurePath instead since the paths don't correspond to paths in the local file system. --- PFERD/crawler.py | 9 +++++---- PFERD/crawlers/dummy.py | 6 +++--- PFERD/transformer.py | 26 +++++++++++++------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/PFERD/crawler.py b/PFERD/crawler.py index 376cada..9ceca20 100644 --- a/PFERD/crawler.py +++ b/PFERD/crawler.py @@ -1,7 +1,7 @@ import configparser from abc import ABC, abstractmethod from contextlib import asynccontextmanager -from pathlib import Path +from pathlib import PurePath # TODO In Python 3.9 and above, AsyncContextManager is deprecated from typing import AsyncContextManager, AsyncIterator, Optional @@ -38,7 +38,8 @@ class Crawler(ABC): e.pretty_print() raise CrawlerLoadException() - # output_dir = Path(section.get("output_dir", name)) + # working_dir = Path(section.get("working_dir", "")) + # output_dir = working_dir / section.get("output_dir", name) def print(self, text: str) -> None: """ @@ -75,14 +76,14 @@ class Crawler(ABC): with self._conductor.progress_bar(desc, total=total) as bar: yield bar - def crawl_bar(self, path: Path) -> AsyncContextManager[ProgressBar]: + def crawl_bar(self, path: PurePath) -> AsyncContextManager[ProgressBar]: pathstr = escape(str(path)) desc = f"[bold magenta]Crawling[/bold magenta] {pathstr}" return self.progress_bar(desc) def download_bar( self, - path: Path, + path: PurePath, size: int, ) -> AsyncContextManager[ProgressBar]: pathstr = escape(str(path)) diff --git a/PFERD/crawlers/dummy.py b/PFERD/crawlers/dummy.py index 46a7a69..204b4b1 100644 --- a/PFERD/crawlers/dummy.py +++ b/PFERD/crawlers/dummy.py @@ -1,6 +1,6 @@ import asyncio import random -from pathlib import Path +from pathlib import PurePath from typing import Any from rich.markup import escape @@ -37,9 +37,9 @@ DUMMY_TREE = { class DummyCrawler(Crawler): async def crawl(self) -> None: - await self._crawl_entry(Path(), DUMMY_TREE) + await self._crawl_entry(PurePath(), DUMMY_TREE) - async def _crawl_entry(self, path: Path, value: Any) -> None: + async def _crawl_entry(self, path: PurePath, value: Any) -> None: if value is True: async with self.exclusive_output(): await ainput(f"File {path}, please press enter: ") diff --git a/PFERD/transformer.py b/PFERD/transformer.py index 1ecaf19..298c580 100644 --- a/PFERD/transformer.py +++ b/PFERD/transformer.py @@ -1,22 +1,22 @@ import re from abc import ABC, abstractmethod from dataclasses import dataclass -from pathlib import Path +from pathlib import PurePath from typing import Dict, Optional, Union class Rule(ABC): @abstractmethod - def transform(self, path: Path) -> Optional[Path]: + def transform(self, path: PurePath) -> Optional[PurePath]: pass class NormalRule(Rule): - def __init__(self, left: Path, right: Path): + def __init__(self, left: PurePath, right: PurePath): self._left = left self._right = right - def _match_prefix(self, path: Path) -> Optional[Path]: + def _match_prefix(self, path: PurePath) -> Optional[PurePath]: left_parts = list(reversed(self._left.parts)) path_parts = list(reversed(path.parts)) @@ -33,9 +33,9 @@ class NormalRule(Rule): if left_parts: return None - return Path(*path_parts) + return PurePath(*path_parts) - def transform(self, path: Path) -> Optional[Path]: + def transform(self, path: PurePath) -> Optional[PurePath]: if rest := self._match_prefix(path): return self._right / rest @@ -43,11 +43,11 @@ class NormalRule(Rule): class ExactRule(Rule): - def __init__(self, left: Path, right: Path): + def __init__(self, left: PurePath, right: PurePath): self._left = left self._right = right - def transform(self, path: Path) -> Optional[Path]: + def transform(self, path: PurePath) -> Optional[PurePath]: if path == self._left: return self._right @@ -59,7 +59,7 @@ class ReRule(Rule): self._left = left self._right = right - def transform(self, path: Path) -> Optional[Path]: + def transform(self, path: PurePath) -> Optional[PurePath]: if match := re.fullmatch(self._left, str(path)): kwargs: Dict[str, Union[int, float]] = {} @@ -75,7 +75,7 @@ class ReRule(Rule): except ValueError: pass - return Path(self._right.format(*groups, **kwargs)) + return PurePath(self._right.format(*groups, **kwargs)) return None @@ -208,9 +208,9 @@ def parse_rule(line: Line) -> Rule: right = parse_string(line) if arrowname == "": - return NormalRule(Path(left), Path(right)) + return NormalRule(PurePath(left), PurePath(right)) elif arrowname == "exact": - return ExactRule(Path(left), Path(right)) + return ExactRule(PurePath(left), PurePath(right)) elif arrowname == "re": return ReRule(left, right) else: @@ -230,7 +230,7 @@ class Transformer: if line: self._rules.append(parse_rule(Line(line, i))) - def transform(self, path: Path) -> Optional[Path]: + def transform(self, path: PurePath) -> Optional[PurePath]: for rule in self._rules: if result := rule.transform(path): return result