mirror of
https://github.com/ldericher/fftcgtool
synced 2025-01-15 15:02:59 +00:00
carddb pickle format
This commit is contained in:
parent
89ea5a3e89
commit
938a10c807
3 changed files with 25 additions and 39 deletions
|
@ -1,13 +1,11 @@
|
|||
import bz2
|
||||
import logging
|
||||
import os
|
||||
import pickle
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from .cards import Cards
|
||||
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:
|
||||
|
@ -33,8 +31,6 @@ class Book:
|
|||
|
||||
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))):
|
||||
# create book page Image
|
||||
page_image = Image.new("RGB", GRID * RESOLUTION)
|
||||
|
@ -63,26 +59,16 @@ class Book:
|
|||
os.mkdir(IMAGES_DIR_NAME)
|
||||
|
||||
# save images
|
||||
for i, page in enumerate(self.__pages):
|
||||
# save page image
|
||||
page["image"].save(os.path.join(IMAGES_DIR_NAME, page["file_name"]))
|
||||
for page in self.__pages:
|
||||
page["file_name"] = os.path.join(IMAGES_DIR_NAME, page["file_name"])
|
||||
page["image"].save(page["file_name"])
|
||||
|
||||
book: dict[str, Cards]
|
||||
try:
|
||||
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
|
||||
for page in self.__pages:
|
||||
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"]:
|
||||
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)
|
||||
|
|
|
@ -4,6 +4,7 @@ import bz2
|
|||
import pickle
|
||||
|
||||
from fftcg import Card
|
||||
from fftcg.cards import Cards
|
||||
from fftcg.code import Code
|
||||
from fftcg.utils import CARDDB_FILE_NAME
|
||||
|
||||
|
@ -24,21 +25,19 @@ class CardDB:
|
|||
def __getitem__(self, code: Code) -> Card:
|
||||
return self.__content[code]
|
||||
|
||||
def load(self):
|
||||
# load book.yml file
|
||||
book: dict
|
||||
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()
|
||||
try:
|
||||
with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file:
|
||||
book = pickle.load(file)
|
||||
self.__content |= pickle.load(file)
|
||||
except FileNotFoundError:
|
||||
book = {}
|
||||
|
||||
# "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
|
||||
}
|
||||
pass
|
||||
|
|
1
main.py
1
main.py
|
@ -18,6 +18,7 @@ def opus_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]:
|
|||
# load the current carddb
|
||||
carddb = fftcg.CardDB.get()
|
||||
carddb.load()
|
||||
carddb.update(opus)
|
||||
|
||||
# create elemental decks for opus
|
||||
return opus.elemental_decks
|
||||
|
|
Loading…
Reference in a new issue