def test_double_completion(self): """Command must complete with two options.""" cmd1 = Command('configure') cmd2 = Command('terminal') cmd3 = Command('interface') cmd1.addChild(cmd2) cmd1.addChild(cmd3) # State 0 must print all commands followed by help message # and return None as candidates candidates = cmd1.complete('', '', 0, run=False, full_line='configure ') assert None == candidates candidates = cmd1.complete('', 'in', 0, run=False, full_line='configure in') assert 'interface ' == candidates candidates = cmd1.complete('', 't', 0, run=False, full_line='configure t') assert 'terminal ' == candidates
def test_double_overlapping_completion(self): """Command must complete with two overlapping options.""" cmd1 = Command('configure') cmd2 = Command('terminal') cmd2.run = lambda l: "terminal output" cmd3 = Command('terminal_1') cmd3.run = lambda l: "terminal_1 output" cmd1.addChild(cmd2) cmd1.addChild(cmd3) # State 0 must print all commands followed by help message # and return None as candidates candidates = cmd1.complete('', '', 0, run=False, full_line='configure ') assert None == candidates candidates = cmd1.complete('', 't', 0, run=False, full_line='configure t') assert 'terminal ' == candidates candidates = cmd1.complete('', 't', 1, run=False, full_line='configure t') assert 'terminal_1 ' == candidates # user pressing tab on ambiguous command candidates = cmd1.complete(["terminal"], 'terminal', 0, run=False, full_line=None) assert "terminal " == candidates candidates = cmd1.complete(["terminal"], 'terminal', 1, run=False, full_line=None) assert "terminal_1 " == candidates output = cmd1.complete(["terminal"], 'configure terminal', 0, run=True, full_line='configure terminal') assert 'terminal output' == output output = cmd1.complete(["terminal_1"], 'configure terminal_1', 0, run=True, full_line='configure terminal_1') assert 'terminal_1 output' == output
def run(self, line): interface = line.split()[-1] self.interface = interface self.prompt = "RouterA(config-if-%s)" % self.interface self.prompt_delim = "#" ip = Command("ip", help="Configure ip parameters: address") address = Command("address") address.run = self.set_address self.addChild(ip).addChild(address) self.loop()
def main(): console = Console("root@dev:~", '#') ls = LS("ls") bash = Bash("bash") top = Top("top") console.addChild(ls) console.addChild(bash) console.addChild(top) console.addChild(Command('show')).addChild(Command('disk')).addChild( DiskIO('io')) console.loop()
def run(self, line): self.prompt = "RouterA(config)" self.prompt_delim = "#" ip = Command("ip", help="Set ip configurations") route = Command("route") add = RouteAdd("add") interface = InterfaceConsole("interface", dynamic_args=True, help="Configure interface parameters") self.addChild(interface) self.addChild(ip).addChild(route).addChild(add) self.loop()
def test_completion_with_buffer(self): """Command must complete correctly with buffer provided.""" cmd1 = Command('configure') cmd2 = Command('terminal') cmd1.addChild(cmd2) candidates = cmd1.complete(['t'], 't', 0, run=False, full_line='configure ') assert 'terminal ' == candidates candidates = cmd1.complete(['t'], 't', 1, run=False, full_line='configure ') assert None == candidates
def test_simple_completion(self): """Command must complete with only one option.""" cmd1 = Command('configure') cmd2 = Command('terminal') cmd1.addChild(cmd2) candidates = cmd1.complete('', '', 0, run=False, full_line='configure ') assert 'terminal ' == candidates candidates = cmd1.complete('', '', 1, run=False, full_line='configure ') assert None == candidates
def run(self, line): self.prompt = "RouterA" self.prompt_delim = "#" password = getpass.getpass() if password != 'python': _print("%% Invalid Password, access denied") return configure = Command("configure", help="Enter configure mode") terminal = ConfigureTerminal("terminal") configure.addChild(terminal) show = Show('show', help="Show configurations", dynamic_args=True) self.addChild(configure) self.addChild(show) self.loop()
def test_double_overlapping_nested_completion(self): """Command must complete with two overlapping nested options.""" cmd1 = Command('configure') cmd2 = Command('terminal') cmd3 = Command('terminal_1') cmd1.addChild(cmd2) cmd1.addChild(cmd3) cmd4 = Command("option") cmd2.addChild(cmd4) cmd3.addChild(cmd4) cmd5 = Command("Aaaa") cmd4.addChild(cmd5) cmd6 = Command("B") cmd4.addChild(cmd6) # show help for A, B (NOT terminal terminal_1) tab complete with io.StringIO() as buf, redirect_stdout(buf): output = cmd1.complete(["terminal", "option"], '', 0, run=False, full_line=None) assert output is None help_output = buf.getvalue() assert help_output == '\rPossible Completions:\n\r Aaaa No help provided\n\r B No help provided\n' # show help for A, B (NOT terminal terminal_1) enter with io.StringIO() as buf, redirect_stdout(buf): output = cmd1.complete(["terminal", "option"], 'terminal option', 0, run=True, full_line='terminal option') assert output is None help_output = buf.getvalue() assert help_output == '\nIncomplete Command: terminal option\n\nHelp:\nAaaa - No help provided\n B - No help provided\n\n'
def main(): console = Console(prompt="firewall", prompt_delim=">") show = Command("show", help="Show command helper") setup = Command("setup", help="Setup options") console.loop()
def test_command_creation(self): """Command must be created with name and default help message.""" cmd = Command('configure') assert cmd.name == 'configure' assert cmd.help == 'No help provided' assert cmd.dynamic_args == False
def test_completion_with_dynamic_arg(self): cmd1 = Command('show') cmd2 = Command('call', dynamic_args=True) cmd3 = Command('calls', dynamic_args=True) cmd2.args = lambda: ['100', '101'] cmd3.args = lambda: ['continuous', 'raw'] cmd1.addChild(cmd2) cmd1.addChild(cmd3) candidates = cmd1.complete(['c'], '', 0, run=False, full_line='show calls') self.assertEqual(None, candidates) candidates = cmd1.complete(['c'], 'c', 0, run=False, full_line='show calls') self.assertEqual('call ', candidates) candidates = cmd1.complete(['c'], 'c', 1, run=False, full_line='show calls') self.assertEqual('calls ', candidates) candidates = cmd2.complete([''], '', 0, run=False, full_line='show calls') self.assertEqual(None, candidates) candidates = cmd2.complete([''], '1', 0, run=False, full_line='show calls') self.assertEqual('100', candidates) candidates = cmd2.complete([''], '1', 1, run=False, full_line='show calls') self.assertEqual('101', candidates) candidates = cmd3.complete([''], '', 0, run=False, full_line='show calls c') self.assertEqual(None, candidates) candidates = cmd3.complete([''], 'c', 0, run=False, full_line='show calls c') self.assertEqual('continuous', candidates) candidates = cmd3.complete([''], 'r', 0, run=False, full_line='show calls c') self.assertEqual('raw', candidates) candidates = cmd1.complete(['calls', 'c'], 'c', 0, run=False, full_line='show calls c') self.assertEqual('continuous', candidates) candidates = cmd2.complete(['1'], '1', 0, run=False, full_line='show calls c') self.assertEqual('100', candidates) candidates = cmd2.complete(['1'], '1', 1, run=False, full_line='show calls c') self.assertEqual('101', candidates)