skyeweave/atlasimagecomposer/cli/cli.py

95 lines
3.4 KiB
Python
Raw Normal View History

2024-08-09 16:57:33 +00:00
import argparse
2024-10-14 18:23:09 +00:00
import logging
2024-08-09 16:57:33 +00:00
import pathlib
import sys
2024-10-05 12:01:50 +00:00
from typing import List
2024-08-09 16:57:33 +00:00
2024-10-04 18:18:56 +00:00
from .. import __version__
from ..backend import compose
2024-10-14 18:23:09 +00:00
from ..config import Paths, AtlasDefaults, Logging
2024-10-05 12:01:50 +00:00
from ..utils.filesystem import rmdir
2024-10-14 18:23:09 +00:00
from ..utils.disables import disable_tqdm
2024-08-09 16:57:33 +00:00
2024-10-14 18:23:09 +00:00
LOGGER = logging.getLogger(Logging.NAME)
2024-08-09 16:57:33 +00:00
# pylint: disable=too-few-public-methods
class Arguments(argparse.Namespace):
"""
Default Arguments when calling the CLI
"""
output: str
id: str
2024-10-05 12:01:50 +00:00
cacheclear: bool
filter: List[str]
timeout: int
2024-10-14 18:23:09 +00:00
quiet: bool
2024-08-09 16:57:33 +00:00
def parse_arguments():
"""
Create a parser and parse the arguments of sys.argv based on the Arguments Namespace
Returns arguments and extra arguments separately
"""
parser = argparse.ArgumentParser(
prog="atlasimagecomposer",
2024-10-05 12:01:50 +00:00
description="CLI tool to automatically generate expression sheets for servants",
epilog="If there are any issues during execution, it helps to clear the cache from time to time")
2024-08-09 16:57:33 +00:00
parser.add_argument("--version", action="version", version=f"atlasimagecomposer {__version__}")
2024-10-05 12:01:50 +00:00
parser.add_argument("--output", action="store", type=str, default=None, dest="output", help="Set the output location. This can be an absolute or relative path")
parser.add_argument("--id", action="store", type=str, default=None, dest="id", help="Set the servantId/charaId - Skips user prompt when provided")
2024-10-05 12:01:50 +00:00
parser.add_argument("--filter", action="extend", nargs="+", dest="filter", help='Specify one or more spritesheet ids that will be fetched')
parser.add_argument("--timeout", action="store", type=int, default=None, dest="timeout", help="Set the timeout for all requests towards AtlasAcademy, default is 10s")
parser.add_argument("--clear-cache", action="store_true", default=False, dest="cacheclear", help="Clear cached assets before downloading files")
2024-10-14 18:23:09 +00:00
parser.add_argument("--quiet", "-q", action="store_true", default=False, dest="quiet", help="Disable logging output")
2024-10-05 12:01:50 +00:00
2024-08-09 16:57:33 +00:00
args = Arguments()
args, extra_args = parser.parse_known_args(sys.argv[1:], namespace=args)
return args, extra_args
2024-10-04 18:18:56 +00:00
def welcome():
print("-------------------------------------------------")
print(" Welcome to the FGO Sprite loader and composer ")
print(" developed by Firq ")
print("-------------------------------------------------")
2024-08-09 16:57:33 +00:00
2024-10-05 12:01:50 +00:00
def run_cli():
2024-10-04 18:18:56 +00:00
args, _ = parse_arguments()
if args.output:
Paths.OUTPUT = pathlib.Path(args.output)
2024-10-05 12:01:50 +00:00
if args.timeout and args.timeout >= 0:
AtlasDefaults.TIMEOUT = args.timeout
2024-10-14 18:23:09 +00:00
if args.quiet:
disable_tqdm()
LOGGER.disabled = True
else:
welcome()
2024-10-05 12:01:50 +00:00
input_id = args.id
if not input_id:
input_id = input("Enter servantId/charaId: ")
2024-10-05 12:01:50 +00:00
try:
t = int(input_id)
2024-10-05 12:01:50 +00:00
if t <= 0:
raise ValueError
except ValueError:
2024-10-14 18:23:09 +00:00
LOGGER.error("Servant ID has to be a valid integer above 0")
2024-10-05 12:01:50 +00:00
sys.exit(1)
input_id = int(input_id)
2024-10-05 12:01:50 +00:00
if args.cacheclear:
cachepath = Paths.IMAGES / str(input_id)
if input_id > 10000:
cachepath = Paths.IMAGES / "manual" / str(input_id)
2024-10-05 12:01:50 +00:00
if cachepath.exists():
rmdir(cachepath)
2024-10-14 18:23:09 +00:00
LOGGER.info("Successfully cleared cached assets")
2024-10-05 12:01:50 +00:00
else:
2024-10-14 18:23:09 +00:00
LOGGER.info("No cache to clear was found, continuing")
2024-10-05 12:01:50 +00:00
compose(input_id, args.filter)