##// 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 def __copy__(self):
713 def __copy__(self):
714 return sessionvars(copy.copy(self._vars), self._start)
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 def getmember(self, context, mapping, key):
720 def getmember(self, context, mapping, key):
717 key = templateutil.unwrapvalue(context, mapping, key)
721 key = templateutil.unwrapvalue(context, mapping, key)
718 return self._vars.get(key)
722 return self._vars.get(key)
@@ -291,13 +291,10 b' def ifcontains(context, mapping, args):'
291 # i18n: "ifcontains" is a keyword
291 # i18n: "ifcontains" is a keyword
292 raise error.ParseError(_("ifcontains expects three or four arguments"))
292 raise error.ParseError(_("ifcontains expects three or four arguments"))
293
293
294 haystack = evalfuncarg(context, mapping, args[1])
294 haystack = evalwrapped(context, mapping, args[1])
295 keytype = getattr(haystack, 'keytype', None)
296 try:
295 try:
297 needle = evalrawexp(context, mapping, args[0])
296 needle = evalrawexp(context, mapping, args[0])
298 needle = templateutil.unwrapastype(context, mapping, needle,
297 found = haystack.contains(context, mapping, needle)
299 keytype or bytes)
300 found = (needle in haystack)
301 except error.ParseError:
298 except error.ParseError:
302 found = False
299 found = False
303
300
@@ -38,6 +38,13 b' class wrapped(object):'
38 __metaclass__ = abc.ABCMeta
38 __metaclass__ = abc.ABCMeta
39
39
40 @abc.abstractmethod
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 def getmember(self, context, mapping, key):
48 def getmember(self, context, mapping, key):
42 """Return a member item for the specified key
49 """Return a member item for the specified key
43
50
@@ -91,6 +98,10 b' class wrappedbytes(wrapped):'
91 def __init__(self, value):
98 def __init__(self, value):
92 self._value = value
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 def getmember(self, context, mapping, key):
105 def getmember(self, context, mapping, key):
95 raise error.ParseError(_('%r is not a dictionary')
106 raise error.ParseError(_('%r is not a dictionary')
96 % pycompat.bytestr(self._value))
107 % pycompat.bytestr(self._value))
@@ -125,6 +136,9 b' class wrappedvalue(wrapped):'
125 def __init__(self, value):
136 def __init__(self, value):
126 self._value = value
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 def getmember(self, context, mapping, key):
142 def getmember(self, context, mapping, key):
129 raise error.ParseError(_('%r is not a dictionary') % self._value)
143 raise error.ParseError(_('%r is not a dictionary') % self._value)
130
144
@@ -171,6 +185,10 b' class hybrid(wrapped):'
171 self._joinfmt = joinfmt
185 self._joinfmt = joinfmt
172 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved
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 def getmember(self, context, mapping, key):
192 def getmember(self, context, mapping, key):
175 # TODO: maybe split hybrid list/dict types?
193 # TODO: maybe split hybrid list/dict types?
176 if not util.safehasattr(self._values, 'get'):
194 if not util.safehasattr(self._values, 'get'):
@@ -255,6 +273,10 b' class mappable(wrapped):'
255 def tomap(self):
273 def tomap(self):
256 return self._makemap(self._key)
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 def getmember(self, context, mapping, key):
280 def getmember(self, context, mapping, key):
259 w = makewrapped(context, mapping, self._value)
281 w = makewrapped(context, mapping, self._value)
260 return w.getmember(context, mapping, key)
282 return w.getmember(context, mapping, key)
@@ -302,6 +324,9 b' class _mappingsequence(wrapped):'
302 self._tmpl = tmpl
324 self._tmpl = tmpl
303 self._defaultsep = sep
325 self._defaultsep = sep
304
326
327 def contains(self, context, mapping, item):
328 raise error.ParseError(_('not comparable'))
329
305 def getmember(self, context, mapping, key):
330 def getmember(self, context, mapping, key):
306 raise error.ParseError(_('not a dictionary'))
331 raise error.ParseError(_('not a dictionary'))
307
332
@@ -371,6 +396,10 b' class mappedgenerator(wrapped):'
371 self._make = make
396 self._make = make
372 self._args = args
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 def _gen(self, context):
403 def _gen(self, context):
375 return self._make(context, *self._args)
404 return self._make(context, *self._args)
376
405
@@ -4166,6 +4166,15 b' Test ifcontains function'
4166 1
4166 1
4167 0
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 Test revset function
4178 Test revset function
4170
4179
4171 $ hg log --template '{rev} {ifcontains(rev, revset("."), "current rev", "not current rev")}\n'
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