本文整理汇总了Python中sre_parse.parse_template函数的典型用法代码示例。如果您正苦于以下问题:Python parse_template函数的具体用法?Python parse_template怎么用?Python parse_template使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_template函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_template
def parse_template(self, pattern):
"""Parse template."""
i = ReplaceTokens(self._original, use_format=self.use_format)
iter(i)
self.result = [self._empty]
for t in i:
if len(t) > 1:
if self.use_format and t[0:1] == self._lc_bracket:
self.handle_format_group(t[1:-1].strip())
else:
c = t[1:]
first = c[0:1]
if first.isdigit() and (self.use_format or len(c) == 3):
value = int(c, 8)
if value > 0xFF:
if self.binary:
# Re fails on octal greater than 0o377 or 0xFF
raise ValueError("octal escape value outside of range 0-0o377!")
self.result.append(compat.uchr(value))
else:
self.result.append(self.string_convert('\\%03o' % value))
elif not self.use_format and (c[0:1].isdigit() or c[0:1] == self._group):
self.handle_group(t)
elif c == self._lc:
self.single_case(i, _LOWER)
elif c == self._lc_span:
self.span_case(i, _LOWER)
elif c == self._uc:
self.single_case(i, _UPPER)
elif c == self._uc_span:
self.span_case(i, _UPPER)
elif c == self._end:
# This is here just as a reminder that \E is ignored
pass
elif (
not self.binary and
(first == self._unicode_narrow or (not NARROW and first == self._unicode_wide))
):
value = int(t[2:], 16)
if value <= 0xFF:
self.result.append('\\%03o' % value)
else:
self.result.append(compat.uchr(value))
elif first == self._hex:
self.result.append('\\%03o' % int(t[2:], 16))
else:
self.result.append(t)
else:
self.result.append(t)
if len(self.result) > 1:
self.literal_slots.append(self._empty.join(self.result))
del self.result[:]
self.result.append(self._empty)
self.slot += 1
self._template = self._empty.join(self.literal_slots)
self.groups, self.literals = sre_parse.parse_template(self._template, pattern)
示例2: __init__
def __init__(self, pattern, template):
"""Initialize."""
self.__original = template
self.__back_ref = set()
self.__add_back_references(CAP_TOKEN)
self.__template = self.__escape_template(template)
self.groups, self.literals = sre_parse.parse_template(self.__template, pattern)
示例3: _compile_repl
def _compile_repl(*key):
p = _cache_repl.get(key)
if (p is not None):
return p
(repl, pattern,) = key
try:
p = sre_parse.parse_template(repl, pattern)
except error, v:
raise error, v
示例4: _compile_repl
def _compile_repl(*key):
# internal: compile replacement pattern
p = _cache_repl.get(key)
if p is not None:
return p
repl, pattern = key
try:
p = sre_parse.parse_template(repl, pattern)
except error, v:
raise error, v # invalid expression
示例5: _compile_repl
def _compile_repl(repl, pattern):
try:
return _cache_repl[(repl, pattern)]
except KeyError:
pass
p = sre_parse.parse_template(repl, pattern)
if len(_cache_repl) >= _MAXCACHE:
_cache_repl.clear()
_cache_repl[(repl, pattern)] = p
return p
示例6: _compile_repl
def _compile_repl(repl, pattern):
# internal: compile replacement pattern
try:
return _cache_repl[repl, pattern]
except KeyError:
pass
p = sre_parse.parse_template(repl, pattern)
if len(_cache_repl) >= _MAXCACHE:
_cache_repl.clear()
_cache_repl[repl, pattern] = p
return p
示例7: _compile_repl
def _compile_repl(*key):
# internal: compile replacement pattern
p = _cache_repl.get(key)
if p is not None:
return p
repl, pattern = key
p = sre_parse.parse_template(repl, pattern)
if len(_cache_repl) >= _MAXCACHE:
_cache_repl.clear()
_cache_repl[key] = p
return p
示例8: _compile_repl
def _compile_repl(*key):
p = _cache_repl.get(key)
if p is not None:
return p
repl, pattern = key
try:
p = sre_parse.parse_template(repl, pattern)
except error as v:
raise error, v
if len(_cache_repl) >= _MAXCACHE:
_cache_repl.clear()
_cache_repl[key] = p
return p
示例9: _compile_repl
def _compile_repl(repl, pattern):
# internal: compile replacement pattern
try:
#fix me brython
#return _cache_repl[repl, pattern]
return _cache_repl["%s:%s" % (repl, pattern)]
except KeyError:
pass
p = sre_parse.parse_template(repl, pattern)
if len(_cache_repl) >= _MAXCACHE:
_cache_repl.clear()
_cache_repl["%s:%s" % (repl, pattern)] = p
#fix me brython
#_cache_repl[repl, pattern] = p
return p
示例10: _expand
def _expand(self, m, template):
# XXX This code depends on internals of the regular expression
# engine! There's no standard API to do a substitution when you
# have already found the match. One should be added.
# The solution here is designed to be backwards compatible
# with previous Python versions, e.g. 1.5.2.
# XXX This dynamic test should be done only once.
if getattr(re, "engine", "pre") == "pre":
return re.pcre_expand(m, template)
else: # sre
# XXX This import should be avoidable...
import sre_parse
# XXX This parses the template over and over...
ptemplate = sre_parse.parse_template(template, m.re)
return sre_parse.expand_template(ptemplate, m)
示例11: _compiledReplacement
def _compiledReplacement(self, regexField, replField):
repl = getattr(self, replField, None)
if not repl: return BLANK_PARSE_TEMPLATE
cache = self._compiledCache()
regex = getattr(self, regexField, None)
if replField in cache and (regex,repl) in cache[replField]:
return cache[replField][(regex,repl)]
else:
try:
compiled = parse_template(repl, self._compiledRegex(regexField))
cache[replField] = {(regex,repl) : compiled}
return compiled
except:
log.warn("Invalid %s on %s", replField, self)
cache[replField] = {(regex,repl) : None}
return None
示例12: setup_template
def setup_template(self, pattern, template):
if isinstance(template, compat.binary_type):
self.binary = True
tokens = btokens
ctokens = ctok.btokens
else:
self.binary = False
tokens = utokens
ctokens = ctok.utokens
self._original = template
self._back_ref = set()
self._def_back_ref = tokens["def_back_ref"]
self._b_slash = ctokens["b_slash"]
self._empty = ctokens["empty"]
self._add_back_references(ctokens["replace_tokens"])
self._template = self._escape_template(template)
self.groups, self.literals = sre_parse.parse_template(self._template, pattern)
示例13: __init__
def __init__(self, pattern, template):
"""Initialize."""
if isinstance(template, binary_type):
self.binary = True
tokens = btokens
else:
self.binary = False
tokens = utokens
self._original = template
self._back_ref = set()
self._b_slash = tokens[_B_SLASH]
self._def_back_ref = tokens[_DEF_BACK_REF]
self._empty = tokens[_EMPTY]
self._add_back_references(tokens[_REPLACE_TOKENS])
self._template = self.__escape_template(template)
self.groups, self.literals = sre_parse.parse_template(self._template, pattern)
示例14: setInfo
def setInfo(self, **data):
"""
Set attributes on a process.
This method accepts any keyword argument for the property that you wish
to set. The only required property is "uid".
@type uid: string
@keyword uid: Unique identifier of a process
@rtype: DirectResponse
@return: B{Properties}
- data: (dictionary) Object representing a process's new properties
"""
facade = self._getFacade()
processUid = data['uid']
for regexParam in ['includeRegex', 'excludeRegex', 'replaceRegex']:
regex = data.get(regexParam)
if regex:
try:
re.compile(regex)
except re.error as e:
m = "%s : %s" % (regexParam, e)
return DirectResponse.fail(msg=m)
replaceRegex = data.get('replaceRegex')
if replaceRegex:
replaceRegex = re.compile(replaceRegex)
replacement = data.get('replacement')
if replacement:
try:
groups, literals = parse_template(replacement,replaceRegex)
for index, group in groups:
if group > replaceRegex.groups:
m = "Group (%s) referenced in replacement must be defined in replaceRegex" % group
return DirectResponse.fail(msg=m)
except re.error as e:
m = "replacement : %s" % (e,)
return DirectResponse.fail(msg=m)
process = facade.getInfo(processUid)
audit('UI.Process.Edit', processUid, data_=data, skipFields_=('uid'))
return DirectResponse.succeed(data=Zuul.unmarshal(data, process))
示例15: expand_sub
def expand_sub(string, template, debug=0, mode='all') :
""" Given a regular expression and a replacement string, generate expansions of
the regular expression and for each one return it and its transformation
as applied by the replacement string.
string : regular expression to expand
template : transformation to apply to each regular expression
mode : can take 3 values
all : return all possible shortest strings that the regular expression
would match
first : return the first string that all would return
random : return one random string that the regular expression would match
"""
pattern = sre_parse.parse(string, flags=sre_parse.SRE_FLAG_VERBOSE)
pattern.mode = mode
template = sre_parse.parse_template(template, pattern)
if debug :
print pattern
print template
for s in _iterate(pattern, pattern.data, MatchObj(pattern, "")) :
s.patient = 0
yield (s.string, sre_parse.expand_template(template, s))