From 3f29e096a2846a66c316f25df16f3dd6ebb194bc Mon Sep 17 00:00:00 2001 From: dalthviz <16781833+dalthviz@users.noreply.github.com> Date: Wed, 2 Jul 2025 18:54:16 -0500 Subject: [PATCH] Add args to get custom palette from file. Fix logging level setup. --- qdarkstyle/utils/__init__.py | 12 ++--- qdarkstyle/utils/__main__.py | 93 ++++++++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 31 deletions(-) diff --git a/qdarkstyle/utils/__init__.py b/qdarkstyle/utils/__init__.py index bb98fa9..dde6e62 100644 --- a/qdarkstyle/utils/__init__.py +++ b/qdarkstyle/utils/__init__.py @@ -79,7 +79,7 @@ def process_palette( sys.exit(1) id_ = palette.ID - print(f"-- PROCESSING THEME: {id_}") + _logger.info(f"-- PROCESSING THEME: {id_}") # Create base palette directory and files from id palette_path = os.path.join(base_path, id_) @@ -128,17 +128,17 @@ def process_palette( # TODO: delete/remove all files and folders to ensure that old files # are not used - print(f"-- GENERATING PALETTE IMAGE FOR: {id_}") + _logger.info(f"-- GENERATING PALETTE IMAGE FOR: {id_}") create_palette_image( palette=palette, base_svg_path=base_svg_path, path=images_path ) - print(f"-- GENERATING IMAGE FILES (.svg > .png) FOR: {id_}") + _logger.info(f"-- GENERATING IMAGE FILES (.svg > .png) FOR: {id_}") create_images( base_svg_path=base_svg_path, base_path=base_path, palette=palette ) - print(f"-- GENERATING QRC FILE FOR: {id_}") + _logger.info(f"-- GENERATING QRC FILE FOR: {id_}") generate_qrc_file( resource_prefix=resource_prefix, style_prefix=style_prefix, @@ -146,10 +146,10 @@ def process_palette( base_path=base_path, ) - print(f"-- GENERATING QSS FILE (.scss > .qss) FOR: {id_}") + _logger.info(f"-- GENERATING QSS FILE (.scss > .qss) FOR: {id_}") create_qss(palette=palette, base_path=base_path) - print(f"-- CONVERTING RESOURCE FILE (. qrc > _rc.py/.rcc) FOR: {id_}") + _logger.info(f"-- CONVERTING RESOURCE FILE (. qrc > _rc.py/.rcc) FOR: {id_}") compile_qrc_file( compile_for=compile_for, palette=palette, diff --git a/qdarkstyle/utils/__main__.py b/qdarkstyle/utils/__main__.py index 199b3e0..ee7562d 100644 --- a/qdarkstyle/utils/__main__.py +++ b/qdarkstyle/utils/__main__.py @@ -24,6 +24,7 @@ Links to understand those tools: # Standard library imports import argparse +import importlib.util import logging import sys @@ -58,7 +59,7 @@ class QSSFileHandler(FileSystemEventHandler): print('\n') -# See https://sumit-ghosh.com/posts/parsing-dictionary-key-value-pairs-kwargs-argparse-python/ +# Based on https://sumit-ghosh.com/posts/parsing-dictionary-key-value-pairs-kwargs-argparse-python/ class CustomPaletteParser(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): setattr(namespace, self.dest, dict()) @@ -67,8 +68,19 @@ class CustomPaletteParser(argparse.Action): getattr(namespace, self.dest)[key] = value +def import_from_file(module_name, file_path): + # Taken from: https://gist.github.com/mportesdev/afb2ec26021ccabee0f67d6f7d18be3f + spec = importlib.util.spec_from_file_location(module_name, file_path) + module = importlib.util.module_from_spec(spec) + sys.modules[module_name] = module + spec.loader.exec_module(module) + + return module + + def main(): """Process QRC files.""" + logging.basicConfig(level=logging.DEBUG) parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( @@ -77,26 +89,6 @@ def main(): type=str, help="Base QRC file directory.", ) - parser.add_argument( - "--create", - default="qtpy", - choices=[ - "pyqt5", - "pyqt6", - "pyside2", - "pyside6", - "qtpy", - "pyqtgraph", - "qt", - "qt5", - "all", - ], - type=str, - help="Choose which one would be generated.", - ) - parser.add_argument( - "--watch", "-w", action="store_true", help="Watch for file changes." - ) parser.add_argument( "--base_svg_path", default=SVG_PATH, @@ -122,9 +114,48 @@ def main(): help="Prefix used in resources.", ) parser.add_argument( - "--custom_palette", + "--custom_palette_key_value", nargs="*", action=CustomPaletteParser, + help="List of `key=value` definitions for a custom palette. " + "Alternative to `--custom_palette_file` + " + "`--custom_palette_class_name` args.", + ) + parser.add_argument( + "--custom_palette_file", + type=str, + help="Path to a Python file with the custom Palette subclass " + "definition. Alternative to `--custom_palette_key_value` arg. " + "Needs to be used alongside `--custom_palette_class_name` " + "to work.", + ) + parser.add_argument( + "--custom_palette_class_name", + type=str, + help="Class name importable from the given Python file with the custom " + "Palette subclass definition. Alternative to " + "`--custom_palette_key_value` arg. Needs to be used alongside " + "`--custom_palette_file` to work.", + ) + parser.add_argument( + "--create", + default="qtpy", + choices=[ + "pyqt5", + "pyqt6", + "pyside2", + "pyside6", + "qtpy", + "pyqtgraph", + "qt", + "qt5", + "all", + ], + type=str, + help="Choose which one would be generated.", + ) + parser.add_argument( + "--watch", "-w", action="store_true", help="Watch for file changes." ) args = parser.parse_args() @@ -140,7 +171,7 @@ def main(): except KeyboardInterrupt: observer.stop() observer.join() - elif args.custom_palette: + elif args.custom_palette_key_value: process_palette( palette=Palette.from_dict( args.custom_palette, @@ -151,7 +182,20 @@ def main(): images_path=args.images_path, base_path=args.base_path, ) - + elif args.custom_palette_file and args.custom_palette_class_name: + custom_palette_module = import_from_file( + "palette", args.custom_palette_file, + ) + custom_palette_class = getattr( + custom_palette_module, args.custom_palette_class_name, + ) + process_palette( + palette=custom_palette_class, + compile_for=args.create, + base_svg_path=args.base_svg_path, + images_path=args.images_path, + base_path=args.base_path, + ) else: for palette in [DarkPalette, LightPalette]: process_palette( @@ -164,5 +208,4 @@ def main(): if __name__ == "__main__": - logging.basicConfig(level=logging.DEBUG) sys.exit(main())