From 821251b77c755c75df2b49b0c99ddf250653fd32 Mon Sep 17 00:00:00 2001 From: Firq Date: Sun, 20 Oct 2024 19:11:26 +0200 Subject: [PATCH] Split download and compose into two functions --- skyeweave/cli/cli.py | 4 ++- skyeweave/service/compose.py | 51 ++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/skyeweave/cli/cli.py b/skyeweave/cli/cli.py index 04cd3fa..a6ce9b1 100644 --- a/skyeweave/cli/cli.py +++ b/skyeweave/cli/cli.py @@ -91,4 +91,6 @@ def run(): rmdir(PathConfig.IMAGES) LOGGER.info("Successfully reset local storage") - SkyeWeave().compose(input_id, args.filter) + weaver = SkyeWeave(input_id, args.filter) + weaver.download() + weaver.compose() diff --git a/skyeweave/service/compose.py b/skyeweave/service/compose.py index 9f1ada0..7296900 100644 --- a/skyeweave/service/compose.py +++ b/skyeweave/service/compose.py @@ -1,6 +1,6 @@ import logging import pathlib -from typing import List, Optional +from typing import Dict, List, Optional, TypedDict from collections import Counter from PIL import Image @@ -11,38 +11,49 @@ from .atlas import SpritesheetData, fetch_data, fetch_expression_sheets, fetch_c LOGGER = logging.getLogger(LoggingConfig.NAME) +class CharaInfos(TypedDict): + folder: pathlib.Path + config: SpritesheetData + class SkyeWeave: output_folder: pathlib.Path image_folder: pathlib.Path + chara_ids: List[str] + chara_infos: Dict[str, CharaInfos] - def __init__(self, output: Optional[pathlib.Path] = None, assets: Optional[pathlib.Path] = None): - self.output_folder = output or PathConfig.OUTPUT - self.image_folder = assets or PathConfig.IMAGES + def __init__(self, input_id: int, filters: Optional[List[str]] = None, output: Optional[pathlib.Path] = None, assets: Optional[pathlib.Path] = None): + _output_folder = output or PathConfig.OUTPUT + _image_folder = assets or PathConfig.IMAGES - self.output_folder.mkdir(exist_ok=True) - self.image_folder.mkdir(exist_ok=True) - - def compose(self, input_id: int, filters: Optional[List[str]] = None): if input_id < 10000: - chara_ids = fetch_data(input_id) - savefolder, tempfolder = self.output_folder / str(input_id), self.image_folder / str(input_id) + _chara_ids = fetch_data(input_id) + self.output_folder = _output_folder / str(input_id) + self.image_folder = _image_folder / str(input_id) else: LOGGER.info(f"Processing manually uploaded charaId {input_id}") - savefolder, tempfolder = self.output_folder / "manual", self.image_folder / "manual" - chara_ids = [str(input_id)] + _chara_ids = [str(input_id)] + self.output_folder = _output_folder / "manual" + self.image_folder = _image_folder / "manual" - savefolder.mkdir(parents=True, exist_ok=True) - tempfolder.mkdir(parents=True, exist_ok=True) + self.chara_ids = [ v for v in _chara_ids if v in filters ] if filters else _chara_ids + LOGGER.debug(self.chara_ids) + self.chara_infos = {} - chara_ids = [ v for v in chara_ids if v in filters ] if filters else chara_ids - LOGGER.debug(chara_ids) + self.output_folder.mkdir(parents=True, exist_ok=True) + self.image_folder.mkdir(parents=True, exist_ok=True) - for char_id in chara_ids: - expfolder = fetch_expression_sheets(tempfolder, char_id) + def download(self): + for char_id in self.chara_ids: + expfolder = fetch_expression_sheets(self.image_folder, char_id) config = fetch_config(char_id) - self.process_sprite(expfolder, config, savefolder) + self.chara_infos.update({char_id : { "folder": expfolder, "config": config}}) + LOGGER.debug(self.chara_infos) - LOGGER.info(f"Files have been saved at: {savefolder.absolute()}") + def compose(self): + for key, val in self.chara_infos.items(): + LOGGER.info(f"Processing sheet for {key}") + self.process_sprite(val["folder"], val["config"], self.output_folder) + LOGGER.info(f"Files have been saved at: {self.output_folder.absolute()}") def process_sprite(self, images_folder: pathlib.Path, configdata: SpritesheetData, outputfolder: pathlib.Path): main_sprite = self._gen_main_sprite(images_folder / "0.png")