1
0
Fork 0
mirror of https://github.com/ldericher/fftcgtool synced 2025-01-15 15:02:59 +00:00

carddb pickle format

This commit is contained in:
Jörn-Michael Miehe 2021-08-23 16:46:53 +02:00
parent 89ea5a3e89
commit 938a10c807
3 changed files with 25 additions and 39 deletions

View file

@ -1,13 +1,11 @@
import bz2
import logging import logging
import os import os
import pickle
from PIL import Image from PIL import Image
from .cards import Cards from .cards import Cards
from .imageloader import ImageLoader from .imageloader import ImageLoader
from .utils import GRID, RESOLUTION, CARDDB_FILE_NAME, CARD_BACK_URL, IMAGES_DIR_NAME from .utils import GRID, RESOLUTION, CARD_BACK_URL, IMAGES_DIR_NAME
class Book: class Book:
@ -33,8 +31,6 @@ class Book:
self.__pages = [] self.__pages = []
page_images: list[Image.Image]
page_cards: Cards
for page_num, (page_images, page_cards) in enumerate(zip(GRID.chunks(images), GRID.chunks(cards))): for page_num, (page_images, page_cards) in enumerate(zip(GRID.chunks(images), GRID.chunks(cards))):
# create book page Image # create book page Image
page_image = Image.new("RGB", GRID * RESOLUTION) page_image = Image.new("RGB", GRID * RESOLUTION)
@ -63,26 +59,16 @@ class Book:
os.mkdir(IMAGES_DIR_NAME) os.mkdir(IMAGES_DIR_NAME)
# save images # save images
for i, page in enumerate(self.__pages): for page in self.__pages:
# save page image page["file_name"] = os.path.join(IMAGES_DIR_NAME, page["file_name"])
page["image"].save(os.path.join(IMAGES_DIR_NAME, page["file_name"])) page["image"].save(page["file_name"])
book: dict[str, Cards] # ask for upload
try: for page in self.__pages:
with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file:
book = pickle.load(file)
except FileNotFoundError:
book = {}
# save book
for i, page in enumerate(self.__pages):
# ask for upload
face_url = input(f"Upload '{page['file_name']}' and paste URL: ") face_url = input(f"Upload '{page['file_name']}' and paste URL: ")
if not face_url:
face_url = f"file://{os.path.abspath(page['file_name'])}"
for card in page["cards"]: for card in page["cards"]:
card.face_url = face_url card.face_url = face_url
# add contents of that image
book[page["file_name"]] = page["cards"]
# update book.yml file
with bz2.BZ2File(CARDDB_FILE_NAME, "w") as file:
pickle.dump(book, file)

View file

@ -4,6 +4,7 @@ import bz2
import pickle import pickle
from fftcg import Card from fftcg import Card
from fftcg.cards import Cards
from fftcg.code import Code from fftcg.code import Code
from fftcg.utils import CARDDB_FILE_NAME from fftcg.utils import CARDDB_FILE_NAME
@ -24,21 +25,19 @@ class CardDB:
def __getitem__(self, code: Code) -> Card: def __getitem__(self, code: Code) -> Card:
return self.__content[code] return self.__content[code]
def load(self): def update(self, cards: Cards):
# load book.yml file for card in cards:
book: dict 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()
try: try:
with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file: with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file:
book = pickle.load(file) self.__content |= pickle.load(file)
except FileNotFoundError: except FileNotFoundError:
book = {} pass
# "invert" book into card database:
# every card is indexable by its code
self.__content.clear()
for file_name, cards in book.items():
self.__content |= {
card.code: card
for card in cards
}

View file

@ -18,6 +18,7 @@ def opus_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]:
# load the current carddb # load the current carddb
carddb = fftcg.CardDB.get() carddb = fftcg.CardDB.get()
carddb.load() carddb.load()
carddb.update(opus)
# create elemental decks for opus # create elemental decks for opus
return opus.elemental_decks return opus.elemental_decks