我们从Python开源项目中,提取了以下36个代码示例,用于说明如何使用curses.ungetch()。
def test_main_menu_config_global_module_removal(self, write_config_mock): """Test removal of a module named Global.""" original_length = len(self.ec2rl._modules) global_mod = ec2rlcore.module.get_module("test/modules/bad_mod.d/global.yaml") self.ec2rl._modules.append(global_mod) self.assertNotEqual(len(self.ec2rl._modules), original_length) curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) with contextlib.redirect_stdout(self.output): self.assertTrue(self.ec2rl.menu_config()) self.assertEqual(len(self.output.getvalue()), 129) self.assertTrue(re.match(r"^\n----------\[Configuration File\]----------\n\nConfiguration file saved:\n" r"/var/tmp/ec2rl/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}_[0-9]{2}_[0-9]{2}.[0-9]{6}" r"/configuration.cfg\n$", self.output.getvalue())) self.assertEqual(len(self.ec2rl._modules), original_length) self.assertTrue("Global" not in self.ec2rl._modules) self.assertTrue(write_config_mock.called)
def test_menu_config_global_arg_selection(self): """Test setting the Global arg 'only-domains' to 'performance'""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_DOWN) curses.ungetch(" ") curses.ungetch(curses.KEY_DOWN) curses.ungetch(" ") curses.ungetch(curses.KEY_DOWN) curses.ungetch(" ") # This key sequence may need to change if the Global args in the menu are modified. curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) curses.ungetch("\n") self.menu() self.assertFalse(self.menu["Configure global module arguments"]["only-domains"]["application"].toggled) self.assertFalse(self.menu["Configure global module arguments"]["only-domains"]["net"].toggled) self.assertFalse(self.menu["Configure global module arguments"]["only-domains"]["os"].toggled) self.assertTrue(self.menu["Configure global module arguments"]["only-domains"]["performance"].toggled)
def test_menu_config_global_arg_clear_int_index(self): """ Test setting that the "Clear" footer option sets the Concurrency value to an empty string. Verify using an integer index (e.g. list access). """ curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_LEFT) curses.ungetch(curses.KEY_LEFT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) # This key sequence may need to change if the Global args in the menu are modified. curses.ungetch("\n") curses.ungetch("1") curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch("\n") self.menu() self.assertEqual(self.menu["Configure global module arguments"][3].get_value(), "")
def test_menu_config_menu_scrolling_up(self): """Test paging down to the bottom of the Modules sub-menu thens scrolling up to the top.""" curses.initscr() # Exit the menu curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # Test scrolling up to the top for i in range(len(self.modules)): curses.ungetch(curses.KEY_UP) # Go to the bottom of the Modules sub-menu for iteration in range(15): curses.ungetch(curses.KEY_NPAGE) # Get into the Modules submenu curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) self.assertTrue(self.menu())
def test_menu_config_menu_scrolling_down(self): """Test scrolling down through the Modules sub-menu.""" curses.initscr() # Exit the menu curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # Test scrolling down, line by line for i in range(len(self.modules)): curses.ungetch(curses.KEY_DOWN) # Get into the Modules submenu curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) self.assertTrue(self.menu())
def test_menu_config_menu_scrolling_down_single_page(self): """Test scrolling down through the Global sub-menu.""" curses.initscr() # Exit the menu curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # Test scrolling down, line by line, and one past the bottom for i in range(len(self.menu["Configure global module arguments"])): curses.ungetch(curses.KEY_DOWN) # Get into the Global submenu curses.ungetch("\n") self.assertTrue(self.menu())
def test_menu_config_toggle_all_off(self): """Test that "N" deselects all items in the only-classes menu.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # Deselect all items curses.ungetch("N") curses.ungetch("\n") curses.ungetch("\n") self.assertTrue(self.menu()) for item in self.menu["Configure global module arguments"]["only-classes"]: self.assertFalse(item.toggled)
def test_menu_config_toggle_all_on_mixed_items(self): """Test that "N" deselects then selects "perfimpact" in the Global args menu.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # Select all items (turns perfimpact on) curses.ungetch("N") # Deselect all items (perfimpact is already off) curses.ungetch("N") # Toggle perfimpact off curses.ungetch(" ") curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch("\n") self.assertTrue(self.menu()) self.assertTrue(self.menu["Configure global module arguments"]["perfimpact"].toggled)
def test_menu_config_view_module_help(self): """ Test drawing the menu, selecting the modules menu, a module, and the module help, and then exiting the menu. """ curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") # _draw_notifications only reacts to ord("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) self.assertTrue(self.menu())
def test_menu_config_view_long_module_help(self): """ Test drawing the menu, selecting the modules menu, a module, and the module help, and then exiting the menu. In this test the help string is more (separated) lines than the window and should be truncated by the _draw_notification method. """ # TODO working here num_messages = 0 while num_messages < 100: self.menu["View all modules"]["aptlog"].helptext += "a\n" num_messages += 1 curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) self.assertTrue(self.menu())
def test_menu_config_set_module_value(self): """Test drawing module option menu, configuring the period module option, and then exiting the menu.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch("1") curses.ungetch("\n") curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) self.assertTrue(self.menu()) self.assertEqual(self.menu["View all modules"]["atop"]["period"].get_value(), "1")
def WinProcKeySess(self): kp = 0 while (kp != 27) & (kp != 9) & (kp!=45) & (kp!=84) & (kp!=122): # while not press: esc,tab,minus,T,z kp = self.WinCgetch() if kp == 101: # e key self.DBsessionOrder = 1 if kp == 108 : # l key self.DBsessionOrder = 2 if kp == 120: # x key self.DBsessionOrder = 3 if kp == 114: # r key self.DBsessionOrder = 4 if kp == 32: # space key if not self.WinTxtSessSpool(): self.WinPrintError('Error txt spooling.') if kp == 104: # h key pass self.WinShowSubMenu() if kp == 27: self.PRG_EXIT = 1 curses.ungetch(kp) # push key to main loop return(kp)
def switch_import_file(self, stdscr = None): if stdscr is None and len(self.stdscrs) > 0: stdscr = self.stdscrs[-1] ret = self._switch(stdscr, Spreader, logic="importer") if len(self.stdscrs) > 0: # curses.ungetch(ord("L") - ord("@")) curses.ungetch("Q") return ret
def test_main_menu_config_save_config_failed(self, mock_side_effect_function): """Test handling of a failed save_config() call.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) self.assertFalse(self.ec2rl.menu_config()) self.assertTrue(mock_side_effect_function.called)
def test_menu_config_select_modules_no_module_args(self): """Test drawing the menus when selecting a module that contains no configuration args.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch("\n") curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) self.assertTrue(self.menu())
def test_menu_config_select_module_exit_selection(self): """Test drawing and exiting the menu.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_LEFT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_LEFT) curses.ungetch(curses.KEY_UP) curses.ungetch(curses.KEY_DOWN) self.assertTrue(self.menu())
def test_menu_config_select_module_exit_selection_no_colors(self, mock_side_effect_function): """Test drawing and exiting the menu when the terminal doesn't support colors.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) self.assertTrue(self.menu()) self.assertTrue(mock_side_effect_function.called)
def test_menu_config_key_resize(self): """Test redrawing the window in response to a resize keypress.""" curses.initscr() curses.ungetch("\n") curses.ungetch("\n") curses.ungetch(curses.KEY_RESIZE) curses.ungetch(curses.KEY_RIGHT) self.assertTrue(self.menu())
def test_menu_config_key_right_gt_footer_len(self): """Test behavior when attempting to select an item to the right of the rightmost footer items.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_LEFT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) self.assertTrue(self.menu())
def test_menu_config_space_non_toggleitem(self): """Test behavior when attempting toggle a non-ToggleItem with the spacebar.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch(" ") self.assertTrue(self.menu())
def test_menu_config_toggle_all_off_mixed_items(self): """Test that "N" deselects "perfimpact" in the Global args menu.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # Deselect all items curses.ungetch("N") curses.ungetch("\n") self.assertTrue(self.menu()) self.assertFalse(self.menu["Configure global module arguments"]["perfimpact"].toggled)
def test_menu_config_clear_non_textentryitem(self): """Test behavior when attempting clear a non-TextEntryItem.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_LEFT) curses.ungetch(curses.KEY_LEFT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") self.assertTrue(self.menu())
def test_menu_config_set_and_clear_module_value(self): """Test drawing module option menu, not configuring the period module option, and then exiting the menu.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch(curses.KEY_LEFT) curses.ungetch(curses.KEY_LEFT) curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch(curses.KEY_RIGHT) curses.ungetch("\n") curses.ungetch("1") curses.ungetch("\n") curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) self.assertTrue(self.menu()) self.assertEqual(self.menu["View all modules"]["atop"]["period"].get_value(), "")
def test_menu_unsupportedfooteroption(self): """Test that unsupported footer strings raise MenuUnsupportedFooterOptionError when encountered.""" self.menu.footer_items = ["a"] curses.ungetch("\n") with self.assertRaises(ec2rlcore.menu.MenuUnsupportedFooterOptionError) as error: self.assertTrue(self.menu()) self.assertEqual(str(error.exception), "Encountered unsupported footer selection: a")
def test_menu_item_textentryitem(self): """Test that TextEntryItem obtains user input when called and that get_value() returns the stored value.""" curses.initscr() curses.ungetch("\n") curses.ungetch("a") self.textentryitem() self.assertEqual(self.textentryitem.get_value(), "a")
def test_menu_textentryitem__deletion(self): """Test that the input_caller returns the expected string value.""" curses.initscr() curses.ungetch("\n") curses.ungetch("a") curses.ungetch(curses.KEY_BACKSPACE) curses.ungetch(curses.KEY_BACKSPACE) curses.ungetch(curses.KEY_BACKSPACE) curses.ungetch("a") curses.ungetch("a") self.textentryitem() self.assertEqual(self.textentryitem.get_value(), "a")
def test_menu_textentryitem__overwrite_insert(self): """Test that the input_caller returns the expected string value.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_IC) curses.ungetch("b") curses.ungetch(curses.KEY_LEFT) curses.ungetch("a") curses.ungetch(curses.KEY_IC) self.textentryitem() self.assertEqual(self.textentryitem.get_value(), "b")
def test_menu_textentryitem__key_right(self): """Test that the input_caller returns the expected string value.""" curses.initscr() curses.ungetch("\n") curses.ungetch("b") curses.ungetch(curses.KEY_RIGHT) curses.ungetch("a") self.textentryitem() self.assertEqual(self.textentryitem.get_value(), "a b")
def test_menu_textentryitem__ascii_bel(self): """Test that the input_caller returns the expected string value.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.ascii.BEL) # BEL is a non-printable character so the return value should be an empty string self.textentryitem() self.assertEqual(self.textentryitem.get_value(), "")
def test_menu_textentryitem__key_up(self): """Test that the input_caller returns the expected string value.""" curses.initscr() curses.ungetch("\n") curses.ungetch(curses.KEY_UP) curses.ungetch(curses.KEY_DOWN) curses.ungetch("a") self.textentryitem() self.assertEqual(self.textentryitem.get_value(), "a")
def hackCursesFixes(): if sys.platform == 'darwin': def windowChangedHandler(signum, frame): curses.ungetch(curses.KEY_RESIZE) signal.signal(signal.SIGWINCH, windowChangedHandler) def wakeGetch(signum, frame): curses.ungetch(0) signal.signal(signal.SIGUSR1, wakeGetch)
def changeToFindPrior(self): curses.ungetch(self.savedCh) self.host.changeFocusTo(self.host.interactiveFind)
def saveEventChangeToHostWindow(self, *args): curses.ungetch(self.savedCh) self.host.changeFocusTo(self.host)
def test_main_menu_config(self, open_mock): """Test that the menu configurator functions.""" self.ec2rl.options.global_args["abc"] = "def" curses.initscr() # main menu -> exit curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # Modules menu -> main menu curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # atop -> Modules menu curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # atop -> period = 1 curses.ungetch("\n") curses.ungetch("1") curses.ungetch("\n") # Modules menu -> atop curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) # main menu -> Modules menu curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) # Global menu -> main menu curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # Global -> concurrency -> 2 curses.ungetch("\n") curses.ungetch("2") curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) # Global menu -> only-classes menu curses.ungetch("\n") curses.ungetch(curses.KEY_RIGHT) # unselect "collect" curses.ungetch(" ") curses.ungetch("\n") # main menu -> Global menu curses.ungetch("\n") with contextlib.redirect_stdout(self.output): self.assertTrue(self.ec2rl.menu_config()) self.assertTrue(re.match(r"^\n----------\[Configuration File\]----------\n\nConfiguration file saved:\n" r"/var/tmp/ec2rl/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}_[0-9]{2}_[0-9]{2}.[0-9]{6}" r"/configuration.cfg\n$", self.output.getvalue())) self.assertEqual(len(self.output.getvalue()), 129) self.assertTrue(open_mock.called) self.assertEqual(self.ec2rl.options.global_args["abc"], "def") self.assertEqual(self.ec2rl.options.global_args["concurrency"], "2") self.assertEqual(self.ec2rl.options.global_args["onlyclasses"], "diagnose,gather") self.assertEqual(self.ec2rl.options.per_module_args["atop"]["period"], "1")
def test_main_menu_config_run(self, prediag_mock, main_log_handler_mock, debug_log_handler_mock, mkdir_mock, copyfile_mock, chdir_mock, write_config_mock, logging_fh_mock): """Test running ec2rl from the menu.""" # Setup the instance of Main responses.add(responses.GET, "http://169.254.169.254/latest/meta-data/instance-id", body="i-deadbeef", status=200) path_to_ec2rl = os.path.abspath("ec2rl") test_path = os.path.sep.join([os.path.split(path_to_ec2rl)[0], "test", "modules", "ec2rl"]) sys.argv = [test_path, "menu-config"] module_path = os.path.join(self.callpath, "test/modules/single_diagnose/") ec2rl_run_test = ec2rlcore.main.Main(debug=True, full_init=True) ec2rl_run_test._prediags = ec2rlcore.moduledir.ModuleDir(module_path) ec2rl_run_test._modules = ec2rlcore.moduledir.ModuleDir(module_path) ec2rl_run_test._postdiags = ec2rlcore.moduledir.ModuleDir(module_path) # We don't need to run pre/post modules for this test ec2rl_run_test._prediags = [] ec2rl_run_test._postdiags = [] # Setup the input buffer for curses to grab from curses.initscr() # main menu -> run curses.ungetch("\n") curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) curses.ungetch(curses.KEY_DOWN) with contextlib.redirect_stdout(self.output): ec2rl_run_test() self.assertTrue(self.output.getvalue().startswith("\n-----------[Backup Creation]-----------\n\nNo backup op")) self.assertTrue(self.output.getvalue().endswith( "/SV_3KrcrMZ2quIDzjn?InstanceID=i-deadbeef&Version={}\n\n".format(ec2rlcore.main.Main.PROGRAM_VERSION))) self.assertTrue("-\n\nRunning Modules:\nxennetrocket\n\n-" in self.output.getvalue()) self.assertEqual(len(self.output.getvalue()), 1635) self.assertTrue(prediag_mock.called) self.assertTrue(main_log_handler_mock.called) self.assertTrue(debug_log_handler_mock.called) self.assertTrue(mkdir_mock.called) self.assertTrue(copyfile_mock.called) self.assertTrue(chdir_mock.called) self.assertTrue(write_config_mock.called) self.assertTrue(logging_fh_mock.called) # The resulting subcommand should be "run" self.assertEqual(ec2rl_run_test.subcommand, "run")
def WinCgetch(self): kp = -1 while kp==-1: kp = self.VWscreen.getch() ScrResized = curses.is_term_resized(self.scrsize[0],self.scrsize[1]) if ScrResized: try: y,x = self.VWscreen.getmaxyx() minY = self.ScreenMinY minX = self.ScreenMinX bestY = self.ScreenFullY bestX = self.ScreenFullX if (y<minY) | (x<minX): # check resized size self.VWscreen.clear() self.VWscreen.refresh() curses.resizeterm(self.scrsize[0],self.scrsize[1]) # reset to original size self.VWscreen.clear() self.VWscreen.refresh() curses.flash() self.WinPrintError("Screen size not valid. ") time.sleep(1) self.PRG_EXIT_SCR_SIZE = 1 curses.ungetch(27) # push ESC key to main loop: exit from program else: # set new size if (y>= bestY) and (x>=bestX): self.ScreenFullSize = 1 else: self.ScreenFullSize = 0 self.VWscreen.clear() curses.resizeterm(y, x) self.scrsize = [] self.scrsize = (y,x) self.WinRefreshScr() except Exception as err: self.WinPrintError("Error resizing screen... "+str(err)+ " " ) return(kp)