def test_init(self): command_item_1 = CommandItem("command_item_1", "exit") command_item_2 = CommandItem("command_item_2", "ls", ["-l", "-a", "~"], self.menu, True) command_item_3 = CommandItem(text="command_item_3", command="rm", menu=self.menu, arguments=["-r", "-f", "./test"], should_exit=False) self.assertEqual(command_item_1.text, "command_item_1") self.assertEqual(command_item_2.text, "command_item_2") self.assertEqual(command_item_3.text, "command_item_3") self.assertEqual(command_item_1.menu, None) self.assertEqual(command_item_2.menu, self.menu) self.assertEqual(command_item_3.menu, self.menu) self.assertFalse(command_item_1.should_exit) self.assertTrue(command_item_2.should_exit) self.assertFalse(command_item_3.should_exit) self.assertEqual(command_item_1.command, "exit") self.assertEqual(command_item_2.command, "ls") self.assertEqual(command_item_3.command, "rm") self.assertEqual(command_item_1.arguments, []) self.assertEqual(command_item_2.arguments, ["-l", "-a", "~"]) self.assertEqual(command_item_3.arguments, ["-r", "-f", "./test"])
def menu(): """ Make use of an interactive menu. """ interactive_menu = ConsoleMenu("Welcome to qr-rocket menu", "Select an option") interactive_menu.append_item( CommandItem("Create a new PDF file", "rocketqr create")) interactive_menu.append_item( CommandItem("Delete all auto generated files", "rocketqr delete")) interactive_menu.append_item( CommandItem("Go to templates menu", "rocketqr templates menu")) interactive_menu.show()
def test_return(self): if platform.system().lower() == "windows": return_command_item = CommandItem("return_command_item", "exit 1") else: return_command_item = CommandItem("return_command_item", "exit 1") return_command_item.action() self.assertEqual(return_command_item.get_return(), 1)
def main(): """ Run the intial setup script, check for an input file and show the main menu """ init_script() read_info_file() # Define Menu menu = ConsoleMenu( title="AWS EC2 and S3 Webserver Configuration Script", subtitle="Developer Operations Assignment 1 - Dylan Gore", ) # Define menu options and what they do opt_full_run = FunctionItem("Run automatic configuration", wait_func, [full_run]) opt_user_input = FunctionItem("Input custom settings", wait_func, [accept_user_input]) opt_active = FunctionItem("List running instances/active buckets", wait_func, [list_active]) opt_monitoring = FunctionItem("Display monitoring info", wait_func, [display_monitor]) # opt_website = FunctionItem("Check if web server is running", wait_func, [check_website]) # opt_iam = FunctionItem("Create IAM role & policy", wait_func, [check_for_iam_role, iam, sts_client]) opt_configure_aws = CommandItem("Configure AWS credentials and Settigns", "aws configure") opt_load_config_file = FunctionItem("Load configuration file", wait_func, [read_info_file]) opt_cleanup = FunctionItem("Cleanup AWS", wait_func, [cleanup]) opt_clear_logs = FunctionItem("Clear logs", wait_func, [clear_logs]) # Add options to menu menu.append_item(opt_full_run) menu.append_item(opt_user_input) menu.append_item(opt_active) menu.append_item(opt_monitoring) # menu.append_item(opt_website) # menu.append_item(opt_iam) menu.append_item(opt_configure_aws) menu.append_item(opt_load_config_file) menu.append_item(opt_cleanup) menu.append_item(opt_clear_logs) # Display the menu and wait for user input menu.show() # Print when user runs the 'Exit' option print("Exiting...")
from consolemenu import ConsoleMenu from consolemenu.items import FunctionItem, SubmenuItem, CommandItem menu = ConsoleMenu(title='Menu Title', subtitle='Subtitle') command_item = CommandItem('Run a console command!', 'touch.txt') function_item = FunctionItem('call a function', input, ['enter input']) submenu = ConsoleMenu('a submenu') submenu_item = SubmenuItem('show a submenu', submenu, menu=menu) menu.append_item(command_item) menu.append_item(function_item) menu.append_item(submenu_item) # menu.start() # menu.join() menu.show()
def templates_menu(): templates_menu = ConsoleMenu("You are in qr-rocket templates menu", "Select an option") templates_menu.append_item(CommandItem("Show templates", "rocketqr templates show")) templates_menu.append_item(CommandItem("Create new template", "rocketqr templates create")) templates_menu.append_item(CommandItem("Go back to main menu", "rocketqr menu")) templates_menu.show()
def test_call(self, mock_class): command_item = CommandItem("command_item", "ls", ["-l", "-a", "~"]) command_item.action() mock_class.run.assert_called_with("ls -l -a ~", shell=True)
def test_run(self): create_item = CommandItem("create_item", 'echo hello>test.txt') if platform.system().lower() == "windows": delete_item = CommandItem("delete_item", "del test.txt") expected_contents = "hello \n" else: delete_item = CommandItem("delete_item", "rm test.txt") expected_contents = "hello\n" create_item.action() self.assertEqual(create_item.get_return(), 0) self.assertTrue(os.path.isfile("test.txt")) with open("test.txt", 'r') as text: self.assertEqual(text.read(), expected_contents) delete_item.action() self.assertEqual(delete_item.get_return(), 0) self.assertFalse(os.path.isfile("test.txt"))