mirror of
https://github.com/ldericher/fftcgtool
synced 2025-01-15 15:02:59 +00:00
"stdout" CLI option
This commit is contained in:
parent
e9e6d5b2b8
commit
7f8f8cf625
2 changed files with 41 additions and 8 deletions
|
@ -43,6 +43,10 @@ class TTSDeck(Cards):
|
|||
for code in codes
|
||||
])
|
||||
|
||||
@property
|
||||
def file_name(self) -> str:
|
||||
return f"{super().file_name}.json"
|
||||
|
||||
__FFDECKS_API_URL = "https://ffdecks.com/api/deck"
|
||||
__RE_FFDECKS_ID = re.compile(r"((https?://)?ffdecks\.com(/+api)?/+deck/+)?([0-9]+).*", flags=re.UNICODE)
|
||||
|
||||
|
@ -125,7 +129,7 @@ class TTSDeck(Cards):
|
|||
# create deck object
|
||||
return cls(codes, name, description, True)
|
||||
|
||||
def tts_object(self, language: Language) -> dict[str, any]:
|
||||
def get_tts_object(self, language: Language) -> dict[str, any]:
|
||||
carddb = CardDB()
|
||||
|
||||
# unique face urls used
|
||||
|
@ -207,11 +211,14 @@ class TTSDeck(Cards):
|
|||
|
||||
return deck_dict
|
||||
|
||||
def get_json(self, language: Language) -> str:
|
||||
return json.dumps(self.get_tts_object(language), indent=2)
|
||||
|
||||
def save(self, language: Language) -> None:
|
||||
# only save if the deck contains cards
|
||||
if self:
|
||||
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(language), file, indent=2)
|
||||
with open(os.path.join(DECKS_DIR_NAME, self.file_name), "w") as file:
|
||||
file.write(self.get_json(language))
|
||||
|
|
28
fftcgtool.py
28
fftcgtool.py
|
@ -1,7 +1,10 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import io
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
import fftcg
|
||||
|
||||
|
@ -66,6 +69,12 @@ def main() -> None:
|
|||
help="language for imported objects",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-s", "--stdout",
|
||||
action="store_true",
|
||||
help="print the deck files in a zip archive to stdout, skip creating JSONs on disk",
|
||||
)
|
||||
|
||||
subparsers = parser.add_subparsers(
|
||||
description="Import either an Opus to extend the mod, or import a deck to play right away.",
|
||||
dest="subcommand",
|
||||
|
@ -143,7 +152,24 @@ def main() -> None:
|
|||
os.chdir(OUT_DIR_NAME)
|
||||
|
||||
# call function based on args
|
||||
for deck in args.func(args):
|
||||
decks = args.func(args)
|
||||
|
||||
# decide what to do with the decks
|
||||
if args.stdout:
|
||||
# create file-like buffer
|
||||
with io.BytesIO() as buffer:
|
||||
# create zip file in buffer
|
||||
with zipfile.ZipFile(buffer, "w") as zip_file:
|
||||
for deck in decks:
|
||||
zip_file.writestr(deck.file_name, deck.get_json(args.language))
|
||||
|
||||
# write zip file to stdout
|
||||
buffer.seek(0)
|
||||
with open(sys.stdout.fileno(), "wb", closefd=False, buffering=0) as stdout:
|
||||
stdout.write(buffer.read())
|
||||
|
||||
else:
|
||||
for deck in decks:
|
||||
deck.save(args.language)
|
||||
|
||||
# bye
|
||||
|
|
Loading…
Reference in a new issue