def display_commands(self, user=None): """[displats all commands for user] """ if user: menu = CursesMenu("Spotify Collobrative Playlist", "User: "******"Spotify Collobrative Playlist", "User", show_exit_option=True) sl1 = SelectionItem("Choose Active Playlist", 1) sl2 = SelectionItem("Add new song to playlist", 2) sl3 = SelectionItem("List Playlists", 3) sl4 = SelectionItem("Print Songs in Playlist", 4) sl5 = SelectionItem("Create Playlist", 5) sl6 = SelectionItem("Exit", 6) menu.append_item(sl1) menu.append_item(sl2) menu.append_item(sl3) menu.append_item(sl4) menu.append_item(sl5) menu.append_item(sl6) menu.start() menu.join() return menu.returned_value
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 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 test_currently_active_menu(self): menu1 = CursesMenu("menu1", "test_currently_active_menu") menu2 = CursesMenu("menu2", "test_currently_active_menu") self.assertIsNone(CursesMenu.currently_active_menu) menu1.start() menu1.wait_for_start(10) self.assertIs(CursesMenu.currently_active_menu, menu1) menu2.start() menu2.wait_for_start(10) self.assertIs(CursesMenu.currently_active_menu, menu2)
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_init(self): menu1 = CursesMenu() menu2 = CursesMenu("menu2", "test_init", True) menu3 = CursesMenu(title="menu3", subtitle="test_init", show_exit_option=False) self.assertIsNone(menu1.title) self.assertEqual(menu2.title, "menu2") self.assertEqual(menu3.title, "menu3") self.assertIsNone(menu1.subtitle) self.assertEqual(menu2.subtitle, "test_init") self.assertEqual(menu3.subtitle, "test_init") self.assertTrue(menu1.show_exit_option) self.assertTrue(menu2.show_exit_option) self.assertFalse(menu3.show_exit_option)
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
class RemoveProgram: """ # # Видалення програм # """ # Основне меню menu = CursesMenu("Remove software", "YOU DELETE SOFT!!!") # Пункти меню _menu_items = [ CommandItem("All software", "bash bash/remove.sh"), CommandItem("ssh", "sudo apt autoremove ssh"), CommandItem("Gparted", "sudo apt autoremove gparted"), CommandItem("VLC", "sudo apt autoremove vlc"), CommandItem("screenfetch", "sudo apt autoremove screenfetch"), CommandItem("f.lux", "sudo apt autoremove flux"), CommandItem("gimp", "sudo apt autoremove gimp"), CommandItem("Kdenlive", "sudo apt autoremove kdenlive"), CommandItem("Wine", "sudo apt autoremove wine"), CommandItem("Rhythmbox", "sudo apt autoremove rhythmbox") ] # Додавання пунктів до меню for item in _menu_items: menu.append_item(item)
class Xfce: """ # # Модуль налаштувань для XFCE # """ # Основне меню menu = CursesMenu("XFCE soft") # Пункти меню _menu_items = [ CommandItem("All remove", "bash bash/xfce.sh"), CommandItem("Remove XFCE4-Notes", "sudo apt autoremove xfce4-notes"), CommandItem("Remove Pidgin", "sudo apt autoremove pidgin"), CommandItem("Remove Thunderbird", "sudo apt autoremove thunderird"), CommandItem("Remove Parole", "sudo apt autoremove parole"), CommandItem("Remove Sudoku", "sudo apt autoremove gnome-sudoku"), CommandItem("Remove Mines", "sudo apt autoremove gnome-mines"), CommandItem("Remove firefox", "sudo apt autoremove firefox") ] # Додавання пунктів до меню for item in _menu_items: menu.append_item(item)
class InstallSoft: """ # # Модуль встановлення програм # """ # Основне меню menu = CursesMenu("Install software", "DEPENDENCES!!!") # Пункти меню _menu_items = [ CommandItem("All software", "bash bash/install.sh"), CommandItem("ssh", "sudo apt install ssh"), CommandItem("Gparted", "sudo apt install gparted"), CommandItem("VLC", "sudo apt install vlc"), CommandItem("screenfetch", "sudo apt install screenfetch"), CommandItem("f.lux", "bash bash/f.lux.sh"), CommandItem("gimp", "sudo apt install gimp"), CommandItem("Kdenlive", "sudo apt install kdenlive"), CommandItem("Wine", "sudo apt install wine"), CommandItem("Rhythmbox", "sudo apt install rhythmbox") ] # Додавання пунктів до меню for item in _menu_items: menu.append_item(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) pass
def run(self): menu = CursesMenu("Openstack Toolset", "Restore OpenStack Virtual Machine Block Device from SAN - By Real World") options = ['Restore State'] for idx, item in enumerate(options): menu.append_item(SelectionItem(item, idx)) submenu = CursesMenu("Contact the Author.") contact = SubmenuItem("Author: Karl Kloppenborg", submenu=submenu) contact2 = SubmenuItem("Email: [email protected]", submenu=submenu) submenu.append_item(contact) submenu.append_item(contact2) sub = SubmenuItem("Help!", submenu, menu=menu) menu.append_item(sub) menu.show() return menu.selected_option
def setUp(self): super(TestSampleMenu, self).setUp() self.menu = CursesMenu("self.menu", "TestSampleMenu") self.item1 = MenuItem("self.item1", self.menu) self.item2 = MenuItem("self.item2", self.menu) self.menu.append_item(self.item1) self.menu.append_item(self.item2) self.menu.start() self.menu.wait_for_start(timeout=10)
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 create_menu(list_dict_stories, type_new): title = 'Pynews - {} stories'.format(type_new.capitalize()) menu = CursesMenu(title, 'Select the new and press enter') msg = 'This new does not have an URL' for story in list_dict_stories: title = clean_title(story['title']) if 'url' in story: item = FunctionItem(title, url_open, args=[story['url']]) else: item = FunctionItem(title, lambda x: print(x), args=[msg]) menu.append_item(item) return menu
def generate_menu(tasks): """ Create menu for process selection """ menu = CursesMenu("Select something to kill him!") # a CommandItem runs a console command for pid, name in tasks.items(): label = "{:<8} - {}".format('[' + str(pid) + ']', name[:40]) command = "kill {}".format(pid) menu.append_item(cmitems.CommandItem(label, command)) # show the menu and allow the user to interact menu.show()
def show_menu(self): self.menu = CursesMenu('bust - Module Editor', self.set_subtitle()) self.menu.append_item(FunctionItem('Edit name', self.edit_name)) self.menu.append_item( FunctionItem('List registers', self.list_registers)) self.menu.append_item( FunctionItem('Add new register', self.add_register)) self.menu.append_item( FunctionItem('Remove register', self.remove_register)) self.menu.append_item( FunctionItem('Update addresses', self.update_addresses)) self.menu.append_item(FunctionItem('Save JSON', self.save_JSON)) self.menu.show()
def test_init(self): menu = CursesMenu("menu", "test_init") exit_item_1 = ExitItem("exit_item_1") exit_item_2 = ExitItem("exit_item_2", menu) exit_item_3 = ExitItem(text="exit_item_3", menu=menu) self.assertEqual(exit_item_1.text, "exit_item_1") self.assertEqual(exit_item_2.text, "exit_item_2") self.assertEqual(exit_item_3.text, "exit_item_3") self.assertEqual(exit_item_1.menu, None) self.assertEqual(exit_item_2.menu, menu) self.assertEqual(exit_item_3.menu, menu) self.assertTrue(exit_item_1.should_exit) self.assertTrue(exit_item_2.should_exit) self.assertTrue(exit_item_3.should_exit)
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
class SshX: """ # # Модуль підключення SSH з підтримкою запуска програм з графічним інтерфейсом # """ menu = CursesMenu("SSH connect with X-window support") # Пункти _menu_items = [ CommandItem("Home ms-7519(ivan)", "ssh -X [email protected]"), CommandItem("Acer-aspire V5-131(ivan)", "ssh -X [email protected]"), CommandItem("Lenovo B560(diana)", "ssh -X [email protected]"), CommandItem("Server(home)", "ssh -X [email protected]") ] # Додаваня пунктів до меню for item in _menu_items: menu.append_item(item)
def main(): """ Builds main menu, branches to submenus """ # parent menu menu = CursesMenu("Local Configuration Menu", "keyup Project") try: submenu_backup = submenu_backup(menu) submenu_logging = submenu_logging(menu) # assemble main menu menu.append_item(submenu_backup) menu.append_item(submenu_logging) menu.show() user_selection = menu.selected_option except Exception as e: print('Unknown Exception: %s. Exit' % str(e)) return False return True
class SSH: """ # # SSH підключення # """ # Основне меню menu = CursesMenu("SSH Connect") # Пунктии меню _menu_items = [ CommandItem("DIR 615-E4", "ssh [email protected]"), CommandItem("Home ms-7519(ivan)", "ssh [email protected]"), CommandItem("Acer-aspire V5-131(ivan)", "ssh [email protected]"), CommandItem("Lenovo B560(diana)", "ssh [email protected]"), CommandItem("Server(home)", "ssh [email protected]") ] # Додавання пунктів до меню for item in _menu_items: menu.append_item(item)
def step_impl(context): context.menu = CursesMenu("Test Menu", "Subtitle") pass
# Example copied from pypi.org/project/curses-menu/ # Import the necessary packages from cursesmenu import CursesMenu, SelectionMenu from cursesmenu.items import MenuItem, FunctionItem, CommandItem, SubmenuItem # 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)
submenu_logging = submenu_logging(menu) # assemble main menu menu.append_item(submenu_backup) menu.append_item(submenu_logging) menu.show() user_selection = menu.selected_option except Exception as e: 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",
def setUp(self): super(TestCommandItem, self).setUp() self.menu = CursesMenu("self.menu", "TestCommandItem")
def step_impl(context): context.menu = CursesMenu("Test Menu", "Subtitle") context.menu_item = MenuItem("NORMAL", "Menu Item") context.menu.append_item(context.menu_item) pass
# Основне меню. Запускати програму з цього файла # # Класи для створення меню from cursesmenu import CursesMenu from cursesmenu.items import CommandItem, SubmenuItem import platform from modules import InstallSoft as soft from modules import RemoveProgram as remove 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),