1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 23:03:00 +00:00
fftcgtool/fftcg/carddb.py

42 lines
1 KiB
Python
Raw Normal View History

2021-08-17 15:37:28 +00:00
from __future__ import annotations
2021-08-23 11:43:12 +00:00
import bz2
import pickle
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
from .utils import CARDDB_FILE_NAME
2021-08-17 15:37:28 +00:00
class CardDB:
__instance: CardDB = None
2021-09-01 18:14:56 +00:00
__content: dict[Code, Card]
2021-08-16 12:45:15 +00:00
2021-09-01 18:14:56 +00:00
def __new__(cls) -> CardDB:
if CardDB.__instance is None:
CardDB.__instance = object.__new__(cls)
CardDB.__instance.__content = {}
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-08-18 14:52:32 +00:00
def __getitem__(self, code: Code) -> Card:
return self.__content[code]
2021-08-17 15:37:28 +00:00
2021-08-23 14:46:53 +00:00
def update(self, cards: Cards):
for card in cards:
self.__content[card.code] = card
# pickle db file
with bz2.BZ2File(CARDDB_FILE_NAME, "w") as file:
pickle.dump(self.__content, file)
def load(self) -> None:
# unpickle db file
self.__content.clear()
2021-08-16 12:45:15 +00:00
try:
2021-08-23 14:00:17 +00:00
with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file:
2021-08-23 14:46:53 +00:00
self.__content |= pickle.load(file)
2021-08-16 12:45:15 +00:00
except FileNotFoundError:
2021-08-23 14:46:53 +00:00
pass