def __init__(self, hostname, echo=True, login_type=LOGIN_TYPE_BOTH, strict=True, banner=None): """ :type hostname: str :param hostname: The hostname, used for the prompt. :keyword banner: A string to show as soon as the connection is opened. :keyword login_type: integer constant, one of LOGIN_TYPE_PASSWORDONLY, LOGIN_TYPE_USERONLY, LOGIN_TYPE_BOTH, LOGIN_TYPE_NONE. :keyword echo: whether to echo the command in a response. :keyword strict: Whether to raise when a given command has no handler. """ self.hostname = hostname self.banner = banner or 'Welcome to %s!\n' % str(hostname) self.echo = echo self.login_type = login_type self.prompt = hostname + '> ' self.logged_in = False self.commands = CommandSet(strict=strict) self.user_prompt = 'User: '******'Password: ' self.init()
def testAdd(self): cs = CommandSet() self.assertRaises(Exception, cs.eval, 'foo') cs = CommandSet(strict=False) self.assertEqual(cs.eval('foo'), None) cs = CommandSet(strict=True) self.assertRaises(Exception, cs.eval, 'foo') cs.add('foo', 'bar') self.assertEqual(cs.eval('foo'), 'bar') def sayhello(cmd): return 'hello' cs.add('hi', sayhello) self.assertEqual(cs.eval('hi'), 'hello')
def testConstructor(self): CommandSet() CommandSet(strict=True) CommandSet(strict=False)