boxespy/boxes/generators/__init__.py

74 lines
2.4 KiB
Python
Raw Permalink Normal View History

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