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

209 lines
4.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-09-07 12:11:51 +00:00
import fftcgtool
2021-09-03 09:52:05 +00:00
class LanguageParamType(click.ParamType):
2021-09-07 12:11:51 +00:00
def convert(self, value, param, ctx) -> fftcgtool.Language:
if isinstance(value, fftcgtool.Language):
2021-09-03 09:52:05 +00:00
return value
elif isinstance(value, str):
2021-09-07 12:11:51 +00:00
return fftcgtool.Language(value)
2021-09-03 09:52:05 +00:00
else:
2021-09-07 12:11:51 +00:00
return fftcgtool.Language("")
2021-09-03 09:52:05 +00:00
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",
metavar="LANG",
)
@click.option(
"-z", "--zip",
type=click.File("wb"),
help="wrap deck files into a zip archive, skip creating individual JSONs",
metavar="FILE",
)
@click.option(
"-o", "--output",
type=click.Path(
allow_dash=False,
dir_okay=True,
file_okay=False,
),
help="use specified output directory instead of ./out",
default="out",
metavar="DIR",
)
@click.option(
"-u", "--db-url",
type=str,
help="load immutable CardDB from URL instead of local, overrides -f",
metavar="URL",
2021-09-03 09:52:05 +00:00
)
@click.option(
"-f", "--db-file",
type=click.Path(
allow_dash=False,
dir_okay=False,
file_okay=True,
),
default="carddb.zip",
help="use specified CardDB file instead of ./out/carddb.zip",
metavar="FILE",
2021-09-03 09:52:05 +00:00
)
@click.pass_context
def main(ctx, **kwargs) -> None:
2021-09-03 09:52:05 +00:00
"""Imports FFTCG cards for TT-Sim."""
ctx.ensure_object(dict)
ctx.obj["language"] = kwargs["language"]
# set up logging
if kwargs["verbose"] == 0:
2021-09-03 09:52:05 +00:00
verbose = logging.WARN
elif kwargs["verbose"] == 1:
2021-09-03 09:52:05 +00:00
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.")
logger.debug(f"{kwargs = }")
2021-08-04 16:36:23 +00:00
# output directory
if not os.path.exists(kwargs["output"]):
os.mkdir(kwargs["output"])
2021-08-23 14:00:17 +00:00
os.chdir(kwargs["output"])
2021-08-04 16:36:23 +00:00
2021-09-03 07:29:40 +00:00
# load the current carddb
if kwargs["db_url"] is not None:
try:
2021-09-07 12:11:51 +00:00
fftcgtool.CardDB(kwargs["db_url"])
except (ValueError, KeyError, zipfile.BadZipFile) as cause:
logger.critical(f"Couldn't initialize CardDB: {cause}")
sys.exit(1)
else:
2021-09-07 12:11:51 +00:00
fftcgtool.RWCardDB(kwargs["db_file"])
2021-09-03 07:29:40 +00:00
2021-09-03 09:52:05 +00:00
@main.command()
@click.option(
"-n", "--num-requests",
2021-09-03 09:52:05 +00:00
type=int,
default=20,
help="maximum number of concurrent requests",
)
@click.argument(
"opus-ids",
2021-09-03 09:52:05 +00:00
nargs=-1,
type=str,
metavar="[OPUS-ID] ...",
2021-09-03 09:52:05 +00:00
)
@click.pass_context
2021-09-07 12:11:51 +00:00
def opuses(ctx, opus_ids, num_requests) -> list[fftcgtool.TTSDeck]:
2021-09-03 09:52:05 +00:00
"""
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)
2021-09-07 12:11:51 +00:00
language = ctx.obj["language"] or fftcgtool.Language("")
2021-09-03 09:52:05 +00:00
2021-09-07 12:11:51 +00:00
carddb = fftcgtool.CardDB()
decks: list[fftcgtool.TTSDeck] = []
2021-09-03 09:52:05 +00:00
for opus_id in opus_ids:
# import an opus
2021-09-07 12:11:51 +00:00
opus = fftcgtool.Opus(opus_id, language)
book = fftcgtool.Book(opus, language, num_requests)
2021-09-03 09:52:05 +00:00
book.save()
carddb.update(opus)
decks.extend(opus.elemental_decks)
carddb.upload_prompt()
carddb.save()
2021-09-03 09:52:05 +00:00
# create elemental decks for opus
return decks
@main.command()
@click.argument(
"deck-ids",
2021-09-03 09:52:05 +00:00
nargs=-1,
type=str,
metavar="[DECK-ID] ...",
2021-09-03 09:52:05 +00:00
)
2021-09-07 12:11:51 +00:00
def ffdecks(deck_ids) -> list[fftcgtool.TTSDeck]:
2021-09-03 09:52:05 +00:00
"""
Imports Decks from the ffdecks.com API and creates it as a JSON file.
DECK_ID: each of the Decks to import
"""
2021-09-07 12:11:51 +00:00
decks: list[fftcgtool.TTSDeck] = []
2021-09-03 09:52:05 +00:00
for deck_id in deck_ids:
# import a deck
2021-09-07 12:11:51 +00:00
decks.append(fftcgtool.TTSDeck.from_ffdecks_deck(deck_id))
2021-09-03 09:52:05 +00:00
return decks
@main.result_callback()
2021-09-07 12:11:51 +00:00
def finalize(decks: list[fftcgtool.TTSDeck], **kwargs):
2021-09-06 03:07:11 +00:00
logger = logging.getLogger(__name__)
2021-09-03 02:57:34 +00:00
# decide what to do with the decks
if kwargs["zip"] is not None:
2021-09-06 03:07:11 +00:00
logger.debug("Outputting decks to ZIP")
if decks:
# create zip file
with zipfile.ZipFile(kwargs["zip"], "w", compression=zipfile.ZIP_DEFLATED) as zip_file:
2021-09-03 04:28:34 +00:00
# put the decks into that zip file
2021-09-03 02:57:34 +00:00
for deck in decks:
2021-09-06 03:07:11 +00:00
logger.debug(f"Saving Deck {deck!r}")
zip_file.writestr(deck.file_name, deck.get_json(kwargs["language"]))
2021-09-03 02:57:34 +00:00
else:
2021-09-06 03:07:11 +00:00
logger.debug("Outputting decks to disk")
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-06 03:07:11 +00:00
logger.debug(f"Saving Deck {deck!r}")
deck.save(kwargs["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()