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

directory structure

This commit is contained in:
Jörn-Michael Miehe 2021-08-23 16:00:17 +02:00
parent 776769c010
commit f59498e3ac
6 changed files with 31 additions and 14 deletions

View file

@ -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)

View file

@ -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 = {}

View file

@ -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)

View file

@ -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)

View file

@ -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/"

10
main.py
View file

@ -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()