| 11 | |
| 12 | |
| 13 | class ManualNetworkConfig(ListManager[Nic]): |
| 14 | def __init__(self, prompt: str, preset: list[Nic]): |
| 15 | self._actions = [ |
| 16 | tr('Add interface'), |
| 17 | tr('Edit interface'), |
| 18 | tr('Delete interface'), |
| 19 | ] |
| 20 | |
| 21 | super().__init__( |
| 22 | preset, |
| 23 | [self._actions[0]], |
| 24 | self._actions[1:], |
| 25 | prompt, |
| 26 | ) |
| 27 | |
| 28 | async def show(self) -> list[Nic] | None: |
| 29 | return await super()._run() |
| 30 | |
| 31 | @override |
| 32 | def selected_action_display(self, selection: Nic) -> str: |
| 33 | return selection.iface if selection.iface else '' |
| 34 | |
| 35 | @override |
| 36 | async def handle_action(self, action: str, entry: Nic | None, data: list[Nic]) -> list[Nic]: |
| 37 | if action == self._actions[0]: # add |
| 38 | iface = await self._select_iface(data) |
| 39 | if iface: |
| 40 | nic = Nic(iface=iface) |
| 41 | nic = await self._edit_iface(nic) |
| 42 | data += [nic] |
| 43 | elif entry: |
| 44 | if action == self._actions[1]: # edit interface |
| 45 | data = [d for d in data if d.iface != entry.iface] |
| 46 | nic = await self._edit_iface(entry) |
| 47 | data.append(nic) |
| 48 | elif action == self._actions[2]: # delete |
| 49 | data = [d for d in data if d != entry] |
| 50 | |
| 51 | return data |
| 52 | |
| 53 | async def _select_iface(self, data: list[Nic]) -> str | None: |
| 54 | all_ifaces = list_interfaces().values() |
| 55 | existing_ifaces = [d.iface for d in data] |
| 56 | available = set(all_ifaces) - set(existing_ifaces) |
| 57 | |
| 58 | if not available: |
| 59 | return None |
| 60 | |
| 61 | if not available: |
| 62 | return None |
| 63 | |
| 64 | items = [MenuItem(i, value=i) for i in available] |
| 65 | group = MenuItemGroup(items, sort_items=True) |
| 66 | |
| 67 | result = await Selection[str]( |
| 68 | group, |
| 69 | header=tr('Select an interface'), |
| 70 | allow_skip=True, |