2021-08-17 15:37:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-03 05:11:46 +00:00
|
|
|
import json
|
2021-08-23 11:38:01 +00:00
|
|
|
import pickle
|
2021-09-03 05:11:46 +00:00
|
|
|
import zipfile
|
2021-08-16 12:45:15 +00:00
|
|
|
|
2021-08-23 14:55:41 +00:00
|
|
|
from .card import Card
|
|
|
|
from .cards import Cards
|
|
|
|
from .code import Code
|
2021-09-02 01:06:12 +00:00
|
|
|
from .language import API_LANGS
|
2021-08-23 14:55:41 +00:00
|
|
|
from .utils import CARDDB_FILE_NAME
|
2021-08-17 15:37:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CardDB:
|
|
|
|
__instance: CardDB = None
|
2021-09-02 01:06:12 +00:00
|
|
|
__cards: dict[Code, Card]
|
|
|
|
__face_to_url: dict[str, str]
|
2021-08-16 12:45:15 +00:00
|
|
|
|
2021-09-03 05:11:46 +00:00
|
|
|
__DB_FILE_NAME = "cards.pickle"
|
|
|
|
__MAPPING_FILE_NAME = "face_to_url.json"
|
|
|
|
|
2021-09-01 18:14:56 +00:00
|
|
|
def __new__(cls) -> CardDB:
|
|
|
|
if CardDB.__instance is None:
|
|
|
|
CardDB.__instance = object.__new__(cls)
|
2021-09-02 01:06:12 +00:00
|
|
|
CardDB.__instance.__cards = {}
|
|
|
|
CardDB.__instance.__face_to_url = {}
|
2021-08-16 12:45:15 +00:00
|
|
|
|
2021-08-17 15:37:28 +00:00
|
|
|
return CardDB.__instance
|
2021-08-16 12:45:15 +00:00
|
|
|
|
2021-09-02 00:54:16 +00:00
|
|
|
def __contains__(self, item: Code) -> bool:
|
2021-09-02 01:06:12 +00:00
|
|
|
return item in self.__cards
|
2021-09-02 00:54:16 +00:00
|
|
|
|
2021-08-18 14:52:32 +00:00
|
|
|
def __getitem__(self, code: Code) -> Card:
|
2021-09-02 01:06:12 +00:00
|
|
|
return self.__cards[code]
|
|
|
|
|
2021-09-02 01:11:31 +00:00
|
|
|
def get_face_url(self, face: str) -> str:
|
|
|
|
if face in self.__face_to_url:
|
|
|
|
return self.__face_to_url[face]
|
|
|
|
else:
|
|
|
|
return face
|
|
|
|
|
2021-09-03 05:11:46 +00:00
|
|
|
def __pickle(self) -> None:
|
|
|
|
with zipfile.ZipFile(CARDDB_FILE_NAME, "w", compression=zipfile.ZIP_LZMA) as zip_file:
|
|
|
|
# cards db
|
|
|
|
with zip_file.open(CardDB.__DB_FILE_NAME, "w") as file:
|
|
|
|
pickle.dump(self.__cards, file)
|
|
|
|
|
|
|
|
# face_to_url mapping
|
|
|
|
with zip_file.open(CardDB.__MAPPING_FILE_NAME, "w") as file:
|
|
|
|
file.write(json.dumps(self.__face_to_url, indent=2).encode("utf-8"))
|
|
|
|
|
|
|
|
def __unpickle(self) -> None:
|
|
|
|
# unpickle db file
|
|
|
|
self.__cards.clear()
|
|
|
|
self.__face_to_url.clear()
|
|
|
|
try:
|
|
|
|
with zipfile.ZipFile(CARDDB_FILE_NAME, "r") as zip_file:
|
|
|
|
# cards db
|
|
|
|
with zip_file.open(CardDB.__DB_FILE_NAME, "r") as file:
|
|
|
|
self.__cards |= pickle.load(file)
|
|
|
|
|
|
|
|
# face_to_url mapping
|
|
|
|
with zip_file.open(CardDB.__MAPPING_FILE_NAME, "r") as file:
|
|
|
|
self.__face_to_url |= json.load(file)
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def load(self) -> None:
|
|
|
|
self.__unpickle()
|
|
|
|
|
|
|
|
def update(self, cards: Cards) -> None:
|
|
|
|
for card in cards:
|
|
|
|
self.__cards[card.code] = card
|
|
|
|
|
|
|
|
self.__pickle()
|
|
|
|
|
2021-09-02 01:06:12 +00:00
|
|
|
def upload_prompt(self) -> None:
|
|
|
|
faces = list(set([
|
|
|
|
card[lang].face
|
|
|
|
for card in self.__cards.values()
|
|
|
|
for lang in API_LANGS
|
|
|
|
if card[lang].face
|
|
|
|
]))
|
|
|
|
faces.sort()
|
|
|
|
|
|
|
|
for face in faces:
|
|
|
|
if face not in self.__face_to_url:
|
|
|
|
face_url = input(f"Upload '{face}' and paste URL: ")
|
|
|
|
if face_url:
|
|
|
|
self.__face_to_url[face] = face_url
|
|
|
|
|
|
|
|
self.__pickle()
|