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

172 lines
3.9 KiB
Python
Raw Normal View History

2021-08-03 00:22:00 +00:00
#!/usr/bin/env python3
2021-08-04 16:36:23 +00:00
import logging
import os
2021-09-03 02:57:34 +00:00
import sys
import zipfile
2021-08-03 00:22:00 +00:00
2021-09-03 09:52:05 +00:00
import click
2021-08-09 05:01:25 +00:00
import fftcg
2021-08-23 14:00:17 +00:00
# constants
OUT_DIR_NAME = "out" # name of output directory
2021-09-03 09:52:05 +00:00
class LanguageParamType(click.ParamType):
name = "lang"
def convert(self, value, param, ctx) -> fftcg.Language:
if isinstance(value, fftcg.Language):
return value
elif isinstance(value, str):
return fftcg.Language(value)
else:
return fftcg.Language("")
LANGUAGE = LanguageParamType()
@click.group()
@click.option(
"-v", "--verbose",
help="increase output verbosity",
count=True,
)
@click.option(
"-l", "--language",
type=LANGUAGE,
default="en",
help="language for imported objects",
)
@click.option(
"-s", "--stdout",
is_flag=True,
help="print the deck files in a zip archive to stdout, skip creating JSONs on disk",
)
@click.pass_context
def main(ctx, verbose, language, stdout) -> None:
"""Imports FFTCG cards for TT-Sim."""
ctx.ensure_object(dict)
ctx.obj['LANG'] = language
# set up logging
2021-09-03 09:52:05 +00:00
if verbose == 0:
verbose = logging.WARN
elif verbose == 1:
verbose = logging.INFO
else:
2021-09-03 09:52:05 +00:00
verbose = logging.DEBUG
2021-08-23 12:57:05 +00:00
logging.basicConfig(
2021-09-03 09:52:05 +00:00
level=verbose,
2021-08-23 12:57:05 +00:00
format="%(levelname)s: %(processName)s %(message)s",
)
2021-08-04 16:36:23 +00:00
logger = logging.getLogger(__name__)
2021-09-03 07:29:40 +00:00
logger.info("fftcgtool started.")
2021-09-03 09:52:05 +00:00
logger.debug(f"args: {verbose = }, {language = }, {stdout = }")
2021-08-04 16:36:23 +00:00
# output directory
2021-08-23 14:00:17 +00:00
if not os.path.exists(OUT_DIR_NAME):
os.mkdir(OUT_DIR_NAME)
os.chdir(OUT_DIR_NAME)
2021-08-04 16:36:23 +00:00
2021-09-03 07:29:40 +00:00
# load the current carddb
carddb = fftcg.CardDB()
carddb.load()
2021-09-03 09:52:05 +00:00
@main.command()
@click.option(
"-n", "--num_requests",
type=int,
default=20,
help="maximum number of concurrent requests",
)
@click.argument(
"opus_ids",
nargs=-1,
type=str,
metavar="[OPUS_ID] ...",
)
@click.pass_context
def opuses(ctx, opus_ids, num_requests) -> list[fftcg.TTSDeck]:
"""
Imports Opuses from the square API and creates its elemental decks as JSON files.
OPUS_ID: each of the Opuses to import
"""
ctx.ensure_object(dict)
language = ctx.obj['LANG'] or fftcg.Language("")
carddb = fftcg.CardDB()
decks: list[fftcg.TTSDeck] = []
for opus_id in opus_ids:
# import an opus
opus = fftcg.Opus(opus_id, language)
book = fftcg.Book(opus, language, num_requests)
book.save()
carddb.update(opus)
decks.extend(opus.elemental_decks)
carddb.upload_prompt()
# create elemental decks for opus
return decks
@main.command()
@click.argument(
"deck_ids",
nargs=-1,
type=str,
metavar="[DECK_ID] ...",
)
def ffdecks(deck_ids) -> list[fftcg.TTSDeck]:
"""
Imports Decks from the ffdecks.com API and creates it as a JSON file.
DECK_ID: each of the Decks to import
"""
print(f"{deck_ids = }")
decks: list[fftcg.TTSDeck] = []
for deck_id in deck_ids:
# import a deck
decks.append(fftcg.TTSDeck.from_ffdecks_deck(deck_id))
return decks
@main.result_callback()
def process_decks(decks: list[fftcg.TTSDeck], verbose, language, stdout):
# arg needed because it's in this group
int(verbose)
2021-09-03 02:57:34 +00:00
# decide what to do with the decks
2021-09-03 09:52:05 +00:00
if stdout:
2021-09-03 04:28:34 +00:00
# print out a zip file
with open(sys.stdout.fileno(), "wb", closefd=False, buffering=0) as raw_stdout:
with zipfile.ZipFile(raw_stdout, "w", compression=zipfile.ZIP_DEFLATED) as zip_file:
# put the decks into that zip file
2021-09-03 02:57:34 +00:00
for deck in decks:
2021-09-03 09:52:05 +00:00
zip_file.writestr(deck.file_name, deck.get_json(language))
2021-09-03 02:57:34 +00:00
else:
2021-09-03 04:28:34 +00:00
# save the decks to disk
2021-09-03 02:57:34 +00:00
for deck in decks:
2021-09-03 09:52:05 +00:00
deck.save(language)
2021-09-03 02:57:34 +00:00
# bye
print("Done. Put the generated JSON files in your 'Saved Objects' Folder.")
print("Thanks for using fftcgtool!")
2021-08-04 01:39:19 +00:00
2021-08-09 04:18:56 +00:00
if __name__ == "__main__":
2021-08-03 21:41:25 +00:00
main()