Mercurial > hg
annotate mercurial/templateutil.py @ 37328:11d51e518808
obsutil: make obsfateprinter() less dependent on templater
joinfmt() is defined as 'lambda x: scmutil.formatchangeid(repo[x])' in
showsuccsandmarkers().
Function arguments are reordered so they look more normal.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 19 Mar 2018 20:32:06 +0900 |
parents | ebf139cbd4a1 |
children | 676664592313 |
rev | line source |
---|---|
36913
da2977e674a3
templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36912
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 |
36913
da2977e674a3
templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36912
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 |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
10 import abc |
17982
e06e9fd2d99f
template engine: convert generator-based iterator to list-based iterator
Weiwen <weiwen@fb.com>
parents:
17890
diff
changeset
|
11 import types |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
12 |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
13 from .i18n import _ |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
14 from . import ( |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
15 error, |
30615
bb77654dc7ae
py3: replace os.sep with pycompat.ossep (part 3 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30332
diff
changeset
|
16 pycompat, |
25985
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
17 util, |
7eb357b5f774
templater: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25815
diff
changeset
|
18 ) |
37084
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37074
diff
changeset
|
19 from .utils import ( |
37224
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
20 dateutil, |
37084
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37074
diff
changeset
|
21 stringutil, |
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37074
diff
changeset
|
22 ) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
23 |
36444
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36443
diff
changeset
|
24 class ResourceUnavailable(error.Abort): |
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36443
diff
changeset
|
25 pass |
717a279c0c21
templater: specialize ResourceUnavailable error so that it can be caught
Yuya Nishihara <yuya@tcha.org>
parents:
36443
diff
changeset
|
26 |
36443
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36245
diff
changeset
|
27 class TemplateNotFound(error.Abort): |
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36245
diff
changeset
|
28 pass |
8dbd97aef915
templater: move specialized exception types to top
Yuya Nishihara <yuya@tcha.org>
parents:
36245
diff
changeset
|
29 |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
30 class wrapped(object): |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
31 """Object requiring extra conversion prior to displaying or processing |
37279
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
32 as value |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
33 |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
34 Use unwrapvalue(), unwrapastype(), or unwraphybrid() to obtain the inner |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
35 object. |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
36 """ |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
37 |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
38 __metaclass__ = abc.ABCMeta |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
39 |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
40 @abc.abstractmethod |
37324
c2f74b8f6b7f
templater: pass context to itermaps() for future extension
Yuya Nishihara <yuya@tcha.org>
parents:
37323
diff
changeset
|
41 def itermaps(self, context): |
37323
8c31b434697f
templater: define interface for objects which act as iterator of mappings
Yuya Nishihara <yuya@tcha.org>
parents:
37279
diff
changeset
|
42 """Yield each template mapping""" |
8c31b434697f
templater: define interface for objects which act as iterator of mappings
Yuya Nishihara <yuya@tcha.org>
parents:
37279
diff
changeset
|
43 |
8c31b434697f
templater: define interface for objects which act as iterator of mappings
Yuya Nishihara <yuya@tcha.org>
parents:
37279
diff
changeset
|
44 @abc.abstractmethod |
37327
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
45 def join(self, context, mapping, sep): |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
46 """Join items with the separator; Returns a bytes or (possibly nested) |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
47 generator of bytes |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
48 |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
49 A pre-configured template may be rendered per item if this container |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
50 holds unprintable items. |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
51 """ |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
52 |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
53 @abc.abstractmethod |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
54 def show(self, context, mapping): |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
55 """Return a bytes or (possibly nested) generator of bytes representing |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
56 the underlying object |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
57 |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
58 A pre-configured template may be rendered if the underlying object is |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
59 not printable. |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
60 """ |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
61 |
37279
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
62 @abc.abstractmethod |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
63 def tovalue(self, context, mapping): |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
64 """Move the inner value object out or create a value representation |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
65 |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
66 A returned value must be serializable by templaterfilters.json(). |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
67 """ |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
68 |
37227
9bcf096a2da2
templatefilters: declare input type as date where appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37226
diff
changeset
|
69 # stub for representing a date type; may be a real date type that can |
9bcf096a2da2
templatefilters: declare input type as date where appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37226
diff
changeset
|
70 # provide a readable string value |
9bcf096a2da2
templatefilters: declare input type as date where appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37226
diff
changeset
|
71 class date(object): |
9bcf096a2da2
templatefilters: declare input type as date where appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37226
diff
changeset
|
72 pass |
9bcf096a2da2
templatefilters: declare input type as date where appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37226
diff
changeset
|
73 |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
74 class hybrid(wrapped): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
75 """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:
36920
diff
changeset
|
76 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
77 This class allows us to handle both: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
78 - "{files}" (legacy command-line-specific list hack) and |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
79 - "{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:
36920
diff
changeset
|
80 and to access raw values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
81 - "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
82 - "{get(extras, key)}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
83 - "{files|json}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
84 """ |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
85 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
86 def __init__(self, gen, values, makemap, joinfmt, keytype=None): |
37325
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
87 self._gen = gen # generator or function returning generator |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
88 self._values = values |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
89 self._makemap = makemap |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
90 self.joinfmt = joinfmt |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
91 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved |
37275
8c84dc8264dc
templater: mark .gen as a private attribute
Yuya Nishihara <yuya@tcha.org>
parents:
37273
diff
changeset
|
92 |
37324
c2f74b8f6b7f
templater: pass context to itermaps() for future extension
Yuya Nishihara <yuya@tcha.org>
parents:
37323
diff
changeset
|
93 def itermaps(self, context): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
94 makemap = self._makemap |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
95 for x in self._values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
96 yield makemap(x) |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
97 |
37327
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
98 def join(self, context, mapping, sep): |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
99 # TODO: switch gen to (context, mapping) API? |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
100 return joinitems((self.joinfmt(x) for x in self._values), sep) |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
101 |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
102 def show(self, context, mapping): |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
103 # TODO: switch gen to (context, mapping) API? |
37275
8c84dc8264dc
templater: mark .gen as a private attribute
Yuya Nishihara <yuya@tcha.org>
parents:
37273
diff
changeset
|
104 gen = self._gen |
37325
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
105 if gen is None: |
37327
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
106 return self.join(context, mapping, ' ') |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
107 if callable(gen): |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
108 return gen() |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
109 return gen |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
110 |
37279
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
111 def tovalue(self, context, mapping): |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
112 # TODO: return self._values and get rid of proxy methods |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
113 return self |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
114 |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
115 def __contains__(self, x): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
116 return x in self._values |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
117 def __getitem__(self, key): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
118 return self._values[key] |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
119 def __len__(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
120 return len(self._values) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
121 def __iter__(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
122 return iter(self._values) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
123 def __getattr__(self, name): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
124 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:
36920
diff
changeset
|
125 r'itervalues', r'keys', r'values'): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
126 raise AttributeError(name) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
127 return getattr(self._values, name) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
128 |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
129 class mappable(wrapped): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
130 """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:
36920
diff
changeset
|
131 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
132 This class allows us to handle both: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
133 - "{manifest}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
134 - "{manifest % '{rev}:{node}'}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
135 - "{manifest.rev}" |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
136 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
137 Unlike a hybrid, this does not simulate the behavior of the underling |
37279
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
138 value. |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
139 """ |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
140 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
141 def __init__(self, gen, key, value, makemap): |
37276
b4630e332a99
templater: drop unneeded generator from mappable object
Yuya Nishihara <yuya@tcha.org>
parents:
37275
diff
changeset
|
142 self._gen = gen # generator or function returning generator |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
143 self._key = key |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
144 self._value = value # may be generator of strings |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
145 self._makemap = makemap |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
146 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
147 def tomap(self): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
148 return self._makemap(self._key) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
149 |
37324
c2f74b8f6b7f
templater: pass context to itermaps() for future extension
Yuya Nishihara <yuya@tcha.org>
parents:
37323
diff
changeset
|
150 def itermaps(self, context): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
151 yield self.tomap() |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
152 |
37327
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
153 def join(self, context, mapping, sep): |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
154 # TODO: just copies the old behavior where a value was a generator |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
155 # yielding one item, but reconsider about it. join() over a string |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
156 # has no consistent result because a string may be a bytes, or a |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
157 # generator yielding an item, or a generator yielding multiple items. |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
158 # Preserving all of the current behaviors wouldn't make any sense. |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
159 return self.show(context, mapping) |
ebf139cbd4a1
templater: abstract away from joinfmt
Yuya Nishihara <yuya@tcha.org>
parents:
37326
diff
changeset
|
160 |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
161 def show(self, context, mapping): |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
162 # TODO: switch gen to (context, mapping) API? |
37275
8c84dc8264dc
templater: mark .gen as a private attribute
Yuya Nishihara <yuya@tcha.org>
parents:
37273
diff
changeset
|
163 gen = self._gen |
37276
b4630e332a99
templater: drop unneeded generator from mappable object
Yuya Nishihara <yuya@tcha.org>
parents:
37275
diff
changeset
|
164 if gen is None: |
b4630e332a99
templater: drop unneeded generator from mappable object
Yuya Nishihara <yuya@tcha.org>
parents:
37275
diff
changeset
|
165 return pycompat.bytestr(self._value) |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
166 if callable(gen): |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
167 return gen() |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
168 return gen |
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
169 |
37279
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
170 def tovalue(self, context, mapping): |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
171 return _unthunk(context, mapping, self._value) |
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
172 |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
173 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:
36920
diff
changeset
|
174 """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:
36920
diff
changeset
|
175 prefmt = pycompat.identity |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
176 if fmt is None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
177 fmt = '%s=%s' |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
178 prefmt = pycompat.bytestr |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
179 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:
36920
diff
changeset
|
180 lambda k: fmt % (prefmt(k), prefmt(data[k]))) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
181 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
182 def hybridlist(data, name, fmt=None, gen=None): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
183 """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:
36920
diff
changeset
|
184 prefmt = pycompat.identity |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
185 if fmt is None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
186 fmt = '%s' |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
187 prefmt = pycompat.bytestr |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
188 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:
36920
diff
changeset
|
189 |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
190 def unwraphybrid(context, mapping, thing): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
191 """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:
36920
diff
changeset
|
192 template""" |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
193 if not isinstance(thing, wrapped): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
194 return thing |
37273
83e1bbd48991
templater: define interface for objects requiring unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37272
diff
changeset
|
195 return thing.show(context, mapping) |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
196 |
37277
9e8128e84326
templater: pass (context, mapping) down to unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37276
diff
changeset
|
197 def unwrapvalue(context, mapping, thing): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
198 """Move the inner value object out of the wrapper""" |
37279
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
199 if not isinstance(thing, wrapped): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
200 return thing |
37279
26f6fc179e62
templater: define interface for objects requiring unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37278
diff
changeset
|
201 return thing.tovalue(context, mapping) |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
202 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
203 def wraphybridvalue(container, key, value): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
204 """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:
36920
diff
changeset
|
205 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
206 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:
36920
diff
changeset
|
207 should be an item generated by iter(container). |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
208 """ |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
209 makemap = getattr(container, '_makemap', None) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
210 if makemap is None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
211 return value |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
212 if util.safehasattr(value, '_makemap'): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
213 # 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:
36920
diff
changeset
|
214 return value |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
215 return mappable(None, key, value, makemap) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
216 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
217 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:
36920
diff
changeset
|
218 fmt=None, plural=None, separator=' '): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
219 """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:
36920
diff
changeset
|
220 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
221 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:
36920
diff
changeset
|
222 hybriddict() for new template keywords. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
223 """ |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
224 c = [{key: k, value: v} for k, v in data.iteritems()] |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
225 f = _showcompatlist(context, mapping, name, c, plural, separator) |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
226 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:
36920
diff
changeset
|
227 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
228 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:
36920
diff
changeset
|
229 plural=None, separator=' '): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
230 """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:
36920
diff
changeset
|
231 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
232 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:
36920
diff
changeset
|
233 hybridlist() for new template keywords. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
234 """ |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
235 f = _showcompatlist(context, mapping, name, data, plural, separator) |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
236 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:
36920
diff
changeset
|
237 |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
238 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:
37019
diff
changeset
|
239 """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:
37019
diff
changeset
|
240 |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
241 name is name of key in template map. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
242 values is list of strings or dicts. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
243 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:
36920
diff
changeset
|
244 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:
36920
diff
changeset
|
245 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
246 expansion works like this, given name 'foo'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
247 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
248 if values is empty, expand 'no_foos'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
249 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
250 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:
36920
diff
changeset
|
251 joined by 'separator'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
252 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
253 expand 'start_foos'. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
254 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
255 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:
36920
diff
changeset
|
256 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:
36920
diff
changeset
|
257 |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
258 expand 'end_foos'. |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
259 """ |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
260 if not plural: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
261 plural = name + 's' |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
262 if not values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
263 noname = 'no_' + plural |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
264 if context.preload(noname): |
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
265 yield context.process(noname, mapping) |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
266 return |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
267 if not context.preload(name): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
268 if isinstance(values[0], bytes): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
269 yield separator.join(values) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
270 else: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
271 for v in values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
272 r = dict(v) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
273 r.update(mapping) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
274 yield r |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
275 return |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
276 startname = 'start_' + plural |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
277 if context.preload(startname): |
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
278 yield context.process(startname, mapping) |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
279 def one(v, tag=name): |
37074
2891079fb0c0
templater: factor out function to create mapping dict for nested evaluation
Yuya Nishihara <yuya@tcha.org>
parents:
37073
diff
changeset
|
280 vmapping = {} |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
281 try: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
282 vmapping.update(v) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
283 # 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:
36920
diff
changeset
|
284 # 3 raises TypeError. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
285 except (AttributeError, TypeError, ValueError): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
286 try: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
287 # 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:
36920
diff
changeset
|
288 # bytes. Python 3 raises TypeError. |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
289 for a, b in v: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
290 vmapping[a] = b |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
291 except (TypeError, ValueError): |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
292 vmapping[name] = v |
37074
2891079fb0c0
templater: factor out function to create mapping dict for nested evaluation
Yuya Nishihara <yuya@tcha.org>
parents:
37073
diff
changeset
|
293 vmapping = context.overlaymap(mapping, vmapping) |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
294 return context.process(tag, vmapping) |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
295 lastname = 'last_' + name |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
296 if context.preload(lastname): |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
297 last = values.pop() |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
298 else: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
299 last = None |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
300 for v in values: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
301 yield one(v) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
302 if last is not None: |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
303 yield one(last, tag=lastname) |
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
304 endname = 'end_' + plural |
37068
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
305 if context.preload(endname): |
aa97e06a1912
templater: use template context to render old-style list template
Yuya Nishihara <yuya@tcha.org>
parents:
37019
diff
changeset
|
306 yield context.process(endname, mapping) |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
307 |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
308 def flatten(context, mapping, thing): |
37157
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
309 """Yield a single stream from a possibly nested set of iterators""" |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
310 thing = unwraphybrid(context, mapping, thing) |
37157
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
311 if isinstance(thing, bytes): |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
312 yield thing |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
313 elif isinstance(thing, str): |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
314 # We can only hit this on Python 3, and it's here to guard |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
315 # against infinite recursion. |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
316 raise error.ProgrammingError('Mercurial IO including templates is done' |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
317 ' with bytes, not strings, got %r' % thing) |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
318 elif thing is None: |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
319 pass |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
320 elif not util.safehasattr(thing, '__iter__'): |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
321 yield pycompat.bytestr(thing) |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
322 else: |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
323 for i in thing: |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
324 i = unwraphybrid(context, mapping, i) |
37157
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
325 if isinstance(i, bytes): |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
326 yield i |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
327 elif i is None: |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
328 pass |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
329 elif not util.safehasattr(i, '__iter__'): |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
330 yield pycompat.bytestr(i) |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
331 else: |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
332 for j in flatten(context, mapping, i): |
37157
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
333 yield j |
888507ec655e
templateutil: move flatten() from templater
Yuya Nishihara <yuya@tcha.org>
parents:
37104
diff
changeset
|
334 |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
335 def stringify(context, mapping, thing): |
36920
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36913
diff
changeset
|
336 """Turn values into bytes by converting into text and concatenating them""" |
37158
e09d2183e226
templateutil: reimplement stringify() using flatten()
Yuya Nishihara <yuya@tcha.org>
parents:
37157
diff
changeset
|
337 if isinstance(thing, bytes): |
e09d2183e226
templateutil: reimplement stringify() using flatten()
Yuya Nishihara <yuya@tcha.org>
parents:
37157
diff
changeset
|
338 return thing # retain localstr to be round-tripped |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
339 return b''.join(flatten(context, mapping, thing)) |
36920
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36913
diff
changeset
|
340 |
31927
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
341 def findsymbolicname(arg): |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
342 """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
|
343 if nothing found reliably""" |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
344 while True: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
345 func, data = arg |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
346 if func is runsymbol: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
347 return data |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
348 elif func is runfilter: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
349 arg = data[0] |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
350 else: |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
351 return None |
2abc556dbe92
templater: find keyword name more thoroughly on filtering error
Yuya Nishihara <yuya@tcha.org>
parents:
31926
diff
changeset
|
352 |
37278
671a01cd50b5
templater: extract private function to evaluate generator to byte string
Yuya Nishihara <yuya@tcha.org>
parents:
37277
diff
changeset
|
353 def _unthunk(context, mapping, thing): |
671a01cd50b5
templater: extract private function to evaluate generator to byte string
Yuya Nishihara <yuya@tcha.org>
parents:
37277
diff
changeset
|
354 """Evaluate a lazy byte string into value""" |
671a01cd50b5
templater: extract private function to evaluate generator to byte string
Yuya Nishihara <yuya@tcha.org>
parents:
37277
diff
changeset
|
355 if not isinstance(thing, types.GeneratorType): |
671a01cd50b5
templater: extract private function to evaluate generator to byte string
Yuya Nishihara <yuya@tcha.org>
parents:
37277
diff
changeset
|
356 return thing |
671a01cd50b5
templater: extract private function to evaluate generator to byte string
Yuya Nishihara <yuya@tcha.org>
parents:
37277
diff
changeset
|
357 return stringify(context, mapping, thing) |
671a01cd50b5
templater: extract private function to evaluate generator to byte string
Yuya Nishihara <yuya@tcha.org>
parents:
37277
diff
changeset
|
358 |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
359 def evalrawexp(context, mapping, arg): |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
360 """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:
34325
diff
changeset
|
361 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
|
362 func, data = arg |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
363 return func(context, mapping, data) |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
364 |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
365 def evalfuncarg(context, mapping, arg): |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
366 """Evaluate given argument as value type""" |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
367 return _unwrapvalue(context, mapping, evalrawexp(context, mapping, arg)) |
37161
0023da2910c9
templater: extract type conversion from evalfuncarg()
Yuya Nishihara <yuya@tcha.org>
parents:
37160
diff
changeset
|
368 |
0023da2910c9
templater: extract type conversion from evalfuncarg()
Yuya Nishihara <yuya@tcha.org>
parents:
37160
diff
changeset
|
369 # TODO: unify this with unwrapvalue() once the bug of templatefunc.join() |
0023da2910c9
templater: extract type conversion from evalfuncarg()
Yuya Nishihara <yuya@tcha.org>
parents:
37160
diff
changeset
|
370 # is fixed. we can't do that right now because join() has to take a generator |
0023da2910c9
templater: extract type conversion from evalfuncarg()
Yuya Nishihara <yuya@tcha.org>
parents:
37160
diff
changeset
|
371 # of byte strings as it is, not a lazy byte string. |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
372 def _unwrapvalue(context, mapping, thing): |
37277
9e8128e84326
templater: pass (context, mapping) down to unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37276
diff
changeset
|
373 thing = unwrapvalue(context, mapping, thing) |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
374 # 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:
34325
diff
changeset
|
375 # such as date tuple, but filter does not want generator. |
37278
671a01cd50b5
templater: extract private function to evaluate generator to byte string
Yuya Nishihara <yuya@tcha.org>
parents:
37277
diff
changeset
|
376 return _unthunk(context, mapping, thing) |
26124
604a7c941103
templater: extract helper that evaluates filter or function argument
Yuya Nishihara <yuya@tcha.org>
parents:
26106
diff
changeset
|
377 |
29816
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
378 def evalboolean(context, mapping, arg): |
29817
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
379 """Evaluate given argument as boolean, but also takes boolean literals""" |
29816
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
380 func, data = arg |
29817
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
381 if func is runsymbol: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
382 thing = func(context, mapping, data, default=None) |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
383 if thing is None: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
384 # not a template keyword, takes as a boolean literal |
37084
f0b6fbea00cf
stringutil: bulk-replace call sites to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
37074
diff
changeset
|
385 thing = stringutil.parsebool(data) |
29817
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
386 else: |
cc11079644fc
templater: make pad() evaluate boolean argument (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
29816
diff
changeset
|
387 thing = func(context, mapping, data) |
37277
9e8128e84326
templater: pass (context, mapping) down to unwrapvalue()
Yuya Nishihara <yuya@tcha.org>
parents:
37276
diff
changeset
|
388 thing = unwrapvalue(context, mapping, thing) |
29816
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
389 if isinstance(thing, bool): |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
390 return thing |
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
391 # 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:
29815
diff
changeset
|
392 # empty dict/list should be False as they are expected to be '' |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
393 return bool(stringify(context, mapping, thing)) |
29816
034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Yuya Nishihara <yuya@tcha.org>
parents:
29815
diff
changeset
|
394 |
37224
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
395 def evaldate(context, mapping, arg, err=None): |
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
396 """Evaluate given argument as a date tuple or a date string; returns |
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
397 a (unixtime, offset) tuple""" |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
398 thing = evalrawexp(context, mapping, arg) |
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
399 return unwrapdate(context, mapping, thing, err) |
37224
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
400 |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
401 def unwrapdate(context, mapping, thing, err=None): |
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
402 thing = _unwrapvalue(context, mapping, thing) |
37224
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
403 try: |
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
404 return dateutil.parsedate(thing) |
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
405 except AttributeError: |
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
406 raise error.ParseError(err or _('not a date tuple nor a string')) |
37225
e70a90a72b80
templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37224
diff
changeset
|
407 except error.ParseError: |
e70a90a72b80
templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37224
diff
changeset
|
408 if not err: |
e70a90a72b80
templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37224
diff
changeset
|
409 raise |
e70a90a72b80
templatefuncs: use evaldate() where seems appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37224
diff
changeset
|
410 raise error.ParseError(err) |
37224
67efce231633
templater: factor out function that parses argument as date tuple
Yuya Nishihara <yuya@tcha.org>
parents:
37222
diff
changeset
|
411 |
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
|
412 def evalinteger(context, mapping, arg, err=None): |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
413 thing = evalrawexp(context, mapping, arg) |
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
414 return unwrapinteger(context, mapping, thing, err) |
37162
9ab3491f84c2
templater: extract unwrapinteger() function from evalinteger()
Yuya Nishihara <yuya@tcha.org>
parents:
37161
diff
changeset
|
415 |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
416 def unwrapinteger(context, mapping, thing, err=None): |
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
417 thing = _unwrapvalue(context, mapping, thing) |
28343
a6c2310b3827
templater: factor out function that evaluates argument as integer
Yuya Nishihara <yuya@tcha.org>
parents:
28334
diff
changeset
|
418 try: |
37162
9ab3491f84c2
templater: extract unwrapinteger() function from evalinteger()
Yuya Nishihara <yuya@tcha.org>
parents:
37161
diff
changeset
|
419 return int(thing) |
28344
ac371d4c007f
templater: drop redundant type conversion when evaluating integer argument
Yuya Nishihara <yuya@tcha.org>
parents:
28343
diff
changeset
|
420 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
|
421 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
|
422 |
28348
ccedb17a5657
templater: factor out thin helper that evaluates argument as string
Yuya Nishihara <yuya@tcha.org>
parents:
28346
diff
changeset
|
423 def evalstring(context, mapping, arg): |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
424 return stringify(context, mapping, 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
|
425 |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
426 def evalstringliteral(context, mapping, arg): |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
427 """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
|
428 if it is unknown""" |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
429 func, data = arg |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
430 if func is runsymbol: |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
431 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
|
432 else: |
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
433 thing = func(context, mapping, data) |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
434 return stringify(context, mapping, thing) |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
435 |
37163
0fb28899e81a
templater: factor out unwrapastype() from evalastype()
Yuya Nishihara <yuya@tcha.org>
parents:
37162
diff
changeset
|
436 _unwrapfuncbytype = { |
37222
54355c243042
templatefilters: allow declaration of input data type
Yuya Nishihara <yuya@tcha.org>
parents:
37163
diff
changeset
|
437 None: _unwrapvalue, |
37163
0fb28899e81a
templater: factor out unwrapastype() from evalastype()
Yuya Nishihara <yuya@tcha.org>
parents:
37162
diff
changeset
|
438 bytes: stringify, |
37227
9bcf096a2da2
templatefilters: declare input type as date where appropriate
Yuya Nishihara <yuya@tcha.org>
parents:
37226
diff
changeset
|
439 date: unwrapdate, |
37163
0fb28899e81a
templater: factor out unwrapastype() from evalastype()
Yuya Nishihara <yuya@tcha.org>
parents:
37162
diff
changeset
|
440 int: unwrapinteger, |
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
|
441 } |
ee0d74083a22
templater: store revisions as ints so min/max won't compare them as strings
Yuya Nishihara <yuya@tcha.org>
parents:
34536
diff
changeset
|
442 |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
443 def unwrapastype(context, mapping, thing, typ): |
37163
0fb28899e81a
templater: factor out unwrapastype() from evalastype()
Yuya Nishihara <yuya@tcha.org>
parents:
37162
diff
changeset
|
444 """Move the inner value object out of the wrapper and coerce its type""" |
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
|
445 try: |
37163
0fb28899e81a
templater: factor out unwrapastype() from evalastype()
Yuya Nishihara <yuya@tcha.org>
parents:
37162
diff
changeset
|
446 f = _unwrapfuncbytype[typ] |
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
|
447 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
|
448 raise error.ProgrammingError('invalid type specified: %r' % typ) |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
449 return f(context, mapping, thing) |
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
|
450 |
25002
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
451 def runinteger(context, mapping, data): |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
452 return int(data) |
829faf8ab605
templater: tokenize decimal integer literal (issue4638) (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
25001
diff
changeset
|
453 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
454 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
|
455 return data |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
456 |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
457 def _recursivesymbolblocker(key): |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
458 def showrecursion(**args): |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
459 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
|
460 return showrecursion |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
461 |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
462 def runsymbol(context, mapping, key, default=''): |
35467
d6cfa722b044
templater: look up mapping table through template engine
Yuya Nishihara <yuya@tcha.org>
parents:
35414
diff
changeset
|
463 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
|
464 if v is None: |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
465 # 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
|
466 # 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
|
467 safemapping = mapping.copy() |
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
468 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
|
469 try: |
27939
7ed3a3c0cef1
templater: abort if infinite recursion detected while evaluation (issue4758)
Yuya Nishihara <yuya@tcha.org>
parents:
27892
diff
changeset
|
470 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
|
471 except TemplateNotFound: |
28373
9a9dd71e882c
templater: make label() take unknown symbol as color literal
Yuya Nishihara <yuya@tcha.org>
parents:
28349
diff
changeset
|
472 v = default |
36445
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
473 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:
36444
diff
changeset
|
474 # old templatekw: expand all keywords and resources |
37070
1101d6747d2d
templater: drop 'templ' from resources dict
Yuya Nishihara <yuya@tcha.org>
parents:
37068
diff
changeset
|
475 # (TODO: deprecate this after porting web template keywords to new API) |
37073
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
37070
diff
changeset
|
476 props = {k: context._resources.lookup(context, mapping, k) |
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
37070
diff
changeset
|
477 for k in context._resources.knownkeys()} |
37070
1101d6747d2d
templater: drop 'templ' from resources dict
Yuya Nishihara <yuya@tcha.org>
parents:
37068
diff
changeset
|
478 # pass context to _showcompatlist() through templatekw._showlist() |
1101d6747d2d
templater: drop 'templ' from resources dict
Yuya Nishihara <yuya@tcha.org>
parents:
37068
diff
changeset
|
479 props['templ'] = context |
35468
32c278eb876f
templater: keep default resources per template engine (API)
Yuya Nishihara <yuya@tcha.org>
parents:
35467
diff
changeset
|
480 props.update(mapping) |
35588
dadbf213a765
py3: convert dict keys' to str before passing as kwargs
Pulkit Goyal <7895pulkit@gmail.com>
parents:
35481
diff
changeset
|
481 return v(**pycompat.strkwargs(props)) |
36445
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
482 if callable(v): |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
483 # new templatekw |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
484 try: |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
485 return v(context, mapping) |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
486 except ResourceUnavailable: |
e8d37838f5df
templatekw: add 'requires' flag to switch to exception-safe interface
Yuya Nishihara <yuya@tcha.org>
parents:
36444
diff
changeset
|
487 # 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:
36444
diff
changeset
|
488 return None |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
489 return v |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
490 |
25595
a7dd6692e5cb
templater: move runtemplate function out of buildmap/runmap pair
Yuya Nishihara <yuya@tcha.org>
parents:
25580
diff
changeset
|
491 def runtemplate(context, mapping, template): |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
492 for arg in template: |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
493 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
|
494 |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
495 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
|
496 arg, filt = data |
37222
54355c243042
templatefilters: allow declaration of input data type
Yuya Nishihara <yuya@tcha.org>
parents:
37163
diff
changeset
|
497 thing = evalrawexp(context, mapping, arg) |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
498 intype = getattr(filt, '_intype', None) |
17383
099c778ceb33
templater: abort when a template filter raises an exception (issue2987)
Neil Kodner <neilk@fb.com>
parents:
17334
diff
changeset
|
499 try: |
37272
7d3bc1d4e871
templater: pass (context, mapping) down to unwraphybrid()
Yuya Nishihara <yuya@tcha.org>
parents:
37231
diff
changeset
|
500 thing = unwrapastype(context, mapping, thing, intype) |
24280
6c55e37ba5f2
templater: allow piping generator-type function output to filters
Yuya Nishihara <yuya@tcha.org>
parents:
24240
diff
changeset
|
501 return filt(thing) |
37226
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
502 except error.ParseError as e: |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
503 raise error.ParseError(bytes(e), hint=_formatfiltererror(arg, filt)) |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
504 |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
505 def _formatfiltererror(arg, filt): |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
506 fn = pycompat.sysbytes(filt.__name__) |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
507 sym = findsymbolicname(arg) |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
508 if not sym: |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
509 return _("incompatible use of template filter '%s'") % fn |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
510 return (_("template filter '%s' is not compatible with keyword '%s'") |
920589f52be9
templater: attach hint to input-type error of runfilter()
Yuya Nishihara <yuya@tcha.org>
parents:
37225
diff
changeset
|
511 % (fn, sym)) |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
512 |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
513 def runmap(context, mapping, data): |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
514 darg, targ = data |
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
515 d = evalrawexp(context, mapping, darg) |
37323
8c31b434697f
templater: define interface for objects which act as iterator of mappings
Yuya Nishihara <yuya@tcha.org>
parents:
37279
diff
changeset
|
516 if isinstance(d, wrapped): |
37324
c2f74b8f6b7f
templater: pass context to itermaps() for future extension
Yuya Nishihara <yuya@tcha.org>
parents:
37323
diff
changeset
|
517 diter = d.itermaps(context) |
28349
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
518 else: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
519 try: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
520 diter = iter(d) |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
521 except TypeError: |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
522 sym = findsymbolicname(darg) |
34324
e473f482b9b3
templater: use helper function to get name of non-iterable keyword
Yuya Nishihara <yuya@tcha.org>
parents:
34287
diff
changeset
|
523 if sym: |
e473f482b9b3
templater: use helper function to get name of non-iterable keyword
Yuya Nishihara <yuya@tcha.org>
parents:
34287
diff
changeset
|
524 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
|
525 else: |
7cb2f2438f85
templater: handle exception when applying map operator to non-iterable object
Yuya Nishihara <yuya@tcha.org>
parents:
28348
diff
changeset
|
526 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
|
527 |
31807
e6eb86b154c5
templater: provide loop counter as "index" keyword
Yuya Nishihara <yuya@tcha.org>
parents:
31806
diff
changeset
|
528 for i, v in enumerate(diter): |
31806
8f203b491bb5
templater: rename variable "i" to "v" in runmap()
Yuya Nishihara <yuya@tcha.org>
parents:
31521
diff
changeset
|
529 if isinstance(v, dict): |
37074
2891079fb0c0
templater: factor out function to create mapping dict for nested evaluation
Yuya Nishihara <yuya@tcha.org>
parents:
37073
diff
changeset
|
530 lm = context.overlaymap(mapping, v) |
2891079fb0c0
templater: factor out function to create mapping dict for nested evaluation
Yuya Nishihara <yuya@tcha.org>
parents:
37073
diff
changeset
|
531 lm['index'] = i |
34326
e60c601953d7
templater: extract helper to just evaluate template expression
Yuya Nishihara <yuya@tcha.org>
parents:
34325
diff
changeset
|
532 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
|
533 else: |
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
534 # 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
|
535 # 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
|
536 # If so, return the expanded value. |
31806
8f203b491bb5
templater: rename variable "i" to "v" in runmap()
Yuya Nishihara <yuya@tcha.org>
parents:
31521
diff
changeset
|
537 yield v |
13176
895f54a79c6e
templater: use the parser.py parser to extend the templater syntax
Matt Mackall <mpm@selenic.com>
parents:
13175
diff
changeset
|
538 |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
539 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
|
540 darg, memb = data |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
541 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
|
542 if util.safehasattr(d, 'tomap'): |
37074
2891079fb0c0
templater: factor out function to create mapping dict for nested evaluation
Yuya Nishihara <yuya@tcha.org>
parents:
37073
diff
changeset
|
543 lm = context.overlaymap(mapping, d.tomap()) |
34535
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
544 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
|
545 if util.safehasattr(d, 'get'): |
36913
da2977e674a3
templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36912
diff
changeset
|
546 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
|
547 |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
548 sym = findsymbolicname(darg) |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
549 if sym: |
78590585c0db
templater: add dot operator to easily access a sub item
Yuya Nishihara <yuya@tcha.org>
parents:
34534
diff
changeset
|
550 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
|
551 else: |
36545
ab7f86a748e6
py3: drop b'' from error message generated by templater.runmember()
Yuya Nishihara <yuya@tcha.org>
parents:
36544
diff
changeset
|
552 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
|
553 |
30115
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
554 def runnegate(context, mapping, data): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
555 data = evalinteger(context, mapping, data, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
556 _('negation needs an integer argument')) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
557 return -data |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
558 |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
559 def runarithmetic(context, mapping, data): |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
560 func, left, right = data |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
561 left = evalinteger(context, mapping, left, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
562 _('arithmetic only defined on integers')) |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
563 right = evalinteger(context, mapping, right, |
8e42dfde93d1
templater: provide arithmetic operations on integers
Simon Farnsworth <simonfar@fb.com>
parents:
30083
diff
changeset
|
564 _('arithmetic only defined on integers')) |
30116
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
565 try: |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
566 return func(left, right) |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
567 except ZeroDivisionError: |
1c01fa29630f
templater: handle division by zero in arithmetic
Simon Farnsworth <simonfar@fb.com>
parents:
30115
diff
changeset
|
568 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
|
569 |
36913
da2977e674a3
templater: extract template evaluation utility to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36912
diff
changeset
|
570 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:
34458
diff
changeset
|
571 val = dictarg.get(key) |
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34458
diff
changeset
|
572 if val is None: |
b3073e175c17
templater: wrap get/min/max result so map operation can apply to element
Yuya Nishihara <yuya@tcha.org>
parents:
34458
diff
changeset
|
573 return |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
574 return wraphybridvalue(dictarg, key, val) |
37325
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
575 |
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
576 def joinitems(itemiter, sep): |
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
577 """Join items with the separator; Returns generator of bytes""" |
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
578 first = True |
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
579 for x in itemiter: |
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
580 if first: |
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
581 first = False |
37326
9cd88dd3bf64
templater: micro-optimize join() with empty separator
Yuya Nishihara <yuya@tcha.org>
parents:
37325
diff
changeset
|
582 elif sep: |
37325
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
583 yield sep |
41a5d815d2c1
templater: factor out generator of join()-ed items
Yuya Nishihara <yuya@tcha.org>
parents:
37324
diff
changeset
|
584 yield x |