##// END OF EJS Templates
templater: abstract ifcontains() over wrapped types...
Yuya Nishihara -
r38286:fb874fc1 default
parent child Browse files
Show More
@@ -713,6 +713,10 b' class sessionvars(templateutil.wrapped):'
713 713 def __copy__(self):
714 714 return sessionvars(copy.copy(self._vars), self._start)
715 715
716 def contains(self, context, mapping, item):
717 item = templateutil.unwrapvalue(context, mapping, item)
718 return item in self._vars
719
716 720 def getmember(self, context, mapping, key):
717 721 key = templateutil.unwrapvalue(context, mapping, key)
718 722 return self._vars.get(key)
@@ -291,13 +291,10 b' def ifcontains(context, mapping, args):'
291 291 # i18n: "ifcontains" is a keyword
292 292 raise error.ParseError(_("ifcontains expects three or four arguments"))
293 293
294 haystack = evalfuncarg(context, mapping, args[1])
295 keytype = getattr(haystack, 'keytype', None)
294 haystack = evalwrapped(context, mapping, args[1])
296 295 try:
297 296 needle = evalrawexp(context, mapping, args[0])
298 needle = templateutil.unwrapastype(context, mapping, needle,
299 keytype or bytes)
300 found = (needle in haystack)
297 found = haystack.contains(context, mapping, needle)
301 298 except error.ParseError:
302 299 found = False
303 300
@@ -38,6 +38,13 b' class wrapped(object):'
38 38 __metaclass__ = abc.ABCMeta
39 39
40 40 @abc.abstractmethod
41 def contains(self, context, mapping, item):
42 """Test if the specified item is in self
43
44 The item argument may be a wrapped object.
45 """
46
47 @abc.abstractmethod
41 48 def getmember(self, context, mapping, key):
42 49 """Return a member item for the specified key
43 50
@@ -91,6 +98,10 b' class wrappedbytes(wrapped):'
91 98 def __init__(self, value):
92 99 self._value = value
93 100
101 def contains(self, context, mapping, item):
102 item = stringify(context, mapping, item)
103 return item in self._value
104
94 105 def getmember(self, context, mapping, key):
95 106 raise error.ParseError(_('%r is not a dictionary')
96 107 % pycompat.bytestr(self._value))
@@ -125,6 +136,9 b' class wrappedvalue(wrapped):'
125 136 def __init__(self, value):
126 137 self._value = value
127 138
139 def contains(self, context, mapping, item):
140 raise error.ParseError(_("%r is not iterable") % self._value)
141
128 142 def getmember(self, context, mapping, key):
129 143 raise error.ParseError(_('%r is not a dictionary') % self._value)
130 144
@@ -171,6 +185,10 b' class hybrid(wrapped):'
171 185 self._joinfmt = joinfmt
172 186 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved
173 187
188 def contains(self, context, mapping, item):
189 item = unwrapastype(context, mapping, item, self.keytype)
190 return item in self._values
191
174 192 def getmember(self, context, mapping, key):
175 193 # TODO: maybe split hybrid list/dict types?
176 194 if not util.safehasattr(self._values, 'get'):
@@ -255,6 +273,10 b' class mappable(wrapped):'
255 273 def tomap(self):
256 274 return self._makemap(self._key)
257 275
276 def contains(self, context, mapping, item):
277 w = makewrapped(context, mapping, self._value)
278 return w.contains(context, mapping, item)
279
258 280 def getmember(self, context, mapping, key):
259 281 w = makewrapped(context, mapping, self._value)
260 282 return w.getmember(context, mapping, key)
@@ -302,6 +324,9 b' class _mappingsequence(wrapped):'
302 324 self._tmpl = tmpl
303 325 self._defaultsep = sep
304 326
327 def contains(self, context, mapping, item):
328 raise error.ParseError(_('not comparable'))
329
305 330 def getmember(self, context, mapping, key):
306 331 raise error.ParseError(_('not a dictionary'))
307 332
@@ -371,6 +396,10 b' class mappedgenerator(wrapped):'
371 396 self._make = make
372 397 self._args = args
373 398
399 def contains(self, context, mapping, item):
400 item = stringify(context, mapping, item)
401 return item in self.tovalue(context, mapping)
402
374 403 def _gen(self, context):
375 404 return self._make(context, *self._args)
376 405
@@ -4166,6 +4166,15 b' Test ifcontains function'
4166 4166 1
4167 4167 0
4168 4168
4169 $ hg log -l1 -T '{ifcontains("branch", extras, "t", "f")}\n'
4170 t
4171 $ hg log -l1 -T '{ifcontains("branch", extras % "{key}", "t", "f")}\n'
4172 t
4173 $ hg log -l1 -T '{ifcontains("branc", extras % "{key}", "t", "f")}\n'
4174 f
4175 $ hg log -l1 -T '{ifcontains("branc", stringify(extras % "{key}"), "t", "f")}\n'
4176 t
4177
4169 4178 Test revset function
4170 4179
4171 4180 $ hg log --template '{rev} {ifcontains(rev, revset("."), "current rev", "not current rev")}\n'
General Comments 0
You need to be logged in to leave comments. Login now