def testToRegexs(self): from Exscript.util.cast import to_regexs self.assertRaises(TypeError, to_regexs, None) result = to_regexs([]) self.assertIsInstance(result, list) self.assertEqual(len(result), 0) result = to_regexs('regex') self.assertIsInstance(result, list) self.assertEqual(len(result), 1) self.assertTrue(hasattr(result[0], 'match')) result = to_regexs(re.compile('regex')) self.assertIsInstance(result, list) self.assertEqual(len(result), 1) self.assertTrue(hasattr(result[0], 'match')) regexs = ['regex1', re.compile('regex2')] result = to_regexs(regexs) self.assertIsInstance(result, list) self.assertEqual(len(result), 2) self.assertTrue(hasattr(result[0], 'match')) self.assertTrue(hasattr(result[1], 'match')) self.assertEqual(result[0].pattern, 'regex1') self.assertEqual(result[1].pattern, 'regex2')
def testToRegexs(self): from Exscript.util.cast import to_regexs self.assertRaises(TypeError, to_regexs, None) result = to_regexs([]) self.assertTrue(isinstance(result, list)) self.assertTrue(len(result) == 0) result = to_regexs('regex') self.assertTrue(isinstance(result, list)) self.assertTrue(len(result) == 1) self.assertTrue(hasattr(result[0], 'match')) result = to_regexs(re.compile('regex')) self.assertTrue(isinstance(result, list)) self.assertTrue(len(result) == 1) self.assertTrue(hasattr(result[0], 'match')) regexs = ['regex1', re.compile('regex2')] result = to_regexs(regexs) self.assertTrue(isinstance(result, list)) self.assertTrue(len(result) == 2) self.assertTrue(hasattr(result[0], 'match')) self.assertTrue(hasattr(result[1], 'match')) self.assertEqual(result[0].pattern, 'regex1') self.assertEqual(result[1].pattern, 'regex2')
def set_password_prompt(self, regex = None): """ Defines a pattern that is used to monitor the response of the connected host for a password prompt. @type regex: RegEx @param regex: The pattern that, when matched, causes an error. """ if regex is None: self.manual_password_re = regex else: self.manual_password_re = to_regexs(regex)
def set_password_prompt(self, regex=None): """ Defines a pattern that is used to monitor the response of the connected host for a password prompt. :type regex: RegEx :param regex: The pattern that, when matched, causes an error. """ if regex is None: self.manual_password_re = regex else: self.manual_password_re = to_regexs(regex)
def set_login_error_prompt(self, error = None): """ Defines a pattern that is used to monitor the response of the connected host during the authentication procedure. If the pattern matches an error is raised. @type error: RegEx @param error: The pattern that, when matched, causes an error. """ if error is None: self.manual_login_error_re = error else: self.manual_login_error_re = to_regexs(error)
def set_error_prompt(self, error = None): """ Defines a pattern that is used to monitor the response of the connected host. If the pattern matches (any time the expect() or expect_prompt() methods are used), an error is raised. @type error: RegEx @param error: The pattern that, when matched, causes an error. """ if error is None: self.manual_error_re = error else: self.manual_error_re = to_regexs(error)
def set_login_error_prompt(self, error=None): """ Defines a pattern that is used to monitor the response of the connected host during the authentication procedure. If the pattern matches an error is raised. :type error: RegEx :param error: The pattern that, when matched, causes an error. """ if error is None: self.manual_login_error_re = error else: self.manual_login_error_re = to_regexs(error)
def set_error_prompt(self, error=None): """ Defines a pattern that is used to monitor the response of the connected host. If the pattern matches (any time the expect() or expect_prompt() methods are used), an error is raised. :type error: RegEx :param error: The pattern that, when matched, causes an error. """ if error is None: self.manual_error_re = error else: self.manual_error_re = to_regexs(error)
def add_monitor(self, pattern, callback): """ Calls the given function whenever the given pattern matches the buffer. Arguments passed to the callback are the index of the match, and the match object of the regular expression. @type pattern: str|re.RegexObject|list(str|re.RegexObject) @param pattern: One or more regular expressions. @type callback: callable @param callback: The function that is called. """ self.monitors.append([to_regexs(pattern), callback, 0])
def set_prompt(self, prompt = None): """ Defines a pattern that is waited for when calling the expect_prompt() method. If the set_prompt() method is not called, or if it is called with the prompt argument set to None, a default prompt is used that should work with many devices running Unix, IOS, IOS-XR, or Junos and others. @type prompt: RegEx @param prompt: The pattern that matches the prompt of the remote host. """ if prompt is None: self.manual_prompt_re = prompt else: self.manual_prompt_re = to_regexs(prompt)
def set_prompt(self, prompt=None): """ Defines a pattern that is waited for when calling the expect_prompt() method. If the set_prompt() method is not called, or if it is called with the prompt argument set to None, a default prompt is used that should work with many devices running Unix, IOS, IOS-XR, or Junos and others. :type prompt: RegEx :param prompt: The pattern that matches the prompt of the remote host. """ if prompt is None: self.manual_prompt_re = prompt else: self.manual_prompt_re = to_regexs(prompt)
def add_monitor(self, pattern, callback, limit=80): """ Calls the given function whenever the given pattern matches the buffer. Arguments passed to the callback are the index of the match, and the match object of the regular expression. @type pattern: str|re.RegexObject|list(str|re.RegexObject) @param pattern: One or more regular expressions. @type callback: callable @param callback: The function that is called. @type limit: int @param limit: The maximum size of the tail of the buffer that is searched, in number of bytes. """ self.monitors.append([to_regexs(pattern), callback, 0, limit])
def add_monitor(self, pattern, callback, limit = 80): """ Calls the given function whenever the given pattern matches the buffer. Arguments passed to the callback are the index of the match, and the match object of the regular expression. @type pattern: str|re.RegexObject|list(str|re.RegexObject) @param pattern: One or more regular expressions. @type callback: callable @param callback: The function that is called. @type limit: int @param limit: The maximum size of the tail of the buffer that is searched, in number of bytes. """ self.monitors.append([to_regexs(pattern), callback, 0, limit])
def _expect(self, prompt): result = self._domatch(to_regexs(prompt), True) return result
def _waitfor(self, prompt): re_list = to_regexs(prompt) patterns = [p.pattern for p in re_list] self._dbg(2, 'waiting for: ' + repr(patterns)) result = self._domatch(re_list, False) return result