def fn_replace(context, txt, regexp, repl) :
     txt = asstr(txt)
     repl = asstr(repl)
     try :
         res = re.sub(regexp, repl, txt)
     except Exception as e :
         raise et.XPathEvalError(e.message + ": txt = {}, regexp = {}, repl = {}".format(txt, regexp, repl))
     return res
Exemplo n.º 2
0
 def test_operator_define_operator_error_3(self, mock_error, mock_all_same):
     mock_all_same.side_effect = etree.XPathEvalError('Xpath Mock Error')
     op = Operator()
     op.no_failed = 0
     op.define_operator(self.hostname, 'all-same', 'mock_xpath', ['ele1'], 'mock_err', 'mock_info', 'mock command',
                        True, ['mock_id'], 'test_name')
     self.assertEqual(op.no_failed, 1)       #xpathError
     mock_error.assert_called_with('\x1b[31mError in evaluating XPATH, \nComplete Message: Xpath Mock Error', extra='1.1.1.1')
Exemplo n.º 3
0
 def xpath(self, path, context, base):
     # print path, self.vars
     extensions = {
         (None, 'doc'): self.fn_doc,
         (None, 'firstword'): self.fn_firstword,
         (None, 'findsep'): self.fn_findsep,
         (None, 'replace'): self.fn_replace,
         (None, 'dateformat'): self.fn_dateformat,
         (None, 'choose'): self.fn_choose,
         (None, 'split'): self.fn_split,
         (None, 'default'): self.fn_default
     }
     try:
         res = context.xpath(path,
                             extensions=extensions,
                             smart_strings=False,
                             **self.vars)
     except Exception as e:
         raise et.XPathEvalError(e.message + ":\n" + path + ", at line " +
                                 str(base.sourceline))
     if not isinstance(res, basestring) and len(res) == 1:
         res = res[0]
     return res