def test_init(self): root_menu = CursesMenu("root_menu", "test_init") submenu1 = CursesMenu("submenu1", "test_init") submenu2 = CursesMenu("submenu2", "test_init") submenu3 = CursesMenu("submenu2", "test_init") menu_item_1 = SubmenuItem("menu_item_1", submenu1) menu_item_2 = SubmenuItem("menu_item_2", submenu2, root_menu, True) menu_item_3 = SubmenuItem(text="menu_item_3", submenu=submenu3, menu=root_menu, should_exit=False) self.assertEqual(menu_item_1.text, "menu_item_1") self.assertEqual(menu_item_2.text, "menu_item_2") self.assertEqual(menu_item_3.text, "menu_item_3") self.assertIsNone(menu_item_1.menu) self.assertEqual(menu_item_2.menu, root_menu) self.assertEqual(menu_item_3.menu, root_menu) self.assertFalse(menu_item_1.should_exit) self.assertTrue(menu_item_2.should_exit) self.assertFalse(menu_item_3.should_exit) self.assertEqual(menu_item_1.submenu, submenu1) self.assertEqual(menu_item_2.submenu, submenu2) self.assertEqual(menu_item_3.submenu, submenu3) self.assertIsNone(menu_item_1.submenu.parent) self.assertEqual(menu_item_2.submenu.parent, root_menu) self.assertEqual(menu_item_3.submenu.parent, root_menu)
def create_menu(self) -> None: # Create the menu self.menu = CursesMenu("PyMeterReader Configuration Wizard", "Choose item to configure") function_item = FunctionItem("Volkszähler Gateway", self.input_gw, ["Enter URL: "], should_exit=True) self.menu.append_item(function_item) for meter in self.meters: meter_menu = CursesMenu( f"Connect channels for meter {meter.meter_id} at {meter.meter_address}", "By channel") for channel in meter.channels: map_menu = CursesMenu( f"Choose uuid for {channel.channel_name}") for choice in self.gateway_channels: map_menu.append_item( FunctionItem(f"{choice.uuid}: {choice.title}", self.__assign, [meter, channel, choice.uuid, '30m'], should_exit=True)) map_menu.append_item( FunctionItem("Enter private UUID", self.__assign, [meter, channel, None, '30m'], should_exit=True)) meter_menu.append_item( SubmenuItem( f"{channel.channel_name}: {channel.value} {channel.unit}", map_menu, self.menu)) submenu_item = SubmenuItem(f"Meter {meter.meter_id}", meter_menu, self.menu) self.menu.append_item(submenu_item) view_item = FunctionItem("View current mapping", self.__view_mapping) self.menu.append_item(view_item) save_item = FunctionItem("Save current mapping", self.__safe_mapping) self.menu.append_item(save_item) register_service = FunctionItem( "Register PymeterReader as systemd service.", self.__register_service) self.menu.append_item(register_service) reset_item = FunctionItem("Reset all mappings", self.__clear) self.menu.append_item(reset_item) self.menu.show()
def test_action(self): root_menu = CursesMenu("root_menu", "test_action") submenu1 = CursesMenu("submenu1", "test_action") submenu2 = CursesMenu("submenu2", "test_action") submenu_item_1 = SubmenuItem("submenu_item_1", submenu1, menu=root_menu) submenu_item_2 = SubmenuItem("submenu_item_2", submenu2, menu=root_menu) root_menu.append_item(submenu_item_1) root_menu.append_item(submenu_item_2) root_menu.start() root_menu.wait_for_start(timeout=10) self.assertIs(CursesMenu.currently_active_menu, root_menu) submenu_item_1.action() submenu1.wait_for_start(timeout=10) self.assertIs(CursesMenu.currently_active_menu, submenu1) CursesMenu.currently_active_menu.exit() submenu1.join(timeout=10) self.assertIs(CursesMenu.currently_active_menu, root_menu) submenu_item_2.action() submenu2.wait_for_start(timeout=10) self.assertIs(CursesMenu.currently_active_menu, submenu2)
def generate_menu(menu=None, menu_cfg=None, parent_title=None): if not menu_cfg: menu_cfg = c.get_menu() if not menu: title = get_string(menu_cfg, 'title') subtitle = get_string(menu_cfg, 'subtitle', params=parent_title) menu = CursesMenu(title, subtitle) options = menu_cfg['options'] if type(options) == str and get_string(menu_cfg, 'type') == 'submenu': options_list = c.get_config()[options] for option in options_list: if 'on_item_select' in menu_cfg: title = get_string(menu_cfg, 'title') subtitle = get_string(menu_cfg['on_item_select'], 'subtitle', params=option) submenu = CursesMenu(title, subtitle) option_menu = generate_menu(menu_cfg=menu_cfg['on_item_select'], parent_title=option) item = SubmenuItem(option, option_menu, menu=submenu) else: item = FunctionItem(option, getattr(c.get_action_module(), menu_cfg['action']), [option]) # TODO allow for customisation of module name menu.append_item(item) else: for option in menu_cfg['options']: cmd_type = get_string(option, 'type') title = get_string(option, 'title') action = get_string(option, 'action') subtitle = get_string(option, 'subtitle') if cmd_type == 'function': item = FunctionItem(title, getattr(actions, action)) elif cmd_type == 'submenu': submenu = CursesMenu(title, subtitle) item = SubmenuItem(title, submenu, menu=menu) generate_menu(submenu, option, title) else: item = MenuItem(title) menu.append_item(item) return menu
def parse_old_menu(menu_data): """ Take an old-style menuData dictionary and return a CursesMenu :param dict menu_data: :return: A new CursesMenu :rtype: CursesMenu """ menu_title = menu_data['title'] menu = CursesMenu(menu_title) for item in menu_data["options"]: item_type = item["type"] item_title = item["title"] if item_type == menuItem.COMMAND: item_command = item["command"] menu.append_item(CommandItem(item_title, item_command, menu)) elif item_type == menuItem.FUNCTION: item_function = item["function"] menu.append_item(FunctionItem(item_title, item_function, menu)) elif item_type == menuItem.EXITMENU: menu.append_item(ExitItem(item_title, menu)) elif item_type == menuItem.NUMBER: menu.append_item(SelectionItem(item_title, menu)) elif item_type == menuItem.MENU: new_menu = parse_old_menu(item) menu.append_item(SubmenuItem(item_title, menu, new_menu)) return menu
def submenu_logging(menu_obj): submenu = CursesMenu("Logging Configuration") q1_item = FunctionItem("Do you want to turn enable logging?", input, ['yes']) q2_item = FunctionItem( "If yes, do you want logging to stdout or a log file?", input, ['FILE']) submenu.append_item(q1_item) submenu.append_item(q2_item) submenu_item = SubmenuItem("Show a submenu", submenu, menu=menu_obj) return submenu_item
def submenu_backup(menu_obj): submenu = CursesMenu("Access Key Backup") q1_item = FunctionItem( "Do you want to retain a copy of newly created access keys?", input, ["yes"]) q2_item = FunctionItem( "Enter the directory where a backup copy of the access keys should be stored", input, ["~/Backup"]) submenu.append_item(q1_item) submenu.append_item(q2_item) submenu_item = SubmenuItem("Show a submenu", submenu, menu=menu_obj) return submenu_item
def step_impl(context): context.menu = CursesMenu("Test Menu", "Subtitle") context.menu_item = MenuItem("NORMAL", "Menu Item") context.menu.append_item(context.menu_item) context.function_item = FunctionItem("NORMAL", "Call a Python function", input, ["Enter an input"]) context.menu.append_item(context.function_item) context.command_item = CommandItem("NORMAL", "Run a console command", "ipconfig /all") context.menu.append_item(context.command_item) context.selection_menu = SelectionMenu(["item1", "item2", "item3"]) context.selection_submenu_item = SubmenuItem("NORMAL", "Submenu item", context.selection_menu) context.menu.append_item(context.selection_submenu_item) pass
print('Unknown Exception: %s. Exit' % str(e)) return False return True if __name__ == '__main__': menu = CursesMenu("Local Configuration Menu", "xlines Project") # backup submenu = CursesMenu("Exclusions", 'Do you want to add a new file type exclusion?') q1_item1 = FunctionItem("Yes", input, ["yes"]) q1_item2 = FunctionItem("No", print('Exit'), ["no"]) submenu.append_item(q1_item1) submenu.append_item(q1_item2) submenu_exclusions = SubmenuItem("Configure Exclusion List", submenu, menu=menu) menu.append_item(submenu_exclusions) if q1_item1 == "yes": display_exclusions() submenu = CursesMenu( "Access Key Backup", 'Enter the directory where a backup copy of the access keys should be stored' ) q2_item1 = FunctionItem("~/Backup/", input, ["~/Backup"]) submenu.append(q2_item1) submenu.show() # logging submenu2 = CursesMenu("Logging Configuration", "Do you want to enable logging?")
from modules import SSH as ssh from modules import SshX as ssh_X from modules import Xfce as xfce # Створення основного меню menu = CursesMenu("Ubuntu universal script", platform.version()) # Створення основних пунктів _menu_items = [ CommandItem("Update paсkages", "sudo apt update"), CommandItem("Upgrade paсkages", "sudo apt upgrade"), CommandItem("Autoremove paсkages", "sudo apt autoremove"), CommandItem("Remove other kernel`s", "bash bash/remove_kernel.sh"), CommandItem( "Cleaning pickings removed paсkages", "sudo dpkg -l | awk '/^rc/ {print $2}' | xargs sudo dpkg --purge"), # Відображення підменю SubmenuItem("Install soft", soft.menu, menu), SubmenuItem("Remove software", remove.menu, menu), SubmenuItem("Xfce soft", xfce.menu, menu), SubmenuItem("SSH Connect", ssh.menu, menu), SubmenuItem("SSH connect with X-window support", ssh_X.menu, menu) ] # Додавання пунктів до основного меню for item in _menu_items: menu.append_item(item) # Показ меню menu.show()
def step_impl(context): context.picker_menu = PickerMenu(["item1", "item2", "item3"]) context.picker_submenu_item = SubmenuItem("NORMAL", "Submenu item", context.picker_menu) context.menu.append_item(context.picker_submenu_item) pass
def step_impl(context): context.selection_menu = SelectionMenu(["item1", "item2", "item3"]) context.selection_submenu_item = SubmenuItem("NORMAL", "Submenu item", context.selection_menu) context.menu.append_item(context.selection_submenu_item) pass
# Create the menu menu = CursesMenu("Title", "Subtitle") # Create some items # MenuItem is the base class for all items, it doesn't do anything when selected menu_item = MenuItem("Menu Item") # A FunctionItem runs a Python function when selected function_item = FunctionItem("Call a Python function", input, ["Enter an input"]) # A CommandItem runs a console command command_item = CommandItem("Run a console command", "touch hello.txt") # A SelectionMenu constructs a menu from a list of strings selection_menu = SelectionMenu(["item1", "item2", "item3"]) # A SubmenuItem lets you add a menu (the selection_menu above, for example) # as a submenu of another menu submenu_item = SubmenuItem("Submenu item", selection_menu, menu) # Once we're done creating them, we just add the items to the menu menu.append_item(menu_item) menu.append_item(function_item) menu.append_item(command_item) menu.append_item(submenu_item) # Finally, we call show to show the menu and allow the user to interact menu.show()