Skip to content

factory

dev_tool.tui.menu.factory

log = logging.getLogger(__name__) module-attribute

MenuFactory

A factory class for creating TUI menus.

This class provides static methods for creating different types of menus.

create_main_menu staticmethod

A method that creates the main menu.

Returns:

  • Menu

    The main menu.

Source code in dev_tool/tui/menu/factory.py
@staticmethod
def create_main_menu() -> Menu:
    """
    A method that creates the main menu.

    :return: The main menu.
    """

    tabs = {}

    for specification in REGISTRY.values():
        label = specification['label']
        title = specification['category']
        command = specification['bound']
        group = specification['group']
        order = specification['order']

        if title not in tabs:
            tabs[title] = []

        match group:
            case ActionGroup.COMMAND:
                action = CommandAction(command=command)
                item = MenuItem(label=label, action=action)
            case ActionGroup.SUBMENU:
                action = SubmenuAction(submenu=command)
                item = MenuItem(label=label, action=action)

        pair = (order, item)
        tabs[title].append(pair)

    for title, items in tabs.items():
        items.sort(key=lambda x: x[0])
        tabs[title] = [item for order, item in items]

    items: list[Menu | MenuItem] = [
        Menu(title=title, items=items)
        for title, items in tabs.items()
    ]

    return Menu(title='Main Menu', items=items)