Skip to content

Commit

Permalink
Maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
DiddiZ committed Dec 18, 2024
1 parent 1d11104 commit afb4df2
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
37 changes: 34 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
Expand Down Expand Up @@ -50,6 +49,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
Expand All @@ -72,6 +72,7 @@ instance/
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
Expand All @@ -82,7 +83,9 @@ profile_default/
ipython_config.py

# pyenv
.python-version
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
Expand All @@ -91,7 +94,22 @@ ipython_config.py
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
Expand Down Expand Up @@ -127,3 +145,16 @@ dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-merge-conflict
- id: check-executables-have-shebangs
- id: check-shebang-scripts-are-executable
- id: end-of-file-fixer
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
rev: v3.19.1
hooks:
- id: pyupgrade
args:
- "--py39-plus"
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.12.1
rev: 24.10.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
rev: 7.1.1
hooks:
- id: flake8
additional_dependencies:
Expand All @@ -48,7 +48,7 @@ repos:
- "--tab-width"
- "2"
- repo: https://github.com/shssoichiro/oxipng
rev: v9.0.0
rev: v9.1.3
hooks:
- id: oxipng
args:
Expand Down
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"python.testing.pytestArgs": ["tests"],
"python.testing.pytestEnabled": true,
"git.rebaseWhenSync": true,
"notebook.formatOnSave.enabled": true,
"files.eol": "\n",
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
Expand Down
2 changes: 1 addition & 1 deletion mtgproxies/decklists/decklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def extend(self, other: Decklist) -> None:
"""Append another decklist to this."""
self.entries.extend(other.entries)

def save(self, file: str | Path, fmt="arena", mode="w") -> None:
def save(self, file: str | Path, fmt: str = "arena", mode: str = "w") -> None:
"""Write decklist to a file.
Args:
Expand Down
4 changes: 3 additions & 1 deletion mtgproxies/decklists/manastack/manastack.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

from collections.abc import Sequence

import requests

from mtgproxies.decklists import Decklist
from mtgproxies.decklists.sanitizing import validate_card_name, validate_print


def parse_decklist(manastack_id: str, zones: list[str] = ["commander", "mainboard"]):
def parse_decklist(manastack_id: str, zones: Sequence[str] = ("commander", "mainboard")):
"""Parse a decklist from manastack.
Args:
Expand Down
24 changes: 10 additions & 14 deletions mtgproxies/decklists/sanitizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ def validate_print(card_name: str, set_id: str, collector_number: str):
if card is None: # No exact match
# Find alternative print
card = scryfall.recommend_print(card_name=card_name)
warnings.append(
(
"WARNING",
f"Unable to find scan of {format_print(card_name, set_id, collector_number)}."
+ f" Using {format_print(card)} instead.",
)
)
warnings.append((
"WARNING",
f"Unable to find scan of {format_print(card_name, set_id, collector_number)}."
+ f" Using {format_print(card)} instead.",
))

# Warnings for low-quality scans
quality_warnings = get_print_warnings(card)
Expand All @@ -110,11 +108,9 @@ def validate_print(card_name: str, set_id: str, collector_number: str):
# Format warnings string
quality_warnings = listing(quality_warnings, ", ", " and ").capitalize()

warnings.append(
(
"COSMETIC",
f"{quality_warnings} for {format_print(card)}."
+ (f" Maybe you want {format_print(recommendation)}?" if recommendation != card else ""),
)
)
warnings.append((
"COSMETIC",
f"{quality_warnings} for {format_print(card)}."
+ (f" Maybe you want {format_print(recommendation)}?" if recommendation != card else ""),
))
return card, warnings
1 change: 1 addition & 0 deletions mtgproxies/format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""String formatting utils."""

from __future__ import annotations


Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[tool.black]
line-length = 120
target-version = ['py39', 'py310', 'py311', 'py312']
target-version = ['py39', 'py310', 'py311', 'py312', 'py313']
preview = true
enable-unstable-feature = ["hug_parens_with_braces_and_square_brackets"]
43 changes: 27 additions & 16 deletions scryfall/scryfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
See:
https://scryfall.com/docs/api
"""

from __future__ import annotations

import json
import pickle
import threading
from collections import defaultdict
from functools import lru_cache
Expand Down Expand Up @@ -62,13 +64,16 @@ def download(url: str, dst, chunk_size: int = 1024 * 4, silent: bool = False):
with requests.get(url, stream=True) as req:
req.raise_for_status()
file_size = int(req.headers["Content-Length"]) if "Content-Length" in req.headers else None
with open(dst, "xb") as f, tqdm(
total=file_size,
unit="B",
unit_scale=True,
desc=url.split("/")[-1],
disable=silent,
) as pbar:
with (
open(dst, "xb") as f,
tqdm(
total=file_size,
unit="B",
unit_scale=True,
desc=url.split("/")[-1],
disable=silent,
) as pbar,
):
for chunk in req.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
Expand Down Expand Up @@ -115,9 +120,17 @@ def _get_database(database_name: str = "default_cards"):
if len(bulk_data) != 1:
raise ValueError(f"Unknown database {database_name}")

bulk_file = get_file(bulk_data[0]["download_uri"].split("/")[-1], bulk_data[0]["download_uri"])
with open(bulk_file, encoding="utf-8") as json_file:
return json.load(json_file)
bulk_file = Path(get_file(bulk_data[0]["download_uri"].split("/")[-1], bulk_data[0]["download_uri"]))
pickle_file = bulk_file.with_suffix(".pickle")
if not pickle_file.is_file(): # Convert json to pickle
with open(bulk_file, encoding="utf-8") as json_file:
data = json.load(json_file)
with open(pickle_file, "wb") as pickle_file:
pickle.dump(data, pickle_file, protocol=pickle.HIGHEST_PROTOCOL)
return data
else:
with open(pickle_file, "rb") as f:
return pickle.load(f)


def canonic_card_name(card_name: str) -> str:
Expand Down Expand Up @@ -243,12 +256,10 @@ def score(card: dict):
# Return all card in descending order
return recommendations
elif mode == "choices":
artworks = np.array(
[
get_faces(card)[0]["illustration_id"] if "illustration_id" in get_faces(card)[0] else card["id"]
for card in alternatives
] # Not all cards have illustrations, use id instead
)
artworks = np.array([
get_faces(card)[0]["illustration_id"] if "illustration_id" in get_faces(card)[0] else card["id"]
for card in alternatives
]) # Not all cards have illustrations, use id instead
choices = []
for artwork in set(artworks):
artwork_alternatives = np.array(alternatives)[artworks == artwork]
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ known-modules = mtg-proxies:[mtgproxies,scryfall],fpdf2:[fpdf]
[isort]
line_length = 120
profile = black
known_first_party=
known_local_folder=mtgproxies,scryfall

0 comments on commit afb4df2

Please sign in to comment.