| 3 | |
| 4 | |
| 5 | class MenuHelper[ValueT]: |
| 6 | def __init__( |
| 7 | self, |
| 8 | data: list[ValueT], |
| 9 | additional_options: list[str] = [], |
| 10 | ) -> None: |
| 11 | self._separator = '' |
| 12 | self._data = data |
| 13 | self._additional_options = additional_options |
| 14 | |
| 15 | def create_menu_group(self) -> MenuItemGroup: |
| 16 | table_data_mapping = self._table_to_data_mapping(self._data) |
| 17 | |
| 18 | items = [] |
| 19 | for key, value in table_data_mapping.items(): |
| 20 | item = MenuItem(key, value=value) |
| 21 | |
| 22 | if value is None: |
| 23 | item.read_only = True |
| 24 | |
| 25 | items.append(item) |
| 26 | |
| 27 | group = MenuItemGroup(items, sort_items=False) |
| 28 | |
| 29 | return group |
| 30 | |
| 31 | def _table_to_data_mapping(self, data: list[ValueT]) -> dict[str, ValueT | str | None]: |
| 32 | display_data: dict[str, ValueT | str | None] = {} |
| 33 | |
| 34 | if data: |
| 35 | table = as_table(data) |
| 36 | rows = table.split('\n') |
| 37 | |
| 38 | # these are the header rows of the table |
| 39 | display_data = {f'{rows[0]}': None, f'{rows[1]}': None} |
| 40 | |
| 41 | for row, entry in zip(rows[2:], data): |
| 42 | display_data[row] = entry |
| 43 | |
| 44 | if self._additional_options: |
| 45 | display_data[self._separator] = None |
| 46 | |
| 47 | for option in self._additional_options: |
| 48 | display_data[option] = option |
| 49 | |
| 50 | return display_data |
no outgoing calls
no test coverage detected