Mercurial > hg-stable
annotate mercurial/templateutil.py @ 37073:1101d6747d2d
templater: drop 'templ' from resources dict
Partially resolves cycle, templ -> context -> templ. This will make it easier
to replace the resources dict with new immutable resource mapper interface.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Fri, 16 Mar 2018 23:09:21 +0900 |
parents | aa97e06a1912 |
children | 44757e6dad93 |
rev | line source |
---|---|
36919
da2977e674a3
templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36918
diff
changeset
|
1 # templateutil.py - utility for template evaluation |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
2 # |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
4 # |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
8223
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
10263 | 6 # GNU General Public License version 2 or any later version. |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
7 |
36919
da2977e674a3
templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36918
diff
changeset
|
8 from __future__ import absolute_import |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
9 |
17982
e06e9fd2d99f
template engine: convert generator-based iterator to list-based iterator
Weiwen <weiwen@fb.com>
parents:
17890
diff
changeset
|
10 import types |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
11 |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
12 from .i18n import _ |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
13 from . import ( |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
14 error, |
30620
bb77654dc7ae
py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30342
diff
changeset
|
15 pycompat, |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
16 util, |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
17 ) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
18 |
36474
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36473
diff
changeset
|
19 class ResourceUnavailable(error.Abort): |
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36473
diff
changeset
|
20 pass |
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36473
diff
changeset
|
21 |
36473
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36278
diff
changeset
|
22 class TemplateNotFound(error.Abort): |
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36278
diff
changeset
|
23 pass |
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36278
diff
changeset
|
24 |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
25 class hybrid(object): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
26 """Wrapper for list or dict to support legacy template |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
27 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
28 This class allows us to handle both: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
29 - "{files}" (legacy command-line-specific list hack) and |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
30 - "{files % '{file}\n'}" (hgweb-style with inlining and function support) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
31 and to access raw values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
32 - "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
33 - "{get(extras, key)}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
34 - "{files|json}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
35 """ |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
36 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
37 def __init__(self, gen, values, makemap, joinfmt, keytype=None): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
38 if gen is not None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
39 self.gen = gen # generator or function returning generator |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
40 self._values = values |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
41 self._makemap = makemap |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
42 self.joinfmt = joinfmt |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
43 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
44 def gen(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
45 """Default generator to stringify this as {join(self, ' ')}""" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
46 for i, x in enumerate(self._values): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
47 if i > 0: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
48 yield ' ' |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
49 yield self.joinfmt(x) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
50 def itermaps(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
51 makemap = self._makemap |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
52 for x in self._values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
53 yield makemap(x) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
54 def __contains__(self, x): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
55 return x in self._values |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
56 def __getitem__(self, key): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
57 return self._values[key] |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
58 def __len__(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
59 return len(self._values) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
60 def __iter__(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
61 return iter(self._values) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
62 def __getattr__(self, name): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
63 if name not in (r'get', r'items', r'iteritems', r'iterkeys', |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
64 r'itervalues', r'keys', r'values'): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
65 raise AttributeError(name) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
66 return getattr(self._values, name) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
67 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
68 class mappable(object): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
69 """Wrapper for non-list/dict object to support map operation |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
70 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
71 This class allows us to handle both: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
72 - "{manifest}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
73 - "{manifest % '{rev}:{node}'}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
74 - "{manifest.rev}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
75 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
76 Unlike a hybrid, this does not simulate the behavior of the underling |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
77 value. Use unwrapvalue() or unwraphybrid() to obtain the inner object. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
78 """ |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
79 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
80 def __init__(self, gen, key, value, makemap): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
81 if gen is not None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
82 self.gen = gen # generator or function returning generator |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
83 self._key = key |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
84 self._value = value # may be generator of strings |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
85 self._makemap = makemap |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
86 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
87 def gen(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
88 yield pycompat.bytestr(self._value) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
89 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
90 def tomap(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
91 return self._makemap(self._key) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
92 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
93 def itermaps(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
94 yield self.tomap() |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
95 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
96 def hybriddict(data, key='key', value='value', fmt=None, gen=None): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
97 """Wrap data to support both dict-like and string-like operations""" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
98 prefmt = pycompat.identity |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
99 if fmt is None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
100 fmt = '%s=%s' |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
101 prefmt = pycompat.bytestr |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
102 return hybrid(gen, data, lambda k: {key: k, value: data[k]}, |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
103 lambda k: fmt % (prefmt(k), prefmt(data[k]))) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
104 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
105 def hybridlist(data, name, fmt=None, gen=None): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
106 """Wrap data to support both list-like and string-like operations""" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
107 prefmt = pycompat.identity |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
108 if fmt is None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
109 fmt = '%s' |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
110 prefmt = pycompat.bytestr |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
111 return hybrid(gen, data, lambda x: {name: x}, lambda x: fmt % prefmt(x)) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
112 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
113 def unwraphybrid(thing): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
114 """Return an object which can be stringified possibly by using a legacy |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
115 template""" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
116 gen = getattr(thing, 'gen', None) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
117 if gen is None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
118 return thing |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
119 if callable(gen): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
120 return gen() |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
121 return gen |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
122 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
123 def unwrapvalue(thing): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
124 """Move the inner value object out of the wrapper""" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
125 if not util.safehasattr(thing, '_value'): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
126 return thing |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
127 return thing._value |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
128 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
129 def wraphybridvalue(container, key, value): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
130 """Wrap an element of hybrid container to be mappable |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
131 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
132 The key is passed to the makemap function of the given container, which |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
133 should be an item generated by iter(container). |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
134 """ |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
135 makemap = getattr(container, '_makemap', None) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
136 if makemap is None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
137 return value |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
138 if util.safehasattr(value, '_makemap'): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
139 # a nested hybrid list/dict, which has its own way of map operation |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
140 return value |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
141 return mappable(None, key, value, makemap) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
142 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
143 def compatdict(context, mapping, name, data, key='key', value='value', |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
144 fmt=None, plural=None, separator=' '): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
145 """Wrap data like hybriddict(), but also supports old-style list template |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
146 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
147 This exists for backward compatibility with the old-style template. Use |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
148 hybriddict() for new template keywords. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
149 """ |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
150 c = [{key: k, value: v} for k, v in data.iteritems()] |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
151 f = _showcompatlist(context, mapping, name, c, plural, separator) |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
152 return hybriddict(data, key=key, value=value, fmt=fmt, gen=f) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
153 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
154 def compatlist(context, mapping, name, data, element=None, fmt=None, |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
155 plural=None, separator=' '): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
156 """Wrap data like hybridlist(), but also supports old-style list template |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
157 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
158 This exists for backward compatibility with the old-style template. Use |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
159 hybridlist() for new template keywords. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
160 """ |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
161 f = _showcompatlist(context, mapping, name, data, plural, separator) |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
162 return hybridlist(data, name=element or name, fmt=fmt, gen=f) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
163 |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
164 def _showcompatlist(context, mapping, name, values, plural=None, separator=' '): |
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
165 """Return a generator that renders old-style list template |
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
166 |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
167 name is name of key in template map. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
168 values is list of strings or dicts. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
169 plural is plural of name, if not simply name + 's'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
170 separator is used to join values as a string |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
171 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
172 expansion works like this, given name 'foo'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
173 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
174 if values is empty, expand 'no_foos'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
175 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
176 if 'foo' not in template map, return values as a string, |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
177 joined by 'separator'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
178 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
179 expand 'start_foos'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
180 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
181 for each value, expand 'foo'. if 'last_foo' in template |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
182 map, expand it instead of 'foo' for last key. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
183 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
184 expand 'end_foos'. |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
185 """ |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
186 if not plural: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
187 plural = name + 's' |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
188 if not values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
189 noname = 'no_' + plural |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
190 if context.preload(noname): |
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
191 yield context.process(noname, mapping) |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
192 return |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
193 if not context.preload(name): |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
194 if isinstance(values[0], bytes): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
195 yield separator.join(values) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
196 else: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
197 for v in values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
198 r = dict(v) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
199 r.update(mapping) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
200 yield r |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
201 return |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
202 startname = 'start_' + plural |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
203 if context.preload(startname): |
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
204 yield context.process(startname, mapping) |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
205 vmapping = mapping.copy() |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
206 def one(v, tag=name): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
207 try: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
208 vmapping.update(v) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
209 # Python 2 raises ValueError if the type of v is wrong. Python |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
210 # 3 raises TypeError. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
211 except (AttributeError, TypeError, ValueError): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
212 try: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
213 # Python 2 raises ValueError trying to destructure an e.g. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
214 # bytes. Python 3 raises TypeError. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
215 for a, b in v: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
216 vmapping[a] = b |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
217 except (TypeError, ValueError): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
218 vmapping[name] = v |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
219 return context.process(tag, vmapping) |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
220 lastname = 'last_' + name |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
221 if context.preload(lastname): |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
222 last = values.pop() |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
223 else: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
224 last = None |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
225 for v in values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
226 yield one(v) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
227 if last is not None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
228 yield one(last, tag=lastname) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
229 endname = 'end_' + plural |
37071
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
230 if context.preload(endname): |
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37022
diff
changeset
|
231 yield context.process(endname, mapping) |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
232 |
36926
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
233 def stringify(thing): |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
234 """Turn values into bytes by converting into text and concatenating them""" |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
235 thing = unwraphybrid(thing) |
36926
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
236 if util.safehasattr(thing, '__iter__') and not isinstance(thing, bytes): |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
237 if isinstance(thing, str): |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
238 # This is only reachable on Python 3 (otherwise |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
239 # isinstance(thing, bytes) would have been true), and is |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
240 # here to prevent infinite recursion bugs on Python 3. |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
241 raise error.ProgrammingError( |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
242 'stringify got unexpected unicode string: %r' % thing) |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
243 return "".join([stringify(t) for t in thing if t is not None]) |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
244 if thing is None: |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
245 return "" |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
246 return pycompat.bytestr(thing) |
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36919
diff
changeset
|
247 |
31927
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
248 def findsymbolicname(arg): |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
249 """Find symbolic name for the given compiled expression; returns None |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
250 if nothing found reliably""" |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
251 while True: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
252 func, data = arg |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
253 if func is runsymbol: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
254 return data |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
255 elif func is runfilter: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
256 arg = data[0] |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
257 else: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
258 return None |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
259 |
34333
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
260 def evalrawexp(context, mapping, arg): |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
261 """Evaluate given argument as a bare template object which may require |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
262 further processing (such as folding generator of strings)""" |
26124
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
263 func, data = arg |
34333
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
264 return func(context, mapping, data) |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
265 |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
266 def evalfuncarg(context, mapping, arg): |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
267 """Evaluate given argument as value type""" |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
268 thing = evalrawexp(context, mapping, arg) |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
269 thing = unwrapvalue(thing) |
34333
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
270 # evalrawexp() may return string, generator of strings or arbitrary object |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
271 # such as date tuple, but filter does not want generator. |
26124
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
272 if isinstance(thing, types.GeneratorType): |
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
273 thing = stringify(thing) |
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
274 return thing |
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
275 |
29827
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29826
diff
changeset
|
276 def evalboolean(context, mapping, arg): |
29828
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29827
diff
changeset
|
277 """Evaluate given argument as boolean, but also takes boolean literals""" |
29827
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29826
diff
changeset
|
278 func, data = arg |
29828
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29827
diff
changeset
|
279 if func is runsymbol: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29827
diff
changeset
|
280 thing = func(context, mapping, data, default=None) |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29827
diff
changeset
|
281 if thing is None: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29827
diff
changeset
|
282 # not a template keyword, takes as a boolean literal |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29827
diff
changeset
|
283 thing = util.parsebool(data) |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29827
diff
changeset
|
284 else: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29827
diff
changeset
|
285 thing = func(context, mapping, data) |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
286 thing = unwrapvalue(thing) |
29827
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29826
diff
changeset
|
287 if isinstance(thing, bool): |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29826
diff
changeset
|
288 return thing |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29826
diff
changeset
|
289 # other objects are evaluated as strings, which means 0 is True, but |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29826
diff
changeset
|
290 # empty dict/list should be False as they are expected to be '' |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29826
diff
changeset
|
291 return bool(stringify(thing)) |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29826
diff
changeset
|
292 |
34581
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
293 def evalinteger(context, mapping, arg, err=None): |
28344
ac371d4c007f
templater: drop redundant type conversion when evaluating integer argument
Yuya Nishihara <yuya@tcha.org>
parents:
28343
diff
changeset
|
294 v = evalfuncarg(context, mapping, arg) |
28343
a6c2310b3827
templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents:
28334
diff
changeset
|
295 try: |
28344
ac371d4c007f
templater: drop redundant type conversion when evaluating integer argument
Yuya Nishihara <yuya@tcha.org>
parents:
28343
diff
changeset
|
296 return int(v) |
ac371d4c007f
templater: drop redundant type conversion when evaluating integer argument
Yuya Nishihara <yuya@tcha.org>
parents:
28343
diff
changeset
|
297 except (TypeError, ValueError): |
34581
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
298 raise error.ParseError(err or _('not an integer')) |
28343
a6c2310b3827
templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents:
28334
diff
changeset
|
299 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
300 def evalstring(context, mapping, arg): |
34333
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
301 return stringify(evalrawexp(context, mapping, arg)) |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
302 |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
303 def evalstringliteral(context, mapping, arg): |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
304 """Evaluate given argument as string template, but returns symbol name |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
305 if it is unknown""" |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
306 func, data = arg |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
307 if func is runsymbol: |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
308 thing = func(context, mapping, data, default=data) |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
309 else: |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
310 thing = func(context, mapping, data) |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
311 return stringify(thing) |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
312 |
34581
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
313 _evalfuncbytype = { |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
314 bool: evalboolean, |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
315 bytes: evalstring, |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
316 int: evalinteger, |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
317 } |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
318 |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
319 def evalastype(context, mapping, arg, typ): |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
320 """Evaluate given argument and coerce its type""" |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
321 try: |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
322 f = _evalfuncbytype[typ] |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
323 except KeyError: |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
324 raise error.ProgrammingError('invalid type specified: %r' % typ) |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
325 return f(context, mapping, arg) |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
326 |
25002
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
327 def runinteger(context, mapping, data): |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
328 return int(data) |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
329 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
330 def runstring(context, mapping, data): |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
331 return data |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
332 |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
333 def _recursivesymbolblocker(key): |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
334 def showrecursion(**args): |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
335 raise error.Abort(_("recursive reference '%s' in template") % key) |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
336 return showrecursion |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
337 |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
338 def runsymbol(context, mapping, key, default=''): |
35471
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35421
diff
changeset
|
339 v = context.symbol(mapping, key) |
19770
0361163efbaf
templater: support using templates with non-standard names from map file
Alexander Plavin <alexander@plav.in>
parents:
19390
diff
changeset
|
340 if v is None: |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
341 # put poison to cut recursion. we can't move this to parsing phase |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
342 # because "x = {x}" is allowed if "x" is a keyword. (issue4758) |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
343 safemapping = mapping.copy() |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
344 safemapping[key] = _recursivesymbolblocker(key) |
19770
0361163efbaf
templater: support using templates with non-standard names from map file
Alexander Plavin <alexander@plav.in>
parents:
19390
diff
changeset
|
345 try: |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
346 v = context.process(key, safemapping) |
19770
0361163efbaf
templater: support using templates with non-standard names from map file
Alexander Plavin <alexander@plav.in>
parents:
19390
diff
changeset
|
347 except TemplateNotFound: |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
348 v = default |
36475
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
349 if callable(v) and getattr(v, '_requires', None) is None: |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
350 # old templatekw: expand all keywords and resources |
37073
1101d6747d2d
templater: drop 'templ' from resources dict
Yuya Nishihara <yuya@tcha.org>
parents:
37071
diff
changeset
|
351 # (TODO: deprecate this after porting web template keywords to new API) |
36985
255f635c3204
templater: convert resources to a table of callables for future extension
Yuya Nishihara <yuya@tcha.org>
parents:
36927
diff
changeset
|
352 props = {k: f(context, mapping, k) |
255f635c3204
templater: convert resources to a table of callables for future extension
Yuya Nishihara <yuya@tcha.org>
parents:
36927
diff
changeset
|
353 for k, f in context._resources.items()} |
37073
1101d6747d2d
templater: drop 'templ' from resources dict
Yuya Nishihara <yuya@tcha.org>
parents:
37071
diff
changeset
|
354 # pass context to _showcompatlist() through templatekw._showlist() |
1101d6747d2d
templater: drop 'templ' from resources dict
Yuya Nishihara <yuya@tcha.org>
parents:
37071
diff
changeset
|
355 props['templ'] = context |
35472
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35471
diff
changeset
|
356 props.update(mapping) |
35588
dadbf213a765
py3: convert dict keys' to str before passing as kwargs
Pulkit Goyal <7895pulkit@gmail.com>
parents:
35485
diff
changeset
|
357 return v(**pycompat.strkwargs(props)) |
36475
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
358 if callable(v): |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
359 # new templatekw |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
360 try: |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
361 return v(context, mapping) |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
362 except ResourceUnavailable: |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
363 # unsupported keyword is mapped to empty just like unknown keyword |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36474
diff
changeset
|
364 return None |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
365 return v |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
366 |
25595
a7dd6692e5cb
templater: move runtemplate function out of buildmap/runmap pair
Yuya Nishihara <yuya@tcha.org>
parents:
25580
diff
changeset
|
367 def runtemplate(context, mapping, template): |
34333
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
368 for arg in template: |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
369 yield evalrawexp(context, mapping, arg) |
25595
a7dd6692e5cb
templater: move runtemplate function out of buildmap/runmap pair
Yuya Nishihara <yuya@tcha.org>
parents:
25580
diff
changeset
|
370 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
371 def runfilter(context, mapping, data): |
26125
c990afab2243
templater: drop unneeded destructuring of argument tuple at buildfilter
Yuya Nishihara <yuya@tcha.org>
parents:
26124
diff
changeset
|
372 arg, filt = data |
c990afab2243
templater: drop unneeded destructuring of argument tuple at buildfilter
Yuya Nishihara <yuya@tcha.org>
parents:
26124
diff
changeset
|
373 thing = evalfuncarg(context, mapping, arg) |
17383
099c778ceb33
templater: abort when a template filter raises an exception (issue2987)
Neil Kodner <neilk@fb.com>
parents:
17334
diff
changeset
|
374 try: |
24280
6c55e37ba5f2
templater: allow piping generator-type function output to filters
Yuya Nishihara <yuya@tcha.org>
parents:
24240
diff
changeset
|
375 return filt(thing) |
17383
099c778ceb33
templater: abort when a template filter raises an exception (issue2987)
Neil Kodner <neilk@fb.com>
parents:
17334
diff
changeset
|
376 except (ValueError, AttributeError, TypeError): |
31927
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
377 sym = findsymbolicname(arg) |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
378 if sym: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
379 msg = (_("template filter '%s' is not compatible with keyword '%s'") |
34838
d3ea6a1c798f
templater: use pycompat.sysbytes to bytes-ify some __name__ attrs
Augie Fackler <augie@google.com>
parents:
34808
diff
changeset
|
380 % (pycompat.sysbytes(filt.__name__), sym)) |
17383
099c778ceb33
templater: abort when a template filter raises an exception (issue2987)
Neil Kodner <neilk@fb.com>
parents:
17334
diff
changeset
|
381 else: |
34838
d3ea6a1c798f
templater: use pycompat.sysbytes to bytes-ify some __name__ attrs
Augie Fackler <augie@google.com>
parents:
34808
diff
changeset
|
382 msg = (_("incompatible use of template filter '%s'") |
d3ea6a1c798f
templater: use pycompat.sysbytes to bytes-ify some __name__ attrs
Augie Fackler <augie@google.com>
parents:
34808
diff
changeset
|
383 % pycompat.sysbytes(filt.__name__)) |
31927
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
384 raise error.Abort(msg) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
385 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
386 def runmap(context, mapping, data): |
34333
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
387 darg, targ = data |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
388 d = evalrawexp(context, mapping, darg) |
27891
ac8c0ee5c3b8
templater: make _hybrid not callable to avoid conflicting semantics
Yuya Nishihara <yuya@tcha.org>
parents:
27637
diff
changeset
|
389 if util.safehasattr(d, 'itermaps'): |
28349
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
390 diter = d.itermaps() |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
391 else: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
392 try: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
393 diter = iter(d) |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
394 except TypeError: |
34333
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
395 sym = findsymbolicname(darg) |
34331
e473f482b9b3
templater: use helper function to get name of non-iterable keyword
Yuya Nishihara <yuya@tcha.org>
parents:
34294
diff
changeset
|
396 if sym: |
e473f482b9b3
templater: use helper function to get name of non-iterable keyword
Yuya Nishihara <yuya@tcha.org>
parents:
34294
diff
changeset
|
397 raise error.ParseError(_("keyword '%s' is not iterable") % sym) |
28349
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
398 else: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
399 raise error.ParseError(_("%r is not iterable") % d) |
17631
0b241d7a8c62
templating: make new-style templating features work with command line lists
Matt Mackall <mpm@selenic.com>
parents:
17383
diff
changeset
|
400 |
31807
e6eb86b154c5
templater: provide loop counter as "index" keyword
Yuya Nishihara <yuya@tcha.org>
parents:
31806
diff
changeset
|
401 for i, v in enumerate(diter): |
28225
5c11702fe2a3
templater: fix list templating bug
Kostia Balytskyi <ikostia@fb.com>
parents:
28178
diff
changeset
|
402 lm = mapping.copy() |
31807
e6eb86b154c5
templater: provide loop counter as "index" keyword
Yuya Nishihara <yuya@tcha.org>
parents:
31806
diff
changeset
|
403 lm['index'] = i |
31806
8f203b491bb5
templater: rename variable "i" to "v" in runmap()
Yuya Nishihara <yuya@tcha.org>
parents:
31527
diff
changeset
|
404 if isinstance(v, dict): |
8f203b491bb5
templater: rename variable "i" to "v" in runmap()
Yuya Nishihara <yuya@tcha.org>
parents:
31527
diff
changeset
|
405 lm.update(v) |
17991
d605a82cf189
hgweb: display diff for a changeset against any parents (issue2810)
Weiwen <weiwen@fb.com>
parents:
17982
diff
changeset
|
406 lm['originalnode'] = mapping.get('node') |
34333
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34332
diff
changeset
|
407 yield evalrawexp(context, lm, targ) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
408 else: |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
409 # v is not an iterable of dicts, this happen when 'key' |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
410 # has been fully expanded already and format is useless. |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
411 # If so, return the expanded value. |
31806
8f203b491bb5
templater: rename variable "i" to "v" in runmap()
Yuya Nishihara <yuya@tcha.org>
parents:
31527
diff
changeset
|
412 yield v |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
413 |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
414 def runmember(context, mapping, data): |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
415 darg, memb = data |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
416 d = evalrawexp(context, mapping, darg) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
417 if util.safehasattr(d, 'tomap'): |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
418 lm = mapping.copy() |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
419 lm.update(d.tomap()) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
420 return runsymbol(context, lm, memb) |
34536
4c1cfe54c08d
templater: extend dot operator as a short for get(dict, key)
Yuya Nishihara <yuya@tcha.org>
parents:
34535
diff
changeset
|
421 if util.safehasattr(d, 'get'): |
36919
da2977e674a3
templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36918
diff
changeset
|
422 return getdictitem(d, memb) |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
423 |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
424 sym = findsymbolicname(darg) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
425 if sym: |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
426 raise error.ParseError(_("keyword '%s' has no member") % sym) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
427 else: |
36574
ab7f86a748e6
py3: drop b'' from error message generated by templater.runmember()
Yuya Nishihara <yuya@tcha.org>
parents:
36573
diff
changeset
|
428 raise error.ParseError(_("%r has no member") % pycompat.bytestr(d)) |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
429 |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
430 def runnegate(context, mapping, data): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
431 data = evalinteger(context, mapping, data, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
432 _('negation needs an integer argument')) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
433 return -data |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
434 |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
435 def runarithmetic(context, mapping, data): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
436 func, left, right = data |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
437 left = evalinteger(context, mapping, left, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
438 _('arithmetic only defined on integers')) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
439 right = evalinteger(context, mapping, right, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
440 _('arithmetic only defined on integers')) |
30116
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
441 try: |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
442 return func(left, right) |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
443 except ZeroDivisionError: |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
444 raise error.Abort(_('division by zero is not defined')) |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
445 |
36919
da2977e674a3
templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36918
diff
changeset
|
446 def getdictitem(dictarg, key): |
34534
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34459
diff
changeset
|
447 val = dictarg.get(key) |
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34459
diff
changeset
|
448 if val is None: |
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34459
diff
changeset
|
449 return |
36927
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36926
diff
changeset
|
450 return wraphybridvalue(dictarg, key, val) |