mirror of
https://github.com/ldericher/fftcgtool
synced 2025-01-15 15:02:59 +00:00
argparse -> click
This commit is contained in:
parent
60178329ed
commit
92d8283e39
3 changed files with 164 additions and 157 deletions
1
Pipfile
1
Pipfile
|
@ -9,6 +9,7 @@ verify_ssl = true
|
|||
requests = "*"
|
||||
pillow = "*"
|
||||
roman = "*"
|
||||
click = "*"
|
||||
|
||||
[requires]
|
||||
python_version = "3.9"
|
||||
|
|
91
Pipfile.lock
generated
91
Pipfile.lock
generated
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "a2e1b3f733a9b7697e8f974449a04cf81e78f6959a67945afa0e78446d491690"
|
||||
"sha256": "fc8fa635cc9ef197118ea492773004f3c19117414f62305a6019500c045da690"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
|
@ -31,6 +31,14 @@
|
|||
"markers": "python_version >= '3'",
|
||||
"version": "==2.0.4"
|
||||
},
|
||||
"click": {
|
||||
"hashes": [
|
||||
"sha256:8c04c11192119b1ef78ea049e0a6f0463e4c48ef00a30160c704337586f3ad7a",
|
||||
"sha256:fba402a4a47334742d782209a7c79bc448911afe1149d07bdabdf480b3e2f4b6"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==8.0.1"
|
||||
},
|
||||
"idna": {
|
||||
"hashes": [
|
||||
"sha256:14475042e284991034cb48e06f6851428fb14c4dc953acd9be9a5e95c7b6dd7a",
|
||||
|
@ -41,48 +49,49 @@
|
|||
},
|
||||
"pillow": {
|
||||
"hashes": [
|
||||
"sha256:0b2efa07f69dc395d95bb9ef3299f4ca29bcb2157dc615bae0b42c3c20668ffc",
|
||||
"sha256:114f816e4f73f9ec06997b2fde81a92cbf0777c9e8f462005550eed6bae57e63",
|
||||
"sha256:147bd9e71fb9dcf08357b4d530b5167941e222a6fd21f869c7911bac40b9994d",
|
||||
"sha256:15a2808e269a1cf2131930183dcc0419bc77bb73eb54285dde2706ac9939fa8e",
|
||||
"sha256:196560dba4da7a72c5e7085fccc5938ab4075fd37fe8b5468869724109812edd",
|
||||
"sha256:1c03e24be975e2afe70dfc5da6f187eea0b49a68bb2b69db0f30a61b7031cee4",
|
||||
"sha256:1fd5066cd343b5db88c048d971994e56b296868766e461b82fa4e22498f34d77",
|
||||
"sha256:29c9569049d04aaacd690573a0398dbd8e0bf0255684fee512b413c2142ab723",
|
||||
"sha256:2b6dfa068a8b6137da34a4936f5a816aba0ecc967af2feeb32c4393ddd671cba",
|
||||
"sha256:2cac53839bfc5cece8fdbe7f084d5e3ee61e1303cccc86511d351adcb9e2c792",
|
||||
"sha256:2ee77c14a0299d0541d26f3d8500bb57e081233e3fa915fa35abd02c51fa7fae",
|
||||
"sha256:37730f6e68bdc6a3f02d2079c34c532330d206429f3cee651aab6b66839a9f0e",
|
||||
"sha256:3f08bd8d785204149b5b33e3b5f0ebbfe2190ea58d1a051c578e29e39bfd2367",
|
||||
"sha256:479ab11cbd69612acefa8286481f65c5dece2002ffaa4f9db62682379ca3bb77",
|
||||
"sha256:4bc3c7ef940eeb200ca65bd83005eb3aae8083d47e8fcbf5f0943baa50726856",
|
||||
"sha256:660a87085925c61a0dcc80efb967512ac34dbb256ff7dd2b9b4ee8dbdab58cf4",
|
||||
"sha256:67b3666b544b953a2777cb3f5a922e991be73ab32635666ee72e05876b8a92de",
|
||||
"sha256:70af7d222df0ff81a2da601fab42decb009dc721545ed78549cb96e3a1c5f0c8",
|
||||
"sha256:75e09042a3b39e0ea61ce37e941221313d51a9c26b8e54e12b3ececccb71718a",
|
||||
"sha256:8960a8a9f4598974e4c2aeb1bff9bdd5db03ee65fd1fce8adf3223721aa2a636",
|
||||
"sha256:9364c81b252d8348e9cc0cb63e856b8f7c1b340caba6ee7a7a65c968312f7dab",
|
||||
"sha256:969cc558cca859cadf24f890fc009e1bce7d7d0386ba7c0478641a60199adf79",
|
||||
"sha256:9a211b663cf2314edbdb4cf897beeb5c9ee3810d1d53f0e423f06d6ebbf9cd5d",
|
||||
"sha256:a17ca41f45cf78c2216ebfab03add7cc350c305c38ff34ef4eef66b7d76c5229",
|
||||
"sha256:a2f381932dca2cf775811a008aa3027671ace723b7a38838045b1aee8669fdcf",
|
||||
"sha256:a4eef1ff2d62676deabf076f963eda4da34b51bc0517c70239fafed1d5b51500",
|
||||
"sha256:c088a000dfdd88c184cc7271bfac8c5b82d9efa8637cd2b68183771e3cf56f04",
|
||||
"sha256:c0e0550a404c69aab1e04ae89cca3e2a042b56ab043f7f729d984bf73ed2a093",
|
||||
"sha256:c11003197f908878164f0e6da15fce22373ac3fc320cda8c9d16e6bba105b844",
|
||||
"sha256:c2a5ff58751670292b406b9f06e07ed1446a4b13ffced6b6cab75b857485cbc8",
|
||||
"sha256:c35d09db702f4185ba22bb33ef1751ad49c266534339a5cebeb5159d364f6f82",
|
||||
"sha256:c379425c2707078dfb6bfad2430728831d399dc95a7deeb92015eb4c92345eaf",
|
||||
"sha256:cc866706d56bd3a7dbf8bac8660c6f6462f2f2b8a49add2ba617bc0c54473d83",
|
||||
"sha256:d0da39795049a9afcaadec532e7b669b5ebbb2a9134576ebcc15dd5bdae33cc0",
|
||||
"sha256:f156d6ecfc747ee111c167f8faf5f4953761b5e66e91a4e6767e548d0f80129c",
|
||||
"sha256:f4ebde71785f8bceb39dcd1e7f06bcc5d5c3cf48b9f69ab52636309387b097c8",
|
||||
"sha256:fc214a6b75d2e0ea7745488da7da3c381f41790812988c7a92345978414fad37",
|
||||
"sha256:fd7eef578f5b2200d066db1b50c4aa66410786201669fb76d5238b007918fb24",
|
||||
"sha256:ff04c373477723430dce2e9d024c708a047d44cf17166bf16e604b379bf0ca14"
|
||||
"sha256:0412516dcc9de9b0a1e0ae25a280015809de8270f134cc2c1e32c4eeb397cf30",
|
||||
"sha256:04835e68ef12904bc3e1fd002b33eea0779320d4346082bd5b24bec12ad9c3e9",
|
||||
"sha256:06d1adaa284696785375fa80a6a8eb309be722cf4ef8949518beb34487a3df71",
|
||||
"sha256:085a90a99404b859a4b6c3daa42afde17cb3ad3115e44a75f0d7b4a32f06a6c9",
|
||||
"sha256:10e00f7336780ca7d3653cf3ac26f068fa11b5a96894ea29a64d3dc4b810d630",
|
||||
"sha256:11c27e74bab423eb3c9232d97553111cc0be81b74b47165f07ebfdd29d825875",
|
||||
"sha256:11eb7f98165d56042545c9e6db3ce394ed8b45089a67124298f0473b29cb60b2",
|
||||
"sha256:13654b521fb98abdecec105ea3fb5ba863d1548c9b58831dd5105bb3873569f1",
|
||||
"sha256:18a07a683805d32826c09acfce44a90bf474e6a66ce482b1c7fcd3757d588df3",
|
||||
"sha256:19ec4cfe4b961edc249b0e04b5618666c23a83bc35842dea2bfd5dfa0157f81b",
|
||||
"sha256:1c3ff00110835bdda2b1e2b07f4a2548a39744bb7de5946dc8e95517c4fb2ca6",
|
||||
"sha256:27a330bf7014ee034046db43ccbb05c766aa9e70b8d6c5260bfc38d73103b0ba",
|
||||
"sha256:2b11c9d310a3522b0fd3c35667914271f570576a0e387701f370eb39d45f08a4",
|
||||
"sha256:2c661542c6f71dfd9dc82d9d29a8386287e82813b0375b3a02983feac69ef864",
|
||||
"sha256:2cde7a4d3687f21cffdf5bb171172070bb95e02af448c4c8b2f223d783214056",
|
||||
"sha256:2d5e9dc0bf1b5d9048a94c48d0813b6c96fccfa4ccf276d9c36308840f40c228",
|
||||
"sha256:2f23b2d3079522fdf3c09de6517f625f7a964f916c956527bed805ac043799b8",
|
||||
"sha256:35d27687f027ad25a8d0ef45dd5208ef044c588003cdcedf05afb00dbc5c2deb",
|
||||
"sha256:35d409030bf3bd05fa66fb5fdedc39c521b397f61ad04309c90444e893d05f7d",
|
||||
"sha256:4abc247b31a98f29e5224f2d31ef15f86a71f79c7f4d2ac345a5d551d6393073",
|
||||
"sha256:4d89a2e9219a526401015153c0e9dd48319ea6ab9fe3b066a20aa9aee23d9fd3",
|
||||
"sha256:4e59e99fd680e2b8b11bbd463f3c9450ab799305d5f2bafb74fefba6ac058616",
|
||||
"sha256:56fd98c8294f57636084f4b076b75f86c57b2a63a8410c0cd172bc93695ee979",
|
||||
"sha256:59697568a0455764a094585b2551fd76bfd6b959c9f92d4bdec9d0e14616303a",
|
||||
"sha256:6cb3dd7f23b044b0737317f892d399f9e2f0b3a02b22b2c692851fb8120d82c6",
|
||||
"sha256:838eb85de6d9307c19c655c726f8d13b8b646f144ca6b3771fa62b711ebf7624",
|
||||
"sha256:963ebdc5365d748185fdb06daf2ac758116deecb2277ec5ae98139f93844bc09",
|
||||
"sha256:a048dad5ed6ad1fad338c02c609b862dfaa921fcd065d747194a6805f91f2196",
|
||||
"sha256:a66566f8a22561fc1a88dc87606c69b84fa9ce724f99522cf922c801ec68f5c1",
|
||||
"sha256:be25cb93442c6d2f8702c599b51184bd3ccd83adebd08886b682173e09ef0c3f",
|
||||
"sha256:c691b26283c3a31594683217d746f1dad59a7ae1d4cfc24626d7a064a11197d4",
|
||||
"sha256:cc9d0dec711c914ed500f1d0d3822868760954dce98dfb0b7382a854aee55d19",
|
||||
"sha256:ce2e5e04bb86da6187f96d7bab3f93a7877830981b37f0287dd6479e27a10341",
|
||||
"sha256:ce651ca46d0202c302a535d3047c55a0131a720cf554a578fc1b8a2aff0e7d96",
|
||||
"sha256:d675a876b295afa114ca8bf42d7f86b5fb1298e1b6bb9a24405a3f6c8338811c",
|
||||
"sha256:dde3f3ed8d00c72631bc19cbfff8ad3b6215062a5eed402381ad365f82f0c18c",
|
||||
"sha256:e5a31c07cea5edbaeb4bdba6f2b87db7d3dc0f446f379d907e51cc70ea375629",
|
||||
"sha256:f514c2717012859ccb349c97862568fdc0479aad85b0270d6b5a6509dbc142e2",
|
||||
"sha256:fc0db32f7223b094964e71729c0361f93db43664dd1ec86d3df217853cedda87",
|
||||
"sha256:feb5db446e96bfecfec078b943cc07744cc759893cef045aa8b8b6d6aaa8274e"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==8.3.1"
|
||||
"version": "==8.3.2"
|
||||
},
|
||||
"requests": {
|
||||
"hashes": [
|
||||
|
|
209
fftcgtool.py
209
fftcgtool.py
|
@ -1,142 +1,72 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
import click
|
||||
|
||||
import fftcg
|
||||
|
||||
# constants
|
||||
OUT_DIR_NAME = "out" # name of output directory
|
||||
|
||||
|
||||
def opus_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]:
|
||||
carddb = fftcg.CardDB()
|
||||
decks: list[fftcg.TTSDeck] = []
|
||||
for opus_id in args.opus_ids:
|
||||
# import an opus
|
||||
opus = fftcg.Opus(opus_id, args.language)
|
||||
book = fftcg.Book(opus, args.language, args.num_requests)
|
||||
book.save()
|
||||
class LanguageParamType(click.ParamType):
|
||||
name = "lang"
|
||||
|
||||
carddb.update(opus)
|
||||
decks.extend(opus.elemental_decks)
|
||||
|
||||
carddb.upload_prompt()
|
||||
|
||||
# create elemental decks for opus
|
||||
return decks
|
||||
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("")
|
||||
|
||||
|
||||
def ffdecks_decks(args: argparse.Namespace) -> list[fftcg.TTSDeck]:
|
||||
decks: list[fftcg.TTSDeck] = []
|
||||
for deck_id in args.deck_ids:
|
||||
# import a deck
|
||||
decks.append(fftcg.TTSDeck.from_ffdecks_deck(deck_id))
|
||||
|
||||
return decks
|
||||
LANGUAGE = LanguageParamType()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
# set up CLI
|
||||
|
||||
# main parser
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="fftcgtool",
|
||||
description="Imports FFTCG cards for TT-Sim.",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
@click.group()
|
||||
@click.option(
|
||||
"-v", "--verbose",
|
||||
help="increase output verbosity",
|
||||
action="count",
|
||||
count=True,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
@click.option(
|
||||
"-l", "--language",
|
||||
type=fftcg.Language,
|
||||
type=LANGUAGE,
|
||||
default="en",
|
||||
metavar="LANG",
|
||||
help="language for imported objects",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
@click.option(
|
||||
"-s", "--stdout",
|
||||
action="store_true",
|
||||
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."""
|
||||
|
||||
subparsers = parser.add_subparsers(
|
||||
description="Import either an Opus to extend the mod, or import a deck to play right away.",
|
||||
dest="subcommand",
|
||||
help="valid subcommands",
|
||||
required=True,
|
||||
)
|
||||
|
||||
# "opus" subcommand
|
||||
opuses_parser = subparsers.add_parser(
|
||||
"opuses",
|
||||
description="Imports Opuses from the square API and creates its elemental decks as JSON files.",
|
||||
)
|
||||
|
||||
opuses_parser.set_defaults(
|
||||
func=opus_decks
|
||||
)
|
||||
|
||||
opuses_parser.add_argument(
|
||||
"opus_ids",
|
||||
type=str,
|
||||
nargs="+",
|
||||
metavar="Opus_ID",
|
||||
help="the Opuses to import",
|
||||
)
|
||||
|
||||
opuses_parser.add_argument(
|
||||
"-n", "--num_requests",
|
||||
type=int,
|
||||
default=20,
|
||||
metavar="COUNT",
|
||||
help="maximum number of concurrent requests",
|
||||
)
|
||||
|
||||
# "ffdecks" subcommand
|
||||
ffdecks_parser = subparsers.add_parser(
|
||||
"ffdecks",
|
||||
description="Imports Decks from the ffdecks.com API and creates it as a JSON file.",
|
||||
)
|
||||
|
||||
ffdecks_parser.set_defaults(
|
||||
func=ffdecks_decks
|
||||
)
|
||||
|
||||
ffdecks_parser.add_argument(
|
||||
"deck_ids",
|
||||
type=str,
|
||||
nargs="+",
|
||||
metavar="Deck_ID",
|
||||
help="the Decks to import",
|
||||
)
|
||||
|
||||
# parse arguments
|
||||
args = parser.parse_args()
|
||||
ctx.ensure_object(dict)
|
||||
ctx.obj['LANG'] = language
|
||||
|
||||
# set up logging
|
||||
if args.verbose is None:
|
||||
args.verbose = logging.WARN
|
||||
elif args.verbose == 1:
|
||||
args.verbose = logging.INFO
|
||||
if verbose == 0:
|
||||
verbose = logging.WARN
|
||||
elif verbose == 1:
|
||||
verbose = logging.INFO
|
||||
else:
|
||||
args.verbose = logging.DEBUG
|
||||
verbose = logging.DEBUG
|
||||
|
||||
logging.basicConfig(
|
||||
level=args.verbose,
|
||||
level=verbose,
|
||||
format="%(levelname)s: %(processName)s %(message)s",
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.info("fftcgtool started.")
|
||||
logger.debug(f"{args = }")
|
||||
logger.debug(f"args: {verbose = }, {language = }, {stdout = }")
|
||||
|
||||
# output directory
|
||||
if not os.path.exists(OUT_DIR_NAME):
|
||||
|
@ -148,22 +78,89 @@ def main() -> None:
|
|||
carddb = fftcg.CardDB()
|
||||
carddb.load()
|
||||
|
||||
# call function based on args
|
||||
decks = args.func(args)
|
||||
|
||||
@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)
|
||||
|
||||
# decide what to do with the decks
|
||||
if args.stdout:
|
||||
if stdout:
|
||||
# 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
|
||||
for deck in decks:
|
||||
zip_file.writestr(deck.file_name, deck.get_json(args.language))
|
||||
zip_file.writestr(deck.file_name, deck.get_json(language))
|
||||
|
||||
else:
|
||||
# save the decks to disk
|
||||
for deck in decks:
|
||||
deck.save(args.language)
|
||||
deck.save(language)
|
||||
|
||||
# bye
|
||||
print("Done. Put the generated JSON files in your 'Saved Objects' Folder.")
|
||||
|
|
Loading…
Reference in a new issue