refactor: refactor toolset (not finished)

This commit is contained in:
2025-07-23 22:35:47 +08:00
parent 685a0b8066
commit 9f591113f8
10 changed files with 658 additions and 54 deletions

View File

@ -1,55 +1,51 @@
import os
import bme_utils
import logging
from pathlib import Path
import common
import PIL, PIL.Image
# the config for thumbnail
g_ThumbnailSize: int = 16
THUMBNAIL_SIZE: int = 16
class ThumbnailCreator():
__mReporter: bme_utils.Reporter
class ThumbnailBuilder():
def __init__(self):
self.__mReporter = bme_utils.Reporter()
pass
def run(self) -> None:
self.__create_thumbnails()
def __create_thumbnails(self) -> None:
def build_thumbnails(self) -> None:
# get folder path
root_folder: str = common.get_plugin_folder()
root_folder = common.get_plugin_folder()
# prepare handler
def folder_handler(rel_name: str, src_folder: str, dst_folder: str) -> None:
def folder_handler(rel_name: str, src_folder: Path, dst_folder: Path) -> None:
# just create folder
self.__mReporter.info(f'Creating Folder: {src_folder} -> {dst_folder}')
os.makedirs(dst_folder, exist_ok = True)
def file_handler(rel_name: str, src_file: str, dst_file: str) -> None:
logging.info(f'Creating Folder: {src_folder} -> {dst_folder}')
dst_folder.mkdir(parents=False, exist_ok=True)
def file_handler(rel_name: str, src_file: Path, dst_file: Path) -> None:
# skip non-image
if not src_file.endswith('.png'): return
if src_file.suffix != '.png': return
# call thumbnail func
self.__mReporter.info(f'Processing Thumbnail: {src_file} -> {dst_file}')
logging.info(f'Building Thumbnail: {src_file} -> {dst_file}')
self.__resize_image(src_file, dst_file)
# call common processor
common.common_file_migrator(
os.path.join(root_folder, 'raw_icons'),
os.path.join(root_folder, 'icons'),
root_folder / 'raw_icons',
root_folder / 'icons',
folder_handler,
file_handler
)
self.__mReporter.info('Building thumbnail done.')
logging.info('Building thumbnail done.')
def __resize_image(self, src_file: str, dst_file: str) -> None:
def __resize_image(self, src_file: Path, dst_file: Path) -> None:
# open image
src_image: PIL.Image.Image = PIL.Image.open(src_file)
# create thumbnail
src_image.thumbnail((g_ThumbnailSize, g_ThumbnailSize))
src_image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE))
# save to new file
src_image.save(dst_file)
if __name__ == '__main__':
thumbnail_creator = ThumbnailCreator()
thumbnail_creator.run()
common.setup_logging()
thumbnail_builder = ThumbnailBuilder()
thumbnail_builder.build_thumbnails()