Files
MTGC/collection_manager.py

39 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# collection_manager.py
import os
import json
COLLECTION_FILE = os.path.join("data", "collection.json")
def load_collection() -> dict[str, int]:
"""
Returns a dict of card_name → quantity in your collection.
If the file doesnt exist, returns an empty dict.
"""
if not os.path.isdir(os.path.dirname(COLLECTION_FILE)):
os.makedirs(os.path.dirname(COLLECTION_FILE), exist_ok=True)
if not os.path.isfile(COLLECTION_FILE):
return {}
try:
with open(COLLECTION_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
return {str(k): int(v) for k, v in data.items()}
except json.JSONDecodeError:
return {}
def save_collection(collection: dict[str, int]) -> None:
"""
Writes your collection (card_name → quantity) to disk.
"""
if not os.path.isdir(os.path.dirname(COLLECTION_FILE)):
os.makedirs(os.path.dirname(COLLECTION_FILE), exist_ok=True)
with open(COLLECTION_FILE, "w", encoding="utf-8") as f:
json.dump(collection, f, indent=2)
def list_collection() -> list[tuple[str, int]]:
"""
Returns a sorted list of (card_name, qty) from your collection.
"""
coll = load_collection()
return sorted(coll.items(), key=lambda x: x[0].lower())