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

"stdout" CLI option

This commit is contained in:
Jörn-Michael Miehe 2021-09-03 04:57:34 +02:00
parent e9e6d5b2b8
commit 7f8f8cf625
2 changed files with 41 additions and 8 deletions

View file

@ -43,6 +43,10 @@ class TTSDeck(Cards):
for code in codes for code in codes
]) ])
@property
def file_name(self) -> str:
return f"{super().file_name}.json"
__FFDECKS_API_URL = "https://ffdecks.com/api/deck" __FFDECKS_API_URL = "https://ffdecks.com/api/deck"
__RE_FFDECKS_ID = re.compile(r"((https?://)?ffdecks\.com(/+api)?/+deck/+)?([0-9]+).*", flags=re.UNICODE) __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 # create deck object
return cls(codes, name, description, True) 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() carddb = CardDB()
# unique face urls used # unique face urls used
@ -207,11 +211,14 @@ class TTSDeck(Cards):
return deck_dict 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: def save(self, language: Language) -> None:
# only save if the deck contains cards # only save if the deck contains cards
if self: if self:
if not os.path.exists(DECKS_DIR_NAME): if not os.path.exists(DECKS_DIR_NAME):
os.mkdir(DECKS_DIR_NAME) os.mkdir(DECKS_DIR_NAME)
with open(os.path.join(DECKS_DIR_NAME, f"{self.file_name}.json"), "w") as file: with open(os.path.join(DECKS_DIR_NAME, self.file_name), "w") as file:
json.dump(self.tts_object(language), file, indent=2) file.write(self.get_json(language))

View file

@ -1,7 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import io
import logging import logging
import os import os
import sys
import zipfile
import fftcg import fftcg
@ -66,6 +69,12 @@ def main() -> None:
help="language for imported objects", 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( subparsers = parser.add_subparsers(
description="Import either an Opus to extend the mod, or import a deck to play right away.", description="Import either an Opus to extend the mod, or import a deck to play right away.",
dest="subcommand", dest="subcommand",
@ -143,12 +152,29 @@ def main() -> None:
os.chdir(OUT_DIR_NAME) os.chdir(OUT_DIR_NAME)
# call function based on args # call function based on args
for deck in args.func(args): decks = args.func(args)
deck.save(args.language)
# bye # decide what to do with the decks
print("Done. Put the generated JSON files in your 'Saved Objects' Folder.") if args.stdout:
print("Thanks for using fftcgtool!") # 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
print("Done. Put the generated JSON files in your 'Saved Objects' Folder.")
print("Thanks for using fftcgtool!")
if __name__ == "__main__": if __name__ == "__main__":