templater.py
924 lines
| 32.9 KiB
| text/x-python
|
PythonLexer
/ mercurial / templater.py
|
r1909 | # templater.py - template expansion for output | ||
# | ||||
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r1909 | |||
|
r37032 | """Slightly complicated template engine for commands and hgweb | ||
This module provides low-level interface to the template engine. See the | ||||
formatter and cmdutil modules if you are looking for high-level functions | ||||
such as ``cmdutil.rendertemplate(ctx, tmpl)``. | ||||
Internal Data Types | ||||
------------------- | ||||
Template keywords and functions take a dictionary of current symbols and | ||||
resources (a "mapping") and return result. Inputs and outputs must be one | ||||
of the following data types: | ||||
bytes | ||||
a byte string, which is generally a human-readable text in local encoding. | ||||
generator | ||||
a lazily-evaluated byte string, which is a possibly nested generator of | ||||
values of any printable types, and will be folded by ``stringify()`` | ||||
or ``flatten()``. | ||||
None | ||||
sometimes represents an empty value, which can be stringified to ''. | ||||
True, False, int, float | ||||
can be stringified as such. | ||||
|
r38228 | wrappedbytes, wrappedvalue | ||
a wrapper for the above printable types. | ||||
|
r37032 | date tuple | ||
a (unixtime, offset) tuple, which produces no meaningful output by itself. | ||||
hybrid | ||||
represents a list/dict of printable values, which can also be converted | ||||
to mappings by % operator. | ||||
mappable | ||||
represents a scalar printable value, also supports % operator. | ||||
|
r37417 | |||
mappinggenerator, mappinglist | ||||
represents mappings (i.e. a list of dicts), which may have default | ||||
output format. | ||||
|
r37517 | |||
mappedgenerator | ||||
a lazily-evaluated list of byte strings, which is e.g. a result of % | ||||
operation. | ||||
|
r37032 | """ | ||
|
r34139 | from __future__ import absolute_import, print_function | ||
|
r25985 | |||
|
r37091 | import abc | ||
|
r25985 | import os | ||
from .i18n import _ | ||||
from . import ( | ||||
config, | ||||
|
r31520 | encoding, | ||
|
r25985 | error, | ||
parser, | ||||
|
r30615 | pycompat, | ||
|
r25985 | templatefilters, | ||
|
r36940 | templatefuncs, | ||
|
r36931 | templateutil, | ||
|
r25985 | util, | ||
) | ||||
|
r37102 | from .utils import ( | ||
stringutil, | ||||
) | ||||
|
r36461 | |||
|
r13176 | # template parsing | ||
elements = { | ||||
|
r25815 | # token-type: binding-strength, primary, prefix, infix, suffix | ||
"(": (20, None, ("group", 1, ")"), ("func", 1, ")"), None), | ||||
|
r34536 | ".": (18, None, None, (".", 18), None), | ||
|
r34330 | "%": (15, None, None, ("%", 15), None), | ||
|
r31884 | "|": (15, None, None, ("|", 15), None), | ||
"*": (5, None, None, ("*", 5), None), | ||||
"/": (5, None, None, ("/", 5), None), | ||||
"+": (4, None, None, ("+", 4), None), | ||||
"-": (4, None, ("negate", 19), ("-", 4), None), | ||||
|
r31885 | "=": (3, None, None, ("keyvalue", 3), None), | ||
|
r31883 | ",": (2, None, None, ("list", 2), None), | ||
")": (0, None, None, None, None), | ||||
|
r25815 | "integer": (0, "integer", None, None, None), | ||
"symbol": (0, "symbol", None, None, None), | ||||
"string": (0, "string", None, None, None), | ||||
"template": (0, "template", None, None, None), | ||||
"end": (0, None, None, None, None), | ||||
|
r13176 | } | ||
|
r28911 | def tokenize(program, start, end, term=None): | ||
"""Parse a template expression into a stream of tokens, which must end | ||||
with term if specified""" | ||||
|
r13176 | pos = start | ||
|
r32154 | program = pycompat.bytestr(program) | ||
|
r13176 | while pos < end: | ||
c = program[pos] | ||||
if c.isspace(): # skip inter-token whitespace | ||||
pass | ||||
|
r34536 | elif c in "(=,).%|+-*/": # handle simple operators | ||
|
r13176 | yield (c, None, pos) | ||
|
r25783 | elif c in '"\'': # handle quoted templates | ||
s = pos + 1 | ||||
data, pos = _parsetemplate(program, s, end, c) | ||||
yield ('template', data, s) | ||||
pos -= 1 | ||||
|
r25784 | elif c == 'r' and program[pos:pos + 2] in ("r'", 'r"'): | ||
# handle quoted strings | ||||
c = program[pos + 1] | ||||
s = pos = pos + 2 | ||||
|
r13176 | while pos < end: # find closing quote | ||
d = program[pos] | ||||
|
r25638 | if d == '\\': # skip over escaped characters | ||
|
r13176 | pos += 2 | ||
continue | ||||
if d == c: | ||||
|
r25785 | yield ('string', program[s:pos], s) | ||
|
r13176 | break | ||
pos += 1 | ||||
else: | ||||
raise error.ParseError(_("unterminated string"), s) | ||||
|
r30115 | elif c.isdigit(): | ||
|
r25002 | s = pos | ||
while pos < end: | ||||
d = program[pos] | ||||
if not d.isdigit(): | ||||
break | ||||
pos += 1 | ||||
yield ('integer', program[s:pos], s) | ||||
pos -= 1 | ||||
|
r36564 | elif (c == '\\' and program[pos:pos + 2] in (br"\'", br'\"') | ||
or c == 'r' and program[pos:pos + 3] in (br"r\'", br'r\"')): | ||||
|
r25676 | # handle escaped quoted strings for compatibility with 2.9.2-3.4, | ||
# where some of nested templates were preprocessed as strings and | ||||
# then compiled. therefore, \"...\" was allowed. (issue4733) | ||||
# | ||||
# processing flow of _evalifliteral() at 5ab28a2e9962: | ||||
# outer template string -> stringify() -> compiletemplate() | ||||
# ------------------------ ------------ ------------------ | ||||
# {f("\\\\ {g(\"\\\"\")}"} \\ {g("\"")} [r'\\', {g("\"")}] | ||||
# ~~~~~~~~ | ||||
# escaped quoted string | ||||
if c == 'r': | ||||
pos += 1 | ||||
|
r25785 | token = 'string' | ||
|
r25676 | else: | ||
|
r25783 | token = 'template' | ||
|
r25676 | quote = program[pos:pos + 2] | ||
s = pos = pos + 2 | ||||
while pos < end: # find closing escaped quote | ||||
if program.startswith('\\\\\\', pos, end): | ||||
pos += 4 # skip over double escaped characters | ||||
continue | ||||
if program.startswith(quote, pos, end): | ||||
|
r26215 | # interpret as if it were a part of an outer string | ||
|
r26231 | data = parser.unescapestr(program[s:pos]) | ||
|
r25783 | if token == 'template': | ||
data = _parsetemplate(data, 0, len(data))[0] | ||||
|
r25676 | yield (token, data, s) | ||
pos += 1 | ||||
break | ||||
pos += 1 | ||||
else: | ||||
raise error.ParseError(_("unterminated string"), s) | ||||
|
r13176 | elif c.isalnum() or c in '_': | ||
s = pos | ||||
pos += 1 | ||||
while pos < end: # find end of symbol | ||||
d = program[pos] | ||||
if not (d.isalnum() or d == "_"): | ||||
break | ||||
pos += 1 | ||||
sym = program[s:pos] | ||||
|
r18893 | yield ('symbol', sym, s) | ||
|
r13176 | pos -= 1 | ||
|
r28911 | elif c == term: | ||
|
r36709 | yield ('end', None, pos) | ||
|
r25782 | return | ||
|
r13176 | else: | ||
raise error.ParseError(_("syntax error"), pos) | ||||
pos += 1 | ||||
|
r28911 | if term: | ||
raise error.ParseError(_("unterminated template expansion"), start) | ||||
yield ('end', None, pos) | ||||
|
r13176 | |||
|
r25783 | def _parsetemplate(tmpl, start, stop, quote=''): | ||
r""" | ||||
|
r34133 | >>> _parsetemplate(b'foo{bar}"baz', 0, 12) | ||
|
r25783 | ([('string', 'foo'), ('symbol', 'bar'), ('string', '"baz')], 12) | ||
|
r34133 | >>> _parsetemplate(b'foo{bar}"baz', 0, 12, quote=b'"') | ||
|
r25783 | ([('string', 'foo'), ('symbol', 'bar')], 9) | ||
|
r34133 | >>> _parsetemplate(b'foo"{bar}', 0, 9, quote=b'"') | ||
|
r25783 | ([('string', 'foo')], 4) | ||
|
r34133 | >>> _parsetemplate(br'foo\"bar"baz', 0, 12, quote=b'"') | ||
|
r25783 | ([('string', 'foo"'), ('string', 'bar')], 9) | ||
|
r34133 | >>> _parsetemplate(br'foo\\"bar', 0, 10, quote=b'"') | ||
|
r25785 | ([('string', 'foo\\')], 6) | ||
|
r25783 | """ | ||
|
r13176 | parsed = [] | ||
|
r36258 | for typ, val, pos in _scantemplate(tmpl, start, stop, quote): | ||
if typ == 'string': | ||||
parsed.append((typ, val)) | ||||
elif typ == 'template': | ||||
parsed.append(val) | ||||
elif typ == 'end': | ||||
return parsed, pos | ||||
else: | ||||
raise error.ProgrammingError('unexpected type: %s' % typ) | ||||
raise error.ProgrammingError('unterminated scanning of template') | ||||
|
r36527 | def scantemplate(tmpl, raw=False): | ||
r"""Scan (type, start, end) positions of outermost elements in template | ||||
If raw=True, a backslash is not taken as an escape character just like | ||||
r'' string in Python. Note that this is different from r'' literal in | ||||
template in that no template fragment can appear in r'', e.g. r'{foo}' | ||||
is a literal '{foo}', but ('{foo}', raw=True) is a template expression | ||||
'foo'. | ||||
|
r36259 | |||
>>> list(scantemplate(b'foo{bar}"baz')) | ||||
[('string', 0, 3), ('template', 3, 8), ('string', 8, 12)] | ||||
>>> list(scantemplate(b'outer{"inner"}outer')) | ||||
[('string', 0, 5), ('template', 5, 14), ('string', 14, 19)] | ||||
>>> list(scantemplate(b'foo\\{escaped}')) | ||||
[('string', 0, 5), ('string', 5, 13)] | ||||
|
r36527 | >>> list(scantemplate(b'foo\\{escaped}', raw=True)) | ||
[('string', 0, 4), ('template', 4, 13)] | ||||
|
r36259 | """ | ||
last = None | ||||
|
r36527 | for typ, val, pos in _scantemplate(tmpl, 0, len(tmpl), raw=raw): | ||
|
r36259 | if last: | ||
yield last + (pos,) | ||||
if typ == 'end': | ||||
return | ||||
else: | ||||
last = (typ, pos) | ||||
raise error.ProgrammingError('unterminated scanning of template') | ||||
|
r36527 | def _scantemplate(tmpl, start, stop, quote='', raw=False): | ||
|
r36258 | """Parse template string into chunks of strings and template expressions""" | ||
|
r25783 | sepchars = '{' + quote | ||
|
r36527 | unescape = [parser.unescapestr, pycompat.identity][raw] | ||
|
r25781 | pos = start | ||
|
r25654 | p = parser.parser(elements) | ||
|
r36687 | try: | ||
while pos < stop: | ||||
|
r38096 | n = min((tmpl.find(c, pos, stop) | ||
for c in pycompat.bytestr(sepchars)), | ||||
|
r36687 | key=lambda n: (n < 0, n)) | ||
if n < 0: | ||||
yield ('string', unescape(tmpl[pos:stop]), pos) | ||||
pos = stop | ||||
break | ||||
c = tmpl[n:n + 1] | ||||
bs = 0 # count leading backslashes | ||||
if not raw: | ||||
bs = (n - pos) - len(tmpl[pos:n].rstrip('\\')) | ||||
if bs % 2 == 1: | ||||
# escaped (e.g. '\{', '\\\{', but not '\\{') | ||||
yield ('string', unescape(tmpl[pos:n - 1]) + c, pos) | ||||
pos = n + 1 | ||||
continue | ||||
if n > pos: | ||||
yield ('string', unescape(tmpl[pos:n]), pos) | ||||
if c == quote: | ||||
yield ('end', None, n + 1) | ||||
return | ||||
|
r13176 | |||
|
r36687 | parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}')) | ||
|
r36709 | if not tmpl.startswith('}', pos): | ||
|
r36687 | raise error.ParseError(_("invalid token"), pos) | ||
yield ('template', parseres, n) | ||||
|
r36709 | pos += 1 | ||
|
r25783 | |||
|
r36687 | if quote: | ||
raise error.ParseError(_("unterminated string"), start) | ||||
except error.ParseError as inst: | ||||
if len(inst.args) > 1: # has location | ||||
loc = inst.args[1] | ||||
|
r36688 | # Offset the caret location by the number of newlines before the | ||
# location of the error, since we will replace one-char newlines | ||||
# with the two-char literal r'\n'. | ||||
offset = tmpl[:loc].count('\n') | ||||
tmpl = tmpl.replace('\n', br'\n') | ||||
# We want the caret to point to the place in the template that | ||||
# failed to parse, but in a hint we get a open paren at the | ||||
|
r36709 | # start. Therefore, we print "loc + 1" spaces (instead of "loc") | ||
|
r36688 | # to line up the caret with the location of the error. | ||
|
r36709 | inst.hint = (tmpl + '\n' | ||
+ ' ' * (loc + 1 + offset) + '^ ' + _('here')) | ||||
|
r36687 | raise | ||
|
r36258 | yield ('end', None, pos) | ||
|
r13176 | |||
|
r28547 | def _unnesttemplatelist(tree): | ||
"""Expand list of templates to node tuple | ||||
>>> def f(tree): | ||||
|
r34139 | ... print(pycompat.sysstr(prettyformat(_unnesttemplatelist(tree)))) | ||
|
r34133 | >>> f((b'template', [])) | ||
|
r34075 | (string '') | ||
|
r34133 | >>> f((b'template', [(b'string', b'foo')])) | ||
|
r34075 | (string 'foo') | ||
|
r34133 | >>> f((b'template', [(b'string', b'foo'), (b'symbol', b'rev')])) | ||
|
r28547 | (template | ||
|
r34075 | (string 'foo') | ||
(symbol 'rev')) | ||||
|
r34133 | >>> f((b'template', [(b'symbol', b'rev')])) # template(rev) -> str | ||
|
r28547 | (template | ||
|
r34075 | (symbol 'rev')) | ||
|
r34133 | >>> f((b'template', [(b'template', [(b'string', b'foo')])])) | ||
|
r34075 | (string 'foo') | ||
|
r28547 | """ | ||
if not isinstance(tree, tuple): | ||||
return tree | ||||
op = tree[0] | ||||
if op != 'template': | ||||
return (op,) + tuple(_unnesttemplatelist(x) for x in tree[1:]) | ||||
assert len(tree) == 2 | ||||
xs = tuple(_unnesttemplatelist(x) for x in tree[1]) | ||||
if not xs: | ||||
return ('string', '') # empty template "" | ||||
elif len(xs) == 1 and xs[0][0] == 'string': | ||||
return xs[0] # fast path for string with no template fragment "x" | ||||
else: | ||||
return (op,) + xs | ||||
|
r28545 | def parse(tmpl): | ||
"""Parse template string into tree""" | ||||
|
r25781 | parsed, pos = _parsetemplate(tmpl, 0, len(tmpl)) | ||
|
r28545 | assert pos == len(tmpl), 'unquoted template should be consumed' | ||
|
r28547 | return _unnesttemplatelist(('template', parsed)) | ||
|
r28911 | def _parseexpr(expr): | ||
"""Parse a template expression into tree | ||||
|
r34133 | >>> _parseexpr(b'"foo"') | ||
|
r28911 | ('string', 'foo') | ||
|
r34133 | >>> _parseexpr(b'foo(bar)') | ||
|
r28911 | ('func', ('symbol', 'foo'), ('symbol', 'bar')) | ||
|
r34133 | >>> _parseexpr(b'foo(') | ||
|
r28911 | Traceback (most recent call last): | ||
... | ||||
ParseError: ('not a prefix: end', 4) | ||||
|
r34133 | >>> _parseexpr(b'"foo" "bar"') | ||
|
r28911 | Traceback (most recent call last): | ||
... | ||||
ParseError: ('invalid token', 7) | ||||
""" | ||||
p = parser.parser(elements) | ||||
tree, pos = p.parse(tokenize(expr, 0, len(expr))) | ||||
if pos != len(expr): | ||||
raise error.ParseError(_('invalid token'), pos) | ||||
return _unnesttemplatelist(tree) | ||||
|
r28547 | def prettyformat(tree): | ||
return parser.prettyformat(tree, ('integer', 'string', 'symbol')) | ||||
|
r28545 | |||
|
r25001 | def compileexp(exp, context, curmethods): | ||
|
r28956 | """Compile parsed template tree to (func, data) pair""" | ||
|
r35762 | if not exp: | ||
raise error.ParseError(_("missing argument")) | ||||
|
r13176 | t = exp[0] | ||
|
r25001 | if t in curmethods: | ||
return curmethods[t](exp, context) | ||||
|
r13176 | raise error.ParseError(_("unknown method '%s'") % t) | ||
# template evaluation | ||||
def getsymbol(exp): | ||||
if exp[0] == 'symbol': | ||||
return exp[1] | ||||
|
r21822 | raise error.ParseError(_("expected a symbol, got '%s'") % exp[0]) | ||
|
r13176 | |||
def getlist(x): | ||||
if not x: | ||||
return [] | ||||
if x[0] == 'list': | ||||
return getlist(x[1]) + [x[2]] | ||||
return [x] | ||||
def gettemplate(exp, context): | ||||
|
r28545 | """Compile given template tree or load named template from map file; | ||
returns (func, data) pair""" | ||||
|
r28546 | if exp[0] in ('template', 'string'): | ||
|
r28545 | return compileexp(exp, context, methods) | ||
|
r13176 | if exp[0] == 'symbol': | ||
|
r25599 | # unlike runsymbol(), here 'symbol' is always taken as template name | ||
# even if it exists in mapping. this allows us to override mapping | ||||
# by web templates, e.g. 'changelogtag' is redefined in map file. | ||||
|
r13176 | return context._load(exp[1]) | ||
raise error.ParseError(_("expected template specifier")) | ||||
|
r27940 | def _runrecursivesymbol(context, mapping, key): | ||
raise error.Abort(_("recursive reference '%s' in template") % key) | ||||
|
r25596 | def buildtemplate(exp, context): | ||
|
r28547 | ctmpl = [compileexp(e, context, methods) for e in exp[1:]] | ||
|
r36931 | return (templateutil.runtemplate, ctmpl) | ||
|
r25595 | |||
|
r13176 | def buildfilter(exp, context): | ||
|
r26104 | n = getsymbol(exp[2]) | ||
if n in context._filters: | ||||
filt = context._filters[n] | ||||
|
r31886 | arg = compileexp(exp[1], context, methods) | ||
|
r36931 | return (templateutil.runfilter, (arg, filt)) | ||
|
r36930 | if n in context._funcs: | ||
f = context._funcs[n] | ||||
|
r31886 | args = _buildfuncargs(exp[1], context, methods, n, f._argspec) | ||
return (f, args) | ||||
|
r26104 | raise error.ParseError(_("unknown function '%s'") % n) | ||
|
r13176 | |||
def buildmap(exp, context): | ||||
|
r34326 | darg = compileexp(exp[1], context, methods) | ||
targ = gettemplate(exp[2], context) | ||||
|
r36931 | return (templateutil.runmap, (darg, targ)) | ||
|
r13176 | |||
|
r34536 | def buildmember(exp, context): | ||
darg = compileexp(exp[1], context, methods) | ||||
memb = getsymbol(exp[2]) | ||||
|
r36931 | return (templateutil.runmember, (darg, memb)) | ||
|
r34536 | |||
|
r30115 | def buildnegate(exp, context): | ||
arg = compileexp(exp[1], context, exprmethods) | ||||
|
r36931 | return (templateutil.runnegate, arg) | ||
|
r30115 | |||
def buildarithmetic(exp, context, func): | ||||
left = compileexp(exp[1], context, exprmethods) | ||||
right = compileexp(exp[2], context, exprmethods) | ||||
|
r36931 | return (templateutil.runarithmetic, (func, left, right)) | ||
|
r30115 | |||
|
r13176 | def buildfunc(exp, context): | ||
n = getsymbol(exp[1]) | ||||
|
r36930 | if n in context._funcs: | ||
f = context._funcs[n] | ||||
|
r31886 | args = _buildfuncargs(exp[2], context, exprmethods, n, f._argspec) | ||
|
r14925 | return (f, args) | ||
|
r13176 | if n in context._filters: | ||
|
r31886 | args = _buildfuncargs(exp[2], context, exprmethods, n, argspec=None) | ||
|
r13176 | if len(args) != 1: | ||
raise error.ParseError(_("filter %s expects one argument") % n) | ||||
f = context._filters[n] | ||||
|
r36931 | return (templateutil.runfilter, (args[0], f)) | ||
|
r20857 | raise error.ParseError(_("unknown function '%s'") % n) | ||
|
r13176 | |||
|
r31886 | def _buildfuncargs(exp, context, curmethods, funcname, argspec): | ||
"""Compile parsed tree of function arguments into list or dict of | ||||
|
r31921 | (func, data) pairs | ||
|
r37002 | >>> context = engine(lambda t: (templateutil.runsymbol, t)) | ||
|
r31921 | >>> def fargs(expr, argspec): | ||
... x = _parseexpr(expr) | ||||
... n = getsymbol(x[1]) | ||||
... return _buildfuncargs(x[2], context, exprmethods, n, argspec) | ||||
|
r34141 | >>> list(fargs(b'a(l=1, k=2)', b'k l m').keys()) | ||
|
r31922 | ['l', 'k'] | ||
|
r34133 | >>> args = fargs(b'a(opts=1, k=2)', b'**opts') | ||
|
r34141 | >>> list(args.keys()), list(args[b'opts'].keys()) | ||
|
r31922 | (['opts'], ['opts', 'k']) | ||
|
r31921 | """ | ||
|
r31886 | def compiledict(xs): | ||
|
r31922 | return util.sortdict((k, compileexp(x, context, curmethods)) | ||
for k, x in xs.iteritems()) | ||||
|
r31886 | def compilelist(xs): | ||
return [compileexp(x, context, curmethods) for x in xs] | ||||
if not argspec: | ||||
# filter or function with no argspec: return list of positional args | ||||
return compilelist(getlist(exp)) | ||||
# function with argspec: return dict of named args | ||||
|
r31921 | _poskeys, varkey, _keys, optkey = argspec = parser.splitargspec(argspec) | ||
|
r31886 | treeargs = parser.buildargsdict(getlist(exp), funcname, argspec, | ||
keyvaluenode='keyvalue', keynode='symbol') | ||||
|
r31922 | compargs = util.sortdict() | ||
|
r31886 | if varkey: | ||
compargs[varkey] = compilelist(treeargs.pop(varkey)) | ||||
|
r31921 | if optkey: | ||
compargs[optkey] = compiledict(treeargs.pop(optkey)) | ||||
|
r31886 | compargs.update(compiledict(treeargs)) | ||
return compargs | ||||
|
r31885 | def buildkeyvaluepair(exp, content): | ||
raise error.ParseError(_("can't use a key-value pair in this context")) | ||||
|
r25001 | # methods to interpret function arguments or inner expressions (e.g. {_(x)}) | ||
exprmethods = { | ||||
|
r36931 | "integer": lambda e, c: (templateutil.runinteger, e[1]), | ||
"string": lambda e, c: (templateutil.runstring, e[1]), | ||||
"symbol": lambda e, c: (templateutil.runsymbol, e[1]), | ||||
|
r25596 | "template": buildtemplate, | ||
|
r25001 | "group": lambda e, c: compileexp(e[1], c, exprmethods), | ||
|
r34536 | ".": buildmember, | ||
|
r13176 | "|": buildfilter, | ||
"%": buildmap, | ||||
"func": buildfunc, | ||||
|
r31885 | "keyvalue": buildkeyvaluepair, | ||
|
r30115 | "+": lambda e, c: buildarithmetic(e, c, lambda a, b: a + b), | ||
"-": lambda e, c: buildarithmetic(e, c, lambda a, b: a - b), | ||||
"negate": buildnegate, | ||||
"*": lambda e, c: buildarithmetic(e, c, lambda a, b: a * b), | ||||
"/": lambda e, c: buildarithmetic(e, c, lambda a, b: a // b), | ||||
|
r13176 | } | ||
|
r25001 | # methods to interpret top-level template (e.g. {x}, {x|_}, {x % "y"}) | ||
methods = exprmethods.copy() | ||||
|
r25002 | methods["integer"] = exprmethods["symbol"] # '{1}' as variable | ||
|
r25001 | |||
|
r28912 | class _aliasrules(parser.basealiasrules): | ||
"""Parsing and expansion rule set of template aliases""" | ||||
_section = _('template alias') | ||||
_parse = staticmethod(_parseexpr) | ||||
@staticmethod | ||||
def _trygetfunc(tree): | ||||
"""Return (name, args) if tree is func(...) or ...|filter; otherwise | ||||
None""" | ||||
if tree[0] == 'func' and tree[1][0] == 'symbol': | ||||
return tree[1][1], getlist(tree[2]) | ||||
if tree[0] == '|' and tree[2][0] == 'symbol': | ||||
return tree[2][1], [tree[1]] | ||||
def expandaliases(tree, aliases): | ||||
"""Return new tree of aliases are expanded""" | ||||
aliasmap = _aliasrules.buildmap(aliases) | ||||
return _aliasrules.expand(aliasmap, tree) | ||||
|
r13176 | # template engine | ||
|
r1901 | |||
|
r24988 | def unquotestring(s): | ||
|
r28630 | '''unwrap quotes if any; otherwise returns unmodified string''' | ||
|
r28687 | if len(s) < 2 or s[0] not in "'\"" or s[0] != s[-1]: | ||
|
r28630 | return s | ||
|
r25696 | return s[1:-1] | ||
|
r1896 | |||
|
r37091 | class resourcemapper(object): | ||
"""Mapper of internal template resources""" | ||||
__metaclass__ = abc.ABCMeta | ||||
@abc.abstractmethod | ||||
|
r37093 | def availablekeys(self, context, mapping): | ||
"""Return a set of available resource keys based on the given mapping""" | ||||
@abc.abstractmethod | ||||
|
r37091 | def knownkeys(self): | ||
"""Return a set of supported resource keys""" | ||||
@abc.abstractmethod | ||||
def lookup(self, context, mapping, key): | ||||
"""Return a resource for the key if available; otherwise None""" | ||||
|
r37120 | @abc.abstractmethod | ||
def populatemap(self, context, origmapping, newmapping): | ||||
"""Return a dict of additional mapping items which should be paired | ||||
with the given new mapping""" | ||||
|
r37091 | class nullresourcemapper(resourcemapper): | ||
|
r37093 | def availablekeys(self, context, mapping): | ||
return set() | ||||
|
r37091 | def knownkeys(self): | ||
return set() | ||||
def lookup(self, context, mapping, key): | ||||
return None | ||||
|
r37120 | def populatemap(self, context, origmapping, newmapping): | ||
return {} | ||||
|
r8218 | class engine(object): | ||
|
r1909 | '''template expansion engine. | ||
template expansion works like this. a map file contains key=value | ||||
pairs. if value is quoted, it is treated as string. otherwise, it | ||||
is treated as name of template file. | ||||
templater is asked to expand a key in map. it looks up key, and | ||||
|
r4334 | looks for strings like this: {foo}. it expands {foo} by looking up | ||
|
r1909 | foo in map, and substituting it. expansion is recursive: it stops | ||
when there is no more {foo} to replace. | ||||
expansion also allows formatting and filtering. | ||||
format uses key to expand each item in list. syntax is | ||||
{key%format}. | ||||
filter uses function to transform value. syntax is | ||||
{key|filter1|filter2|...}.''' | ||||
|
r35484 | def __init__(self, loader, filters=None, defaults=None, resources=None, | ||
aliases=()): | ||||
|
r10848 | self._loader = loader | ||
|
r26330 | if filters is None: | ||
filters = {} | ||||
|
r10848 | self._filters = filters | ||
|
r36940 | self._funcs = templatefuncs.funcs # make this a parameter if needed | ||
|
r26331 | if defaults is None: | ||
defaults = {} | ||||
|
r35484 | if resources is None: | ||
|
r37091 | resources = nullresourcemapper() | ||
|
r10848 | self._defaults = defaults | ||
|
r35484 | self._resources = resources | ||
|
r28957 | self._aliasmap = _aliasrules.buildmap(aliases) | ||
|
r28545 | self._cache = {} # key: (func, data) | ||
|
r37416 | self._tmplcache = {} # literal template: (func, data) | ||
|
r8218 | |||
|
r37092 | def overlaymap(self, origmapping, newmapping): | ||
"""Create combined mapping from the original mapping and partial | ||||
mapping to override the original""" | ||||
|
r37093 | # do not copy symbols which overrides the defaults depending on | ||
# new resources, so the defaults will be re-evaluated (issue5612) | ||||
knownres = self._resources.knownkeys() | ||||
newres = self._resources.availablekeys(self, newmapping) | ||||
mapping = {k: v for k, v in origmapping.iteritems() | ||||
if (k in knownres # not a symbol per self.symbol() | ||||
or newres.isdisjoint(self._defaultrequires(k)))} | ||||
|
r37092 | mapping.update(newmapping) | ||
|
r37120 | mapping.update( | ||
self._resources.populatemap(self, origmapping, newmapping)) | ||||
|
r37092 | return mapping | ||
|
r37093 | def _defaultrequires(self, key): | ||
"""Resource keys required by the specified default symbol function""" | ||||
v = self._defaults.get(key) | ||||
if v is None or not callable(v): | ||||
return () | ||||
return getattr(v, '_requires', ()) | ||||
|
r35483 | def symbol(self, mapping, key): | ||
"""Resolve symbol to value or function; None if nothing found""" | ||||
|
r35486 | v = None | ||
|
r37091 | if key not in self._resources.knownkeys(): | ||
|
r35486 | v = mapping.get(key) | ||
|
r35483 | if v is None: | ||
v = self._defaults.get(key) | ||||
return v | ||||
|
r37519 | def availableresourcekeys(self, mapping): | ||
"""Return a set of available resource keys based on the given mapping""" | ||||
return self._resources.availablekeys(self, mapping) | ||||
def knownresourcekeys(self): | ||||
"""Return a set of supported resource keys""" | ||||
return self._resources.knownkeys() | ||||
|
r35483 | def resource(self, mapping, key): | ||
"""Return internal data (e.g. cache) used for keyword/function | ||||
evaluation""" | ||||
|
r37091 | v = self._resources.lookup(self, mapping, key) | ||
|
r35484 | if v is None: | ||
|
r36931 | raise templateutil.ResourceUnavailable( | ||
_('template resource not available: %s') % key) | ||||
|
r35484 | return v | ||
|
r35483 | |||
|
r13176 | def _load(self, t): | ||
'''load, parse, and cache a template''' | ||||
if t not in self._cache: | ||||
|
r27940 | # put poison to cut recursion while compiling 't' | ||
|
r28545 | self._cache[t] = (_runrecursivesymbol, t) | ||
|
r27940 | try: | ||
|
r28956 | x = parse(self._loader(t)) | ||
|
r28957 | if self._aliasmap: | ||
x = _aliasrules.expand(self._aliasmap, x) | ||||
|
r28956 | self._cache[t] = compileexp(x, self, methods) | ||
|
r27940 | except: # re-raises | ||
del self._cache[t] | ||||
raise | ||||
|
r13176 | return self._cache[t] | ||
|
r37416 | def _parse(self, tmpl): | ||
"""Parse and cache a literal template""" | ||||
if tmpl not in self._tmplcache: | ||||
x = parse(tmpl) | ||||
self._tmplcache[tmpl] = compileexp(x, self, methods) | ||||
return self._tmplcache[tmpl] | ||||
|
r37085 | def preload(self, t): | ||
"""Load, parse, and cache the specified template if available""" | ||||
try: | ||||
self._load(t) | ||||
return True | ||||
except templateutil.TemplateNotFound: | ||||
return False | ||||
|
r10853 | def process(self, t, mapping): | ||
'''Perform expansion. t is name of map element to expand. | ||||
mapping contains added elements for use during expansion. Is a | ||||
generator.''' | ||||
|
r28545 | func, data = self._load(t) | ||
|
r37416 | return self._expand(func, data, mapping) | ||
def expand(self, tmpl, mapping): | ||||
"""Perform expansion over a literal template | ||||
No user aliases will be expanded since this is supposed to be called | ||||
with an internal template string. | ||||
""" | ||||
func, data = self._parse(tmpl) | ||||
return self._expand(func, data, mapping) | ||||
def _expand(self, func, data, mapping): | ||||
|
r37120 | # populate additional items only if they don't exist in the given | ||
# mapping. this is slightly different from overlaymap() because the | ||||
# initial 'revcache' may contain pre-computed items. | ||||
extramapping = self._resources.populatemap(self, {}, mapping) | ||||
if extramapping: | ||||
extramapping.update(mapping) | ||||
mapping = extramapping | ||||
|
r37290 | return templateutil.flatten(self, mapping, func(self, mapping, data)) | ||
|
r8218 | |||
|
r8361 | engines = {'default': engine} | ||
|
r19125 | def stylelist(): | ||
|
r22634 | paths = templatepaths() | ||
|
r20312 | if not paths: | ||
return _('no templates found, try `hg debuginstall` for more info') | ||||
|
r27637 | dirlist = os.listdir(paths[0]) | ||
|
r19125 | stylelist = [] | ||
for file in dirlist: | ||||
split = file.split(".") | ||||
|
r28403 | if split[-1] in ('orig', 'rej'): | ||
continue | ||||
|
r19125 | if split[0] == "map-cmdline": | ||
stylelist.append(split[1]) | ||||
|
r19127 | return ", ".join(sorted(stylelist)) | ||
|
r19125 | |||
|
r28953 | def _readmapfile(mapfile): | ||
"""Load template elements from the given map file""" | ||||
if not os.path.exists(mapfile): | ||||
raise error.Abort(_("style '%s' not found") % mapfile, | ||||
hint=_("available styles: %s") % stylelist()) | ||||
base = os.path.dirname(mapfile) | ||||
conf = config.config(includepaths=templatepaths()) | ||||
|
r34715 | conf.read(mapfile, remap={'': 'templates'}) | ||
|
r28953 | |||
cache = {} | ||||
tmap = {} | ||||
|
r34716 | aliases = [] | ||
|
r34713 | |||
|
r34715 | val = conf.get('templates', '__base__') | ||
|
r34713 | if val and val[0] not in "'\"": | ||
# treat as a pointer to a base class for this style | ||||
path = util.normpath(os.path.join(base, val)) | ||||
# fallback check in template paths | ||||
if not os.path.exists(path): | ||||
for p in templatepaths(): | ||||
p2 = util.normpath(os.path.join(p, val)) | ||||
if os.path.isfile(p2): | ||||
path = p2 | ||||
break | ||||
p3 = util.normpath(os.path.join(p2, "map")) | ||||
if os.path.isfile(p3): | ||||
path = p3 | ||||
break | ||||
|
r34716 | cache, tmap, aliases = _readmapfile(path) | ||
|
r34713 | |||
|
r34715 | for key, val in conf['templates'].items(): | ||
|
r28953 | if not val: | ||
|
r34715 | raise error.ParseError(_('missing value'), | ||
conf.source('templates', key)) | ||||
|
r28953 | if val[0] in "'\"": | ||
if val[0] != val[-1]: | ||||
raise error.ParseError(_('unmatched quotes'), | ||||
|
r34715 | conf.source('templates', key)) | ||
|
r28953 | cache[key] = unquotestring(val) | ||
|
r34713 | elif key != '__base__': | ||
|
r28953 | val = 'default', val | ||
if ':' in val[1]: | ||||
val = val[1].split(':', 1) | ||||
tmap[key] = val[0], os.path.join(base, val[1]) | ||||
|
r34716 | aliases.extend(conf['templatealias'].items()) | ||
return cache, tmap, aliases | ||||
|
r28953 | |||
|
r8218 | class templater(object): | ||
|
r35484 | def __init__(self, filters=None, defaults=None, resources=None, | ||
cache=None, aliases=(), minchunk=1024, maxchunk=65536): | ||||
|
r35497 | """Create template engine optionally with preloaded template fragments | ||
- ``filters``: a dict of functions to transform a value into another. | ||||
- ``defaults``: a dict of symbol values/functions; may be overridden | ||||
by a ``mapping`` dict. | ||||
|
r37091 | - ``resources``: a resourcemapper object to look up internal data | ||
|
r36998 | (e.g. cache), inaccessible from user template. | ||
|
r35497 | - ``cache``: a dict of preloaded template fragments. | ||
- ``aliases``: a list of alias (name, replacement) pairs. | ||||
self.cache may be updated later to register additional template | ||||
fragments. | ||||
""" | ||||
|
r26332 | if filters is None: | ||
filters = {} | ||||
|
r26333 | if defaults is None: | ||
defaults = {} | ||||
|
r26334 | if cache is None: | ||
cache = {} | ||||
|
r1975 | self.cache = cache.copy() | ||
|
r1896 | self.map = {} | ||
|
r8360 | self.filters = templatefilters.filters.copy() | ||
self.filters.update(filters) | ||||
|
r1964 | self.defaults = defaults | ||
|
r37088 | self._resources = resources | ||
|
r28957 | self._aliases = aliases | ||
|
r7396 | self.minchunk, self.maxchunk = minchunk, maxchunk | ||
|
r13187 | self.ecache = {} | ||
|
r1896 | |||
|
r28954 | @classmethod | ||
|
r35484 | def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None, | ||
cache=None, minchunk=1024, maxchunk=65536): | ||||
|
r28954 | """Create templater from the specified map file""" | ||
|
r35484 | t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk) | ||
|
r34716 | cache, tmap, aliases = _readmapfile(mapfile) | ||
|
r28954 | t.cache.update(cache) | ||
t.map = tmap | ||||
|
r34716 | t._aliases = aliases | ||
|
r28954 | return t | ||
|
r1896 | |||
|
r1899 | def __contains__(self, key): | ||
|
r3637 | return key in self.cache or key in self.map | ||
|
r1899 | |||
|
r8218 | def load(self, t): | ||
|
r6783 | '''Get the template for the given template name. Use a local cache.''' | ||
|
r16686 | if t not in self.cache: | ||
|
r1905 | try: | ||
|
r14168 | self.cache[t] = util.readfile(self.map[t][1]) | ||
|
r25660 | except KeyError as inst: | ||
|
r36931 | raise templateutil.TemplateNotFound( | ||
_('"%s" not in template map') % inst.args[0]) | ||||
|
r25660 | except IOError as inst: | ||
|
r36518 | reason = (_('template file %s: %s') | ||
|
r37102 | % (self.map[t][1], | ||
stringutil.forcebytestr(inst.args[1]))) | ||||
|
r36518 | raise IOError(inst.args[0], encoding.strfromlocal(reason)) | ||
|
r6783 | return self.cache[t] | ||
|
r1896 | |||
|
r37003 | def renderdefault(self, mapping): | ||
|
r32873 | """Render the default unnamed template and return result as string""" | ||
|
r37004 | return self.render('', mapping) | ||
def render(self, t, mapping): | ||||
"""Render the specified named template and return result as string""" | ||||
|
r37176 | return b''.join(self.generate(t, mapping)) | ||
|
r32873 | |||
|
r37037 | def generate(self, t, mapping): | ||
"""Return a generator that renders the specified named template and | ||||
yields chunks""" | ||||
|
r8361 | ttype = t in self.map and self.map[t][0] or 'default' | ||
|
r13187 | if ttype not in self.ecache: | ||
|
r28831 | try: | ||
ecls = engines[ttype] | ||||
except KeyError: | ||||
raise error.Abort(_('invalid template engine: %s') % ttype) | ||||
|
r28957 | self.ecache[ttype] = ecls(self.load, self.filters, self.defaults, | ||
|
r35484 | self._resources, self._aliases) | ||
|
r13187 | proc = self.ecache[ttype] | ||
|
r8361 | |||
|
r10847 | stream = proc.process(t, mapping) | ||
|
r7396 | if self.minchunk: | ||
stream = util.increasingchunks(stream, min=self.minchunk, | ||||
max=self.maxchunk) | ||||
return stream | ||||
|
r7434 | |||
|
r22634 | def templatepaths(): | ||
'''return locations used for template files.''' | ||||
|
r22636 | pathsrel = ['templates'] | ||
|
r22635 | paths = [os.path.normpath(os.path.join(util.datapath, f)) | ||
for f in pathsrel] | ||||
return [p for p in paths if os.path.isdir(p)] | ||||
|
r2189 | |||
|
r22634 | def templatepath(name): | ||
'''return location of template file. returns None if not found.''' | ||||
for p in templatepaths(): | ||||
f = os.path.join(p, name) | ||||
if os.path.exists(f): | ||||
return f | ||||
return None | ||||
|
r2189 | |||
|
r9842 | def stylemap(styles, paths=None): | ||
|
r7966 | """Return path to mapfile for a given style. | ||
Searches mapfile in the following locations: | ||||
1. templatepath/style/map | ||||
2. templatepath/map-style | ||||
3. templatepath/map | ||||
""" | ||||
if paths is None: | ||||
|
r22634 | paths = templatepaths() | ||
|
r36568 | elif isinstance(paths, bytes): | ||
|
r8223 | paths = [paths] | ||
|
r7966 | |||
|
r36568 | if isinstance(styles, bytes): | ||
|
r9842 | styles = [styles] | ||
for style in styles: | ||||
|
r24296 | # only plain name is allowed to honor template paths | ||
if (not style | ||||
|
r36666 | or style in (pycompat.oscurdir, pycompat.ospardir) | ||
|
r30615 | or pycompat.ossep in style | ||
|
r30625 | or pycompat.osaltsep and pycompat.osaltsep in style): | ||
|
r9842 | continue | ||
locations = [os.path.join(style, 'map'), 'map-' + style] | ||||
locations.append('map') | ||||
for path in paths: | ||||
for location in locations: | ||||
mapfile = os.path.join(path, location) | ||||
if os.path.isfile(mapfile): | ||||
return style, mapfile | ||||
|
r7966 | |||
raise RuntimeError("No hgweb templates found in %r" % paths) | ||||