UIGroup: add typing

This commit is contained in:
Rotzbua 2023-01-24 21:03:54 +01:00 committed by Florian Festi
parent c4cba4d604
commit f6fd3fce1c
1 changed files with 17 additions and 16 deletions

View File

@ -3,15 +3,17 @@ from __future__ import annotations
import importlib import importlib
import inspect import inspect
import pkgutil import pkgutil
from types import ModuleType
from typing import Any from typing import Any
import boxes import boxes
ui_groups_by_name = {} ui_groups_by_name = {}
class UIGroup: class UIGroup:
def __init__(self, name, title=None, description="", image="") -> None: def __init__(self, name: str, title: str | None = None, description: str = "", image: str = "") -> None:
self.name = name self.name = name
self.title = title or name self.title = title or name
self.description = description self.description = description
@ -20,19 +22,20 @@ class UIGroup:
# register # register
ui_groups_by_name[name] = self ui_groups_by_name[name] = self
def add(self, box): def add(self, box) -> None:
self.generators.append(box) self.generators.append(box)
self.generators.sort(key=lambda b:getattr(b, '__name__', None) or b.__class__.__name__) self.generators.sort(key=lambda b: getattr(b, '__name__', None) or b.__class__.__name__)
@property @property
def thumbnail(self): def thumbnail(self) -> str:
return self._image and f"{self._image}-thumb.jpg" return self._image and f"{self._image}-thumb.jpg"
@property @property
def image(self): def image(self) -> str:
return self._image and f"{self._image}.jpg" return self._image and f"{self._image}.jpg"
ui_groups = [
ui_groups: list[UIGroup] = [
UIGroup("Box", "Boxes", image="UniversalBox"), UIGroup("Box", "Boxes", image="UniversalBox"),
UIGroup("FlexBox", "Boxes with flex", image="RoundedBox"), UIGroup("FlexBox", "Boxes with flex", image="RoundedBox"),
UIGroup("Tray", "Trays and Drawer Inserts", image="TypeTray"), UIGroup("Tray", "Trays and Drawer Inserts", image="TypeTray"),
@ -42,31 +45,29 @@ ui_groups = [
UIGroup("Part", "Parts and Samples", image="BurnTest"), UIGroup("Part", "Parts and Samples", image="BurnTest"),
UIGroup("Misc", image="TrafficLight"), UIGroup("Misc", image="TrafficLight"),
UIGroup("Unstable", description="Generators are still untested or need manual adjustment to be useful."), UIGroup("Unstable", description="Generators are still untested or need manual adjustment to be useful."),
] ]
def getAllBoxGenerators():
def getAllBoxGenerators() -> dict[str, type[boxes.Boxes]]:
generators = {} generators = {}
for importer, modname, ispkg in pkgutil.walk_packages( for importer, modname, ispkg in pkgutil.walk_packages(path=__path__, prefix=__name__ + '.'):
path=__path__,
prefix=__name__+'.'):
module = importlib.import_module(modname) module = importlib.import_module(modname)
if module.__name__.split('.')[-1].startswith("_"): if module.__name__.split('.')[-1].startswith("_"):
continue continue
for k, v in module.__dict__.items(): for k, v in module.__dict__.items():
if v is boxes.Boxes: if v is boxes.Boxes:
continue continue
if (inspect.isclass(v) and issubclass(v, boxes.Boxes) and if inspect.isclass(v) and issubclass(v, boxes.Boxes) and v.__name__[0] != '_':
v.__name__[0] != '_'):
generators[modname + '.' + v.__name__] = v generators[modname + '.' + v.__name__] = v
return generators return generators
def getAllGeneratorModules():
def getAllGeneratorModules() -> dict[str, ModuleType]:
generators = {} generators = {}
for importer, modname, ispkg in pkgutil.walk_packages( for importer, modname, ispkg in pkgutil.walk_packages(
path=__path__, path=__path__,
prefix=__name__+'.', prefix=__name__ + '.',
onerror=lambda x: None): onerror=lambda x: None):
module = importlib.import_module(modname) module = importlib.import_module(modname)
generators[modname.split('.')[-1]] = module generators[modname.split('.')[-1]] = module
return generators return generators