Skip to content

Commit

Permalink
Delinted paths
Browse files Browse the repository at this point in the history
  • Loading branch information
DiddiZ committed Feb 22, 2024
1 parent 007dc90 commit c82820a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.9, "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install package
run: |
python -m pip install .[dev]
- name: Test with pytest
run: |
python -m pytest
python -m pytest tests/
2 changes: 1 addition & 1 deletion mtgproxies/decklists/decklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.:
Expand Down
8 changes: 4 additions & 4 deletions mtgproxies/plotting/splitpages.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from pathlib import Path
from types import TracebackType

import matplotlib.pyplot as plt
Expand All @@ -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:
Expand All @@ -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
14 changes: 8 additions & 6 deletions mtgproxies/print_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
43 changes: 26 additions & 17 deletions tests/print_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c82820a

Please sign in to comment.