From 41ddaa2e7c253f6489ae607da4e810b1faf5db5f Mon Sep 17 00:00:00 2001 From: Robin Kupper Date: Thu, 22 Feb 2024 21:02:05 +0100 Subject: [PATCH] Delinted paths --- mtgproxies/decklists/decklist.py | 2 +- mtgproxies/plotting/splitpages.py | 8 +++--- mtgproxies/print_cards.py | 14 +++++----- tests/print_test.py | 43 +++++++++++++++++++------------ 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/mtgproxies/decklists/decklist.py b/mtgproxies/decklists/decklist.py index 69d8e3f..127c835 100644 --- a/mtgproxies/decklists/decklist.py +++ b/mtgproxies/decklists/decklist.py @@ -117,7 +117,7 @@ def from_scryfall_ids(card_ids) -> Decklist: return decklist -def parse_decklist(filepath) -> tuple[Decklist, bool, list]: +def parse_decklist(filepath: str | Path) -> tuple[Decklist, bool, list]: """Parse card information from a decklist in text or MtG Arena (or mixed) format. E.g.: diff --git a/mtgproxies/plotting/splitpages.py b/mtgproxies/plotting/splitpages.py index a8d3a1a..9a23a10 100644 --- a/mtgproxies/plotting/splitpages.py +++ b/mtgproxies/plotting/splitpages.py @@ -1,5 +1,6 @@ from __future__ import annotations +from pathlib import Path from types import TracebackType import matplotlib.pyplot as plt @@ -11,10 +12,9 @@ class SplitPages: This mirrors the functionality of the `PdfPages` wrapper from matplotlib. """ - def __init__(self, filename: str) -> None: + def __init__(self, filename: Path | str) -> None: """Create a new SplitPages object.""" - self.file_basename = filename[: filename.rindex(".")] - self.file_extension = filename[filename.rindex(".") :] + self.filename = Path(filename) self.pagecount = 0 def __enter__(self) -> SplitPages: @@ -36,6 +36,6 @@ def savefig(self, figure=None, **kwargs): if figure is None: figure = plt.gcf() - filename = self.file_basename + f"_{self.pagecount:03}" + self.file_extension + filename = self.filename.parent / f"{self.filename.stem}_{self.pagecount:03}{self.filename.suffix}" plt.savefig(filename, **kwargs) self.pagecount += 1 diff --git a/mtgproxies/print_cards.py b/mtgproxies/print_cards.py index 93ae6e4..e17a5e6 100644 --- a/mtgproxies/print_cards.py +++ b/mtgproxies/print_cards.py @@ -19,11 +19,11 @@ def _occupied_space(cardsize, pos, border_crop: int, closed: bool = False): def print_cards_matplotlib( images, - filepath, + filepath: str | Path, papersize=np.array([8.27, 11.69]), cardsize=np.array([2.5, 3.5]), border_crop: int = 14, - interpolation="lanczos", + interpolation: str | None = "lanczos", dpi: int = 600, background_color=None, ): @@ -43,10 +43,11 @@ def print_cards_matplotlib( offset = (papersize - _occupied_space(cardsize, N, border_crop, closed=True)) / 2 # Ensure directory exists - Path(filepath).parent.mkdir(parents=True, exist_ok=True) + filepath = Path(filepath) + filepath.parent.mkdir(parents=True, exist_ok=True) # Choose pdf of image saver - if filepath[-4:] == ".pdf": + if filepath.suffix == ".pdf": saver = PdfPages else: saver = SplitPages @@ -98,7 +99,7 @@ def print_cards_matplotlib( def print_cards_fpdf( images, - filepath, + filepath: str | Path, papersize=np.array([210, 297]), cardsize=np.array([2.5 * 25.4, 3.5 * 25.4]), border_crop: int = 14, @@ -124,7 +125,8 @@ def print_cards_fpdf( offset = (papersize - _occupied_space(cardsize, N, border_crop, closed=True)) / 2 # Ensure directory exists - Path(filepath).parent.mkdir(parents=True, exist_ok=True) + filepath = Path(filepath) + filepath.parent.mkdir(parents=True, exist_ok=True) # Initialize PDF pdf = FPDF(orientation="P", unit="mm", format="A4") diff --git a/tests/print_test.py b/tests/print_test.py index e8bc4f5..835dfdb 100644 --- a/tests/print_test.py +++ b/tests/print_test.py @@ -1,32 +1,41 @@ from pathlib import Path -from tempfile import TemporaryDirectory +import pytest -def test_print_pdf(): - from mtgproxies import fetch_scans_scryfall, print_cards_fpdf + +@pytest.fixture(scope="module") +def example_images() -> list[str]: + from mtgproxies import fetch_scans_scryfall from mtgproxies.decklists import parse_decklist - decklist, _, _ = parse_decklist("examples/decklist.txt") + decklist, _, _ = parse_decklist(Path(__file__).parent.parent / "examples/decklist.txt") images = fetch_scans_scryfall(decklist) - with TemporaryDirectory() as dir: - out_file = Path(dir) / "decklist.pdf" + return images - print_cards_fpdf(images, out_file) - assert out_file.is_file() +def test_print_cards_fpdf(example_images: list[str], tmp_path: Path): + from mtgproxies import print_cards_fpdf + out_file = tmp_path / "decklist.pdf" + print_cards_fpdf(example_images, out_file) -def test_print_png(): - from mtgproxies import fetch_scans_scryfall, print_cards_matplotlib - from mtgproxies.decklists import parse_decklist + assert out_file.is_file() + + +def test_print_cards_matplotlib_pdf(example_images: list[str], tmp_path: Path): + from mtgproxies import print_cards_matplotlib + + out_file = tmp_path / "decklist.pdf" + print_cards_matplotlib(example_images, out_file) + + assert out_file.is_file() - decklist, _, _ = parse_decklist("examples/decklist.txt") - images = fetch_scans_scryfall(decklist) - with TemporaryDirectory() as dir: - out_file = Path(dir) / "decklist.png" +def test_print_cards_matplotlib_png(example_images: list[str], tmp_path: Path): + from mtgproxies import print_cards_matplotlib - print_cards_matplotlib(images, str(out_file)) + out_file = tmp_path / "decklist.png" + print_cards_matplotlib(example_images, out_file) - assert (Path(dir) / "decklist_000.png").is_file() + assert (tmp_path / "decklist_000.png").is_file()