From f59498e3acc3238d409d8f75f487b2751e40273b Mon Sep 17 00:00:00 2001 From: LDericher <40151420+ldericher@users.noreply.github.com> Date: Mon, 23 Aug 2021 16:00:17 +0200 Subject: [PATCH] directory structure --- fftcg/book.py | 12 ++++++++---- fftcg/carddb.py | 4 ++-- fftcg/cards.py | 5 ++++- fftcg/ttsdeck.py | 10 +++++++--- fftcg/utils.py | 4 +++- main.py | 10 +++++++--- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/fftcg/book.py b/fftcg/book.py index a6649c7..3f8c9d8 100644 --- a/fftcg/book.py +++ b/fftcg/book.py @@ -1,12 +1,13 @@ 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, BOOK_PICKLE_NAME, CARD_BACK_URL +from .utils import GRID, RESOLUTION, CARDDB_FILE_NAME, CARD_BACK_URL, IMAGES_DIR_NAME class Book: @@ -58,14 +59,17 @@ class Book: }) def save(self) -> None: + if not os.path.exists(IMAGES_DIR_NAME): + os.mkdir(IMAGES_DIR_NAME) + # save images for i, page in enumerate(self.__pages): # save page image - page["image"].save(page["file_name"]) + page["image"].save(os.path.join(IMAGES_DIR_NAME, page["file_name"])) book: dict[str, Cards] try: - with bz2.BZ2File(BOOK_PICKLE_NAME, "r") as file: + with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file: book = pickle.load(file) except FileNotFoundError: book = {} @@ -80,5 +84,5 @@ class Book: book[page["file_name"]] = page["cards"] # update book.yml file - with bz2.BZ2File(BOOK_PICKLE_NAME, "w") as file: + with bz2.BZ2File(CARDDB_FILE_NAME, "w") as file: pickle.dump(book, file) diff --git a/fftcg/carddb.py b/fftcg/carddb.py index cd66468..cc5976f 100644 --- a/fftcg/carddb.py +++ b/fftcg/carddb.py @@ -5,7 +5,7 @@ import pickle from fftcg import Card from fftcg.code import Code -from fftcg.utils import BOOK_PICKLE_NAME +from fftcg.utils import CARDDB_FILE_NAME class CardDB: @@ -28,7 +28,7 @@ class CardDB: # load book.yml file book: dict try: - with bz2.BZ2File(BOOK_PICKLE_NAME, "r") as file: + with bz2.BZ2File(CARDDB_FILE_NAME, "r") as file: book = pickle.load(file) except FileNotFoundError: book = {} diff --git a/fftcg/cards.py b/fftcg/cards.py index bf72715..5a8f437 100644 --- a/fftcg/cards.py +++ b/fftcg/cards.py @@ -1,3 +1,5 @@ +import re + from .card import Card @@ -18,4 +20,5 @@ class Cards(list[Card]): @property def file_name(self) -> str: - return self.name.lower().replace(" ", "_") + val = self.name.lower().replace(" ", "_") + return re.sub(r"[^\w]", "", val, flags=re.UNICODE) diff --git a/fftcg/ttsdeck.py b/fftcg/ttsdeck.py index 91b976d..855e263 100644 --- a/fftcg/ttsdeck.py +++ b/fftcg/ttsdeck.py @@ -1,13 +1,14 @@ from __future__ import annotations import json +import os import requests from .carddb import CardDB from .cards import Cards from .code import Code -from .utils import CARD_BACK_URL +from .utils import CARD_BACK_URL, DECKS_DIR_NAME class TTSDeck(Cards): @@ -43,7 +44,7 @@ class TTSDeck(Cards): Code(card["card"]["serial_number"]) for card in req.json()["cards"] ] - name = req.json()["name"] + name = f"{req.json()['name']} ({deck_id})" description = req.json()["description"] return cls(codes, name, description) @@ -113,5 +114,8 @@ class TTSDeck(Cards): def save(self) -> None: # only save if the deck contains cards if self: - with open(f"{self.file_name}.json", "w") as file: + if not os.path.exists(DECKS_DIR_NAME): + os.mkdir(DECKS_DIR_NAME) + + with open(os.path.join(DECKS_DIR_NAME, f"{self.file_name}.json"), "w") as file: json.dump(self.tts_object, file, indent=2) diff --git a/fftcg/utils.py b/fftcg/utils.py index e53e670..eacb846 100644 --- a/fftcg/utils.py +++ b/fftcg/utils.py @@ -3,7 +3,9 @@ from .grid import Grid # constants GRID = Grid((10, 7)) # default in TTsim: 10 columns, 7 rows RESOLUTION = Grid((429, 600)) # default in TTsim: 480x670 pixels per card -BOOK_PICKLE_NAME = "book.pickle.bz2" +DECKS_DIR_NAME = "decks" # name of decks directory +IMAGES_DIR_NAME = "images" # name of images directory +CARDDB_FILE_NAME = "carddb.pickle.bz2" # name of card book file # card back URL (image by Aurik) CARD_BACK_URL = "http://cloud-3.steamusercontent.com/ugc/948455238665576576/85063172B8C340602E8D6C783A457122F53F7843/" diff --git a/main.py b/main.py index 30c9ee5..1ec0f1f 100755 --- a/main.py +++ b/main.py @@ -5,6 +5,9 @@ import os import fftcg +# constants +OUT_DIR_NAME = "out" # name of output directory + def opus_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]: # import an opus @@ -94,9 +97,10 @@ def main() -> None: ) # output directory - if not os.path.exists("out"): - os.mkdir("out") - os.chdir("out") + if not os.path.exists(OUT_DIR_NAME): + os.mkdir(OUT_DIR_NAME) + + os.chdir(OUT_DIR_NAME) # call function based on args args = parser.parse_args()