Split download and compose into two functions
All checks were successful
/ pylint (push) Successful in 11s
/ mypy (push) Successful in 13s

This commit is contained in:
Firq 2024-10-20 19:11:26 +02:00
parent 27d7598f81
commit 821251b77c
Signed by: Firq
GPG key ID: 3ACC61C8CEC83C20
2 changed files with 34 additions and 21 deletions

View file

@ -91,4 +91,6 @@ def run():
rmdir(PathConfig.IMAGES) rmdir(PathConfig.IMAGES)
LOGGER.info("Successfully reset local storage") LOGGER.info("Successfully reset local storage")
SkyeWeave().compose(input_id, args.filter) weaver = SkyeWeave(input_id, args.filter)
weaver.download()
weaver.compose()

View file

@ -1,6 +1,6 @@
import logging import logging
import pathlib import pathlib
from typing import List, Optional from typing import Dict, List, Optional, TypedDict
from collections import Counter from collections import Counter
from PIL import Image from PIL import Image
@ -11,38 +11,49 @@ from .atlas import SpritesheetData, fetch_data, fetch_expression_sheets, fetch_c
LOGGER = logging.getLogger(LoggingConfig.NAME) LOGGER = logging.getLogger(LoggingConfig.NAME)
class CharaInfos(TypedDict):
folder: pathlib.Path
config: SpritesheetData
class SkyeWeave: class SkyeWeave:
output_folder: pathlib.Path output_folder: pathlib.Path
image_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): def __init__(self, input_id: int, filters: Optional[List[str]] = None, output: Optional[pathlib.Path] = None, assets: Optional[pathlib.Path] = None):
self.output_folder = output or PathConfig.OUTPUT _output_folder = output or PathConfig.OUTPUT
self.image_folder = assets or PathConfig.IMAGES _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: if input_id < 10000:
chara_ids = fetch_data(input_id) _chara_ids = fetch_data(input_id)
savefolder, tempfolder = self.output_folder / str(input_id), self.image_folder / str(input_id) self.output_folder = _output_folder / str(input_id)
self.image_folder = _image_folder / str(input_id)
else: else:
LOGGER.info(f"Processing manually uploaded charaId {input_id}") 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) self.chara_ids = [ v for v in _chara_ids if v in filters ] if filters else _chara_ids
tempfolder.mkdir(parents=True, exist_ok=True) 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 self.output_folder.mkdir(parents=True, exist_ok=True)
LOGGER.debug(chara_ids) self.image_folder.mkdir(parents=True, exist_ok=True)
for char_id in chara_ids: def download(self):
expfolder = fetch_expression_sheets(tempfolder, char_id) for char_id in self.chara_ids:
expfolder = fetch_expression_sheets(self.image_folder, char_id)
config = fetch_config(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): def process_sprite(self, images_folder: pathlib.Path, configdata: SpritesheetData, outputfolder: pathlib.Path):
main_sprite = self._gen_main_sprite(images_folder / "0.png") main_sprite = self._gen_main_sprite(images_folder / "0.png")