##// END OF EJS Templates
hg-cpython: use ancestor iterator impls from vcsgraph...
hg-cpython: use ancestor iterator impls from vcsgraph Differential Revision: https://phab.mercurial-scm.org/D11947

File last commit:

r47575:d4ba4d51 default
r49350:35ebe6f8 default
Show More
templateutil.py
1170 lines | 35.7 KiB | text/x-python | PythonLexer
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 # templateutil.py - utility for template evaluation
#
Raphaël Gomès
contributor: change mentions of mpm to olivia...
r47575 # Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 #
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291 import abc
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 import types
from .i18n import _
Gregory Szorc
py3: manually import getattr where it is needed...
r43359 from .pycompat import getattr
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 from . import (
error,
pycompat,
Yuya Nishihara
templater: introduce wrapper for smartset (API)...
r45080 smartset,
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 util,
)
Yuya Nishihara
stringutil: bulk-replace call sites to point to new module...
r37102 from .utils import (
Yuya Nishihara
templater: factor out function that parses argument as date tuple
r37241 dateutil,
Yuya Nishihara
stringutil: bulk-replace call sites to point to new module...
r37102 stringutil,
)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 class ResourceUnavailable(error.Abort):
pass
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 class TemplateNotFound(error.Abort):
pass
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
pytype: don't warn us about ignored-on-py3 metaclasses...
r43775 class wrapped(object): # pytype: disable=ignored-metaclass
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291 """Object requiring extra conversion prior to displaying or processing
Yuya Nishihara
templater: define interface for objects requiring unwrapvalue()...
r37297 as value
Yuya Nishihara
templater: inline unwraphybrid()...
r38289 Use unwrapvalue() or unwrapastype() to obtain the inner object.
Yuya Nishihara
templater: define interface for objects requiring unwrapvalue()...
r37297 """
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286 def contains(self, context, mapping, item):
"""Test if the specified item is in self
The item argument may be a wrapped object.
"""
@abc.abstractmethod
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 def getmember(self, context, mapping, key):
"""Return a member item for the specified key
Yuya Nishihara
templater: resolve type of dict key in getmember()...
r38262 The key argument may be a wrapped object.
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 A returned object may be either a wrapped object or a pure value
depending on the self type.
"""
@abc.abstractmethod
Yuya Nishihara
templater: abstract min/max away...
r38284 def getmin(self, context, mapping):
"""Return the smallest item, which may be either a wrapped or a pure
value depending on the self type"""
@abc.abstractmethod
def getmax(self, context, mapping):
"""Return the largest item, which may be either a wrapped or a pure
value depending on the self type"""
@abc.abstractmethod
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 def filter(self, context, mapping, select):
"""Return new container of the same type which includes only the
selected elements
select() takes each item as a wrapped object and returns True/False.
"""
@abc.abstractmethod
Yuya Nishihara
templater: pass context to itermaps() for future extension...
r37340 def itermaps(self, context):
Yuya Nishihara
templater: define interface for objects which act as iterator of mappings
r37339 """Yield each template mapping"""
@abc.abstractmethod
Yuya Nishihara
templater: abstract away from joinfmt...
r37343 def join(self, context, mapping, sep):
"""Join items with the separator; Returns a bytes or (possibly nested)
generator of bytes
A pre-configured template may be rendered per item if this container
holds unprintable items.
"""
@abc.abstractmethod
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291 def show(self, context, mapping):
"""Return a bytes or (possibly nested) generator of bytes representing
the underlying object
A pre-configured template may be rendered if the underlying object is
not printable.
"""
Yuya Nishihara
templater: define interface for objects requiring unwrapvalue()...
r37297 @abc.abstractmethod
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
"""Return a boolean representation of the inner value"""
@abc.abstractmethod
Yuya Nishihara
templater: define interface for objects requiring unwrapvalue()...
r37297 def tovalue(self, context, mapping):
"""Move the inner value object out or create a value representation
A returned value must be serializable by templaterfilters.json().
"""
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
pytype: don't warn us about ignored-on-py3 metaclasses...
r43775 class mappable(object): # pytype: disable=ignored-metaclass
Yuya Nishihara
templater: promote tomap() to an interface type...
r38303 """Object which can be converted to a single template mapping"""
Augie Fackler
templateutil: fix a missing ABCMeta assignment...
r43765 __metaclass__ = abc.ABCMeta
Yuya Nishihara
templater: promote tomap() to an interface type...
r38303 def itermaps(self, context):
yield self.tomap(context)
@abc.abstractmethod
def tomap(self, context):
"""Create a single template mapping representing this"""
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 class wrappedbytes(wrapped):
"""Wrapper for byte string"""
def __init__(self, value):
self._value = value
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286 def contains(self, context, mapping, item):
item = stringify(context, mapping, item)
return item in self._value
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 def getmember(self, context, mapping, key):
Augie Fackler
formatting: blacken the codebase...
r43346 raise error.ParseError(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'%r is not a dictionary') % pycompat.bytestr(self._value)
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261
Yuya Nishihara
templater: abstract min/max away...
r38284 def getmin(self, context, mapping):
return self._getby(context, mapping, min)
def getmax(self, context, mapping):
return self._getby(context, mapping, max)
def _getby(self, context, mapping, func):
if not self._value:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'empty string'))
Yuya Nishihara
templater: abstract min/max away...
r38284 return func(pycompat.iterbytestr(self._value))
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 def filter(self, context, mapping, select):
Augie Fackler
formatting: blacken the codebase...
r43346 raise error.ParseError(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'%r is not filterable') % pycompat.bytestr(self._value)
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 def itermaps(self, context):
Augie Fackler
formatting: blacken the codebase...
r43346 raise error.ParseError(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'%r is not iterable of mappings') % pycompat.bytestr(self._value)
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228
def join(self, context, mapping, sep):
return joinitems(pycompat.iterbytestr(self._value), sep)
def show(self, context, mapping):
return self._value
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
return bool(self._value)
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 def tovalue(self, context, mapping):
return self._value
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 class wrappedvalue(wrapped):
"""Generic wrapper for pure non-list/dict/bytes value"""
def __init__(self, value):
self._value = value
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286 def contains(self, context, mapping, item):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b"%r is not iterable") % self._value)
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 def getmember(self, context, mapping, key):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'%r is not a dictionary') % self._value)
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261
Yuya Nishihara
templater: abstract min/max away...
r38284 def getmin(self, context, mapping):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b"%r is not iterable") % self._value)
Yuya Nishihara
templater: abstract min/max away...
r38284
def getmax(self, context, mapping):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b"%r is not iterable") % self._value)
Yuya Nishihara
templater: abstract min/max away...
r38284
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 def filter(self, context, mapping, select):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b"%r is not iterable") % self._value)
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 def itermaps(self, context):
Augie Fackler
formatting: blacken the codebase...
r43346 raise error.ParseError(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 _(b'%r is not iterable of mappings') % self._value
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228
def join(self, context, mapping, sep):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'%r is not iterable') % self._value)
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228
def show(self, context, mapping):
Yuya Nishihara
templater: fix string representation of wrapped None...
r38291 if self._value is None:
return b''
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 return pycompat.bytestr(self._value)
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
if self._value is None:
return False
if isinstance(self._value, bool):
return self._value
# otherwise evaluate as string, which means 0 is True
return bool(pycompat.bytestr(self._value))
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 def tovalue(self, context, mapping):
return self._value
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: make date wrapper support dot/map operations...
r38305 class date(mappable, wrapped):
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304 """Wrapper for date tuple"""
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def __init__(self, value, showfmt=b'%d %d'):
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304 # value may be (float, int), but public interface shouldn't support
# floating-point timestamp
self._unixtime, self._tzoffset = map(int, value)
Yuya Nishihara
templater: restore the original string format of {date}...
r38318 self._showfmt = showfmt
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304
def contains(self, context, mapping, item):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'date is not iterable'))
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304
def getmember(self, context, mapping, key):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'date is not a dictionary'))
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304
def getmin(self, context, mapping):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'date is not iterable'))
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304
def getmax(self, context, mapping):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'date is not iterable'))
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 def filter(self, context, mapping, select):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'date is not iterable'))
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304 def join(self, context, mapping, sep):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b"date is not iterable"))
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304
def show(self, context, mapping):
Yuya Nishihara
templater: restore the original string format of {date}...
r38318 return self._showfmt % (self._unixtime, self._tzoffset)
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304
Yuya Nishihara
templater: make date wrapper support dot/map operations...
r38305 def tomap(self, context):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return {b'unixtime': self._unixtime, b'tzoffset': self._tzoffset}
Yuya Nishihara
templater: make date wrapper support dot/map operations...
r38305
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
return True
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304 def tovalue(self, context, mapping):
return (self._unixtime, self._tzoffset)
Yuya Nishihara
templatefilters: declare input type as date where appropriate...
r37244
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291 class hybrid(wrapped):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 """Wrapper for list or dict to support legacy template
This class allows us to handle both:
- "{files}" (legacy command-line-specific list hack) and
- "{files % '{file}\n'}" (hgweb-style with inlining and function support)
and to access raw values:
- "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}"
- "{get(extras, key)}"
- "{files|json}"
"""
def __init__(self, gen, values, makemap, joinfmt, keytype=None):
Yuya Nishihara
templater: factor out generator of join()-ed items...
r37341 self._gen = gen # generator or function returning generator
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 self._values = values
self._makemap = makemap
Yuya Nishihara
templater: mark .joinfmt as a private attribute
r37345 self._joinfmt = joinfmt
Yuya Nishihara
templater: mark .keytype as a private attribute
r38287 self._keytype = keytype # hint for 'x in y' where type(x) is unresolved
Yuya Nishihara
templater: mark .gen as a private attribute
r37293
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286 def contains(self, context, mapping, item):
Yuya Nishihara
templater: mark .keytype as a private attribute
r38287 item = unwrapastype(context, mapping, item, self._keytype)
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286 return item in self._values
Yuya Nishihara
templater: move getdictitem() to hybrid class...
r38260 def getmember(self, context, mapping, key):
# TODO: maybe split hybrid list/dict types?
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if not util.safehasattr(self._values, b'get'):
raise error.ParseError(_(b'not a dictionary'))
Yuya Nishihara
templater: mark .keytype as a private attribute
r38287 key = unwrapastype(context, mapping, key, self._keytype)
Yuya Nishihara
templater: move getdictitem() to hybrid class...
r38260 return self._wrapvalue(key, self._values.get(key))
Yuya Nishihara
templater: abstract min/max away...
r38284 def getmin(self, context, mapping):
return self._getby(context, mapping, min)
def getmax(self, context, mapping):
return self._getby(context, mapping, max)
def _getby(self, context, mapping, func):
if not self._values:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'empty sequence'))
Yuya Nishihara
templater: abstract min/max away...
r38284 val = func(self._values)
return self._wrapvalue(val, val)
Yuya Nishihara
templater: move getdictitem() to hybrid class...
r38260 def _wrapvalue(self, key, val):
if val is None:
return
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if util.safehasattr(val, b'_makemap'):
Yuya Nishihara
templater: inline wraphybridvalue()...
r38285 # a nested hybrid list/dict, which has its own way of map operation
return val
Yuya Nishihara
templater: rename mappable to hybriditem as it is the primary use case...
r38302 return hybriditem(None, key, val, self._makemap)
Yuya Nishihara
templater: move getdictitem() to hybrid class...
r38260
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 def filter(self, context, mapping, select):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if util.safehasattr(self._values, b'get'):
Augie Fackler
formatting: blacken the codebase...
r43346 values = {
k: v
Gregory Szorc
py3: finish porting iteritems() to pycompat and remove source transformer...
r43376 for k, v in pycompat.iteritems(self._values)
Augie Fackler
formatting: blacken the codebase...
r43346 if select(self._wrapvalue(k, v))
}
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 else:
values = [v for v in self._values if select(self._wrapvalue(v, v))]
return hybrid(None, values, self._makemap, self._joinfmt, self._keytype)
Yuya Nishihara
templater: pass context to itermaps() for future extension...
r37340 def itermaps(self, context):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 makemap = self._makemap
for x in self._values:
yield makemap(x)
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291
Yuya Nishihara
templater: abstract away from joinfmt...
r37343 def join(self, context, mapping, sep):
# TODO: switch gen to (context, mapping) API?
Yuya Nishihara
templater: mark .joinfmt as a private attribute
r37345 return joinitems((self._joinfmt(x) for x in self._values), sep)
Yuya Nishihara
templater: abstract away from joinfmt...
r37343
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291 def show(self, context, mapping):
# TODO: switch gen to (context, mapping) API?
Yuya Nishihara
templater: mark .gen as a private attribute
r37293 gen = self._gen
Yuya Nishihara
templater: factor out generator of join()-ed items...
r37341 if gen is None:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return self.join(context, mapping, b' ')
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291 if callable(gen):
return gen()
return gen
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
return bool(self._values)
Yuya Nishihara
templater: define interface for objects requiring unwrapvalue()...
r37297 def tovalue(self, context, mapping):
Yuya Nishihara
templater: drop hybrid-ness on unwrapvalue()...
r38288 # TODO: make it non-recursive for trivial lists/dicts
xs = self._values
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 if util.safehasattr(xs, b'get'):
Augie Fackler
formatting: blacken the codebase...
r43346 return {
Gregory Szorc
py3: finish porting iteritems() to pycompat and remove source transformer...
r43376 k: unwrapvalue(context, mapping, v)
for k, v in pycompat.iteritems(xs)
Augie Fackler
formatting: blacken the codebase...
r43346 }
Yuya Nishihara
templater: drop hybrid-ness on unwrapvalue()...
r38288 return [unwrapvalue(context, mapping, x) for x in xs]
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: promote tomap() to an interface type...
r38303 class hybriditem(mappable, wrapped):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 """Wrapper for non-list/dict object to support map operation
This class allows us to handle both:
- "{manifest}"
- "{manifest % '{rev}:{node}'}"
- "{manifest.rev}"
"""
def __init__(self, gen, key, value, makemap):
Yuya Nishihara
templater: drop unneeded generator from mappable object...
r37294 self._gen = gen # generator or function returning generator
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 self._key = key
self._value = value # may be generator of strings
self._makemap = makemap
Yuya Nishihara
templater: promote tomap() to an interface type...
r38303 def tomap(self, context):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 return self._makemap(self._key)
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286 def contains(self, context, mapping, item):
w = makewrapped(context, mapping, self._value)
return w.contains(context, mapping, item)
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 def getmember(self, context, mapping, key):
w = makewrapped(context, mapping, self._value)
return w.getmember(context, mapping, key)
Yuya Nishihara
templater: abstract min/max away...
r38284 def getmin(self, context, mapping):
w = makewrapped(context, mapping, self._value)
return w.getmin(context, mapping)
def getmax(self, context, mapping):
w = makewrapped(context, mapping, self._value)
return w.getmax(context, mapping)
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 def filter(self, context, mapping, select):
w = makewrapped(context, mapping, self._value)
return w.filter(context, mapping, select)
Yuya Nishihara
templater: abstract away from joinfmt...
r37343 def join(self, context, mapping, sep):
Yuya Nishihara
templater: consistently join() string-like object per character (BC)...
r38230 w = makewrapped(context, mapping, self._value)
return w.join(context, mapping, sep)
Yuya Nishihara
templater: abstract away from joinfmt...
r37343
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291 def show(self, context, mapping):
# TODO: switch gen to (context, mapping) API?
Yuya Nishihara
templater: mark .gen as a private attribute
r37293 gen = self._gen
Yuya Nishihara
templater: drop unneeded generator from mappable object...
r37294 if gen is None:
return pycompat.bytestr(self._value)
Yuya Nishihara
templater: define interface for objects requiring unwraphybrid()...
r37291 if callable(gen):
return gen()
return gen
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
Yuya Nishihara
templater: fix truth testing of integer 0 taken from a list/dict...
r38466 w = makewrapped(context, mapping, self._value)
return w.tobool(context, mapping)
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308
Yuya Nishihara
templater: define interface for objects requiring unwrapvalue()...
r37297 def tovalue(self, context, mapping):
return _unthunk(context, mapping, self._value)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: introduce wrapper for smartset (API)...
r45080 class revslist(wrapped):
"""Wrapper for a smartset (a list/set of revision numbers)
If name specified, the revs will be rendered with the old-style list
template of the given name by default.
Yuya Nishihara
templater: remember cache key of evaluated revset...
r45082
The cachekey provides a hint to cache further computation on this
smartset. If the underlying smartset is dynamically created, the cachekey
should be None.
Yuya Nishihara
templater: introduce wrapper for smartset (API)...
r45080 """
Yuya Nishihara
templater: remember cache key of evaluated revset...
r45082 def __init__(self, repo, revs, name=None, cachekey=None):
Yuya Nishihara
templater: introduce wrapper for smartset (API)...
r45080 assert isinstance(revs, smartset.abstractsmartset)
self._repo = repo
self._revs = revs
self._name = name
Yuya Nishihara
templater: remember cache key of evaluated revset...
r45082 self.cachekey = cachekey
Yuya Nishihara
templater: introduce wrapper for smartset (API)...
r45080
def contains(self, context, mapping, item):
rev = unwrapinteger(context, mapping, item)
return rev in self._revs
def getmember(self, context, mapping, key):
raise error.ParseError(_(b'not a dictionary'))
def getmin(self, context, mapping):
makehybriditem = self._makehybriditemfunc()
return makehybriditem(self._revs.min())
def getmax(self, context, mapping):
makehybriditem = self._makehybriditemfunc()
return makehybriditem(self._revs.max())
def filter(self, context, mapping, select):
makehybriditem = self._makehybriditemfunc()
frevs = self._revs.filter(lambda r: select(makehybriditem(r)))
# once filtered, no need to support old-style list template
return revslist(self._repo, frevs, name=None)
def itermaps(self, context):
makemap = self._makemapfunc()
for r in self._revs:
yield makemap(r)
def _makehybriditemfunc(self):
makemap = self._makemapfunc()
return lambda r: hybriditem(None, r, r, makemap)
def _makemapfunc(self):
repo = self._repo
name = self._name
if name:
return lambda r: {name: r, b'ctx': repo[r]}
else:
return lambda r: {b'ctx': repo[r]}
def join(self, context, mapping, sep):
return joinitems(self._revs, sep)
def show(self, context, mapping):
if self._name:
srevs = [b'%d' % r for r in self._revs]
return _showcompatlist(context, mapping, self._name, srevs)
else:
return self.join(context, mapping, b' ')
def tobool(self, context, mapping):
return bool(self._revs)
def tovalue(self, context, mapping):
Yuya Nishihara
templater: fix cbor() filter to accept smartset...
r45081 return self._revs
Yuya Nishihara
templater: introduce wrapper for smartset (API)...
r45080
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 class _mappingsequence(wrapped):
"""Wrapper for sequence of template mappings
This represents an inner template structure (i.e. a list of dicts),
which can also be rendered by the specified named/literal template.
Template mappings may be nested.
"""
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def __init__(self, name=None, tmpl=None, sep=b''):
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 if name is not None and tmpl is not None:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ProgrammingError(
b'name and tmpl are mutually exclusive'
)
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 self._name = name
self._tmpl = tmpl
self._defaultsep = sep
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286 def contains(self, context, mapping, item):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'not comparable'))
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 def getmember(self, context, mapping, key):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'not a dictionary'))
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261
Yuya Nishihara
templater: abstract min/max away...
r38284 def getmin(self, context, mapping):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'not comparable'))
Yuya Nishihara
templater: abstract min/max away...
r38284
def getmax(self, context, mapping):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'not comparable'))
Yuya Nishihara
templater: abstract min/max away...
r38284
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 def filter(self, context, mapping, select):
Yuya Nishihara
templater: extend filter() to accept template expression for emptiness test...
r38468 # implement if necessary; we'll need a wrapped type for a mapping dict
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'not filterable without template'))
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 def join(self, context, mapping, sep):
mapsiter = _iteroverlaymaps(context, mapping, self.itermaps(context))
if self._name:
itemiter = (context.process(self._name, m) for m in mapsiter)
elif self._tmpl:
itemiter = (context.expand(self._tmpl, m) for m in mapsiter)
else:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'not displayable without template'))
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 return joinitems(itemiter, sep)
def show(self, context, mapping):
return self.join(context, mapping, self._defaultsep)
def tovalue(self, context, mapping):
Yuya Nishihara
formatter: remove template resources from nested items before generating JSON
r37520 knownres = context.knownresourcekeys()
items = []
for nm in self.itermaps(context):
# drop internal resources (recursively) which shouldn't be displayed
lm = context.overlaymap(mapping, nm)
Augie Fackler
formatting: blacken the codebase...
r43346 items.append(
{
k: unwrapvalue(context, lm, v)
Gregory Szorc
py3: finish porting iteritems() to pycompat and remove source transformer...
r43376 for k, v in pycompat.iteritems(nm)
Augie Fackler
formatting: blacken the codebase...
r43346 if k not in knownres
}
)
Yuya Nishihara
formatter: remove template resources from nested items before generating JSON
r37520 return items
Yuya Nishihara
templater: add class representing a nested mappings...
r37417
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 class mappinggenerator(_mappingsequence):
"""Wrapper for generator of template mappings
The function ``make(context, *args)`` should return a generator of
mapping dicts.
"""
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def __init__(self, make, args=(), name=None, tmpl=None, sep=b''):
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 super(mappinggenerator, self).__init__(name, tmpl, sep)
self._make = make
self._args = args
def itermaps(self, context):
return self._make(context, *self._args)
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
return _nonempty(self.itermaps(context))
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 class mappinglist(_mappingsequence):
"""Wrapper for list of template mappings"""
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def __init__(self, mappings, name=None, tmpl=None, sep=b''):
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 super(mappinglist, self).__init__(name, tmpl, sep)
self._mappings = mappings
def itermaps(self, context):
return iter(self._mappings)
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
return bool(self._mappings)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: add wrapper for a single template mapping...
r40509 class mappingdict(mappable, _mappingsequence):
"""Wrapper for a single template mapping
This isn't a sequence in a way that the underlying dict won't be iterated
as a dict, but shares most of the _mappingsequence functions.
"""
def __init__(self, mapping, name=None, tmpl=None):
super(mappingdict, self).__init__(name, tmpl)
self._mapping = mapping
def tomap(self, context):
return self._mapping
def tobool(self, context, mapping):
# no idea when a template mapping should be considered an empty, but
# a mapping dict should have at least one item in practice, so always
# mark this as non-empty.
return True
def tovalue(self, context, mapping):
return super(mappingdict, self).tovalue(context, mapping)[0]
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templatefuncs: specialize "no match" value of search() to allow % operation...
r40971 class mappingnone(wrappedvalue):
"""Wrapper for None, but supports map operation
This represents None of Optional[mappable]. It's similar to
mapplinglist([]), but the underlying value is not [], but None.
"""
def __init__(self):
super(mappingnone, self).__init__(None)
def itermaps(self, context):
return iter([])
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: wrap result of '%' operation so it never looks like a thunk...
r37517 class mappedgenerator(wrapped):
"""Wrapper for generator of strings which acts as a list
The function ``make(context, *args)`` should return a generator of
byte strings, or a generator of (possibly nested) generators of byte
strings (i.e. a generator for a list of byte strings.)
"""
def __init__(self, make, args=()):
self._make = make
self._args = args
Yuya Nishihara
templater: abstract ifcontains() over wrapped types...
r38286 def contains(self, context, mapping, item):
item = stringify(context, mapping, item)
return item in self.tovalue(context, mapping)
Yuya Nishihara
templater: wrap result of '%' operation so it never looks like a thunk...
r37517 def _gen(self, context):
return self._make(context, *self._args)
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 def getmember(self, context, mapping, key):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'not a dictionary'))
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261
Yuya Nishihara
templater: abstract min/max away...
r38284 def getmin(self, context, mapping):
return self._getby(context, mapping, min)
def getmax(self, context, mapping):
return self._getby(context, mapping, max)
def _getby(self, context, mapping, func):
xs = self.tovalue(context, mapping)
if not xs:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'empty sequence'))
Yuya Nishihara
templater: abstract min/max away...
r38284 return func(xs)
Yuya Nishihara
templater: introduce filter() function to remove empty items from list...
r38467 @staticmethod
def _filteredgen(context, mapping, make, args, select):
for x in make(context, *args):
s = stringify(context, mapping, x)
if select(wrappedbytes(s)):
yield s
def filter(self, context, mapping, select):
args = (mapping, self._make, self._args, select)
return mappedgenerator(self._filteredgen, args)
Yuya Nishihara
templater: wrap result of '%' operation so it never looks like a thunk...
r37517 def itermaps(self, context):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(_(b'list of strings is not mappable'))
Yuya Nishihara
templater: wrap result of '%' operation so it never looks like a thunk...
r37517
def join(self, context, mapping, sep):
return joinitems(self._gen(context), sep)
def show(self, context, mapping):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return self.join(context, mapping, b'')
Yuya Nishihara
templater: wrap result of '%' operation so it never looks like a thunk...
r37517
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def tobool(self, context, mapping):
return _nonempty(self._gen(context))
Yuya Nishihara
templater: wrap result of '%' operation so it never looks like a thunk...
r37517 def tovalue(self, context, mapping):
return [stringify(context, mapping, x) for x in self._gen(context)]
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def hybriddict(data, key=b'key', value=b'value', fmt=None, gen=None):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 """Wrap data to support both dict-like and string-like operations"""
prefmt = pycompat.identity
if fmt is None:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 fmt = b'%s=%s'
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 prefmt = pycompat.bytestr
Augie Fackler
formatting: blacken the codebase...
r43346 return hybrid(
gen,
data,
lambda k: {key: k, value: data[k]},
lambda k: fmt % (prefmt(k), prefmt(data[k])),
)
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939
def hybridlist(data, name, fmt=None, gen=None):
"""Wrap data to support both list-like and string-like operations"""
prefmt = pycompat.identity
if fmt is None:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 fmt = b'%s'
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 prefmt = pycompat.bytestr
return hybrid(gen, data, lambda x: {name: x}, lambda x: fmt % prefmt(x))
Augie Fackler
formatting: blacken the codebase...
r43346
def compatdict(
context,
mapping,
name,
data,
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 key=b'key',
value=b'value',
Augie Fackler
formatting: blacken the codebase...
r43346 fmt=None,
plural=None,
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 separator=b' ',
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 """Wrap data like hybriddict(), but also supports old-style list template
This exists for backward compatibility with the old-style template. Use
hybriddict() for new template keywords.
"""
Gregory Szorc
py3: finish porting iteritems() to pycompat and remove source transformer...
r43376 c = [{key: k, value: v} for k, v in pycompat.iteritems(data)]
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 f = _showcompatlist(context, mapping, name, c, plural, separator)
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
Augie Fackler
formatting: blacken the codebase...
r43346
def compatlist(
context,
mapping,
name,
data,
element=None,
fmt=None,
plural=None,
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 separator=b' ',
Augie Fackler
formatting: blacken the codebase...
r43346 ):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 """Wrap data like hybridlist(), but also supports old-style list template
This exists for backward compatibility with the old-style template. Use
hybridlist() for new template keywords.
"""
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 f = _showcompatlist(context, mapping, name, data, plural, separator)
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 return hybridlist(data, name=element or name, fmt=fmt, gen=f)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templatekw: alias {name} of file copies dict to {path}...
r39404 def compatfilecopiesdict(context, mapping, name, copies):
"""Wrap list of (dest, source) file names to support old-style list
template and field names
This exists for backward compatibility. Use hybriddict for new template
keywords.
"""
# no need to provide {path} to old-style list template
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 c = [{b'name': k, b'source': v} for k, v in copies]
f = _showcompatlist(context, mapping, name, c, plural=b'file_copies')
Yuya Nishihara
templatekw: alias {name} of file copies dict to {path}...
r39404 copies = util.sortdict(copies)
Augie Fackler
formatting: blacken the codebase...
r43346 return hybrid(
f,
copies,
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 lambda k: {b'name': k, b'path': k, b'source': copies[k]},
lambda k: b'%s (%s)' % (k, copies[k]),
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
templatekw: alias {name} of file copies dict to {path}...
r39404
Yuya Nishihara
templatekw: alias {file} of files list to {path}...
r39403 def compatfileslist(context, mapping, name, files):
"""Wrap list of file names to support old-style list template and field
names
This exists for backward compatibility. Use hybridlist for new template
keywords.
"""
f = _showcompatlist(context, mapping, name, files)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return hybrid(
f, files, lambda x: {b'file': x, b'path': x}, pycompat.identity
)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templatekw: alias {file} of files list to {path}...
r39403
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def _showcompatlist(
context, mapping, name, values, plural=None, separator=b' '
):
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 """Return a generator that renders old-style list template
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 name is name of key in template map.
values is list of strings or dicts.
plural is plural of name, if not simply name + 's'.
separator is used to join values as a string
expansion works like this, given name 'foo'.
if values is empty, expand 'no_foos'.
if 'foo' not in template map, return values as a string,
joined by 'separator'.
expand 'start_foos'.
for each value, expand 'foo'. if 'last_foo' in template
map, expand it instead of 'foo' for last key.
expand 'end_foos'.
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 """
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 if not plural:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 plural = name + b's'
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 if not values:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 noname = b'no_' + plural
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 if context.preload(noname):
yield context.process(noname, mapping)
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 return
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 if not context.preload(name):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 if isinstance(values[0], bytes):
yield separator.join(values)
else:
for v in values:
r = dict(v)
r.update(mapping)
yield r
return
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 startname = b'start_' + plural
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 if context.preload(startname):
yield context.process(startname, mapping)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 def one(v, tag=name):
Yuya Nishihara
templater: factor out function to create mapping dict for nested evaluation...
r37092 vmapping = {}
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 try:
vmapping.update(v)
# Python 2 raises ValueError if the type of v is wrong. Python
# 3 raises TypeError.
except (AttributeError, TypeError, ValueError):
try:
# Python 2 raises ValueError trying to destructure an e.g.
# bytes. Python 3 raises TypeError.
for a, b in v:
vmapping[a] = b
except (TypeError, ValueError):
vmapping[name] = v
Yuya Nishihara
templater: factor out function to create mapping dict for nested evaluation...
r37092 vmapping = context.overlaymap(mapping, vmapping)
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 return context.process(tag, vmapping)
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 lastname = b'last_' + name
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 if context.preload(lastname):
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939 last = values.pop()
else:
last = None
for v in values:
yield one(v)
if last is not None:
yield one(last, tag=lastname)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 endname = b'end_' + plural
Yuya Nishihara
templater: use template context to render old-style list template...
r37086 if context.preload(endname):
yield context.process(endname, mapping)
Yuya Nishihara
templater: move hybrid class and functions to templateutil module...
r36939
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 def flatten(context, mapping, thing):
Yuya Nishihara
templateutil: move flatten() from templater...
r37174 """Yield a single stream from a possibly nested set of iterators"""
Yuya Nishihara
templater: inline unwraphybrid()...
r38289 if isinstance(thing, wrapped):
thing = thing.show(context, mapping)
Yuya Nishihara
templateutil: move flatten() from templater...
r37174 if isinstance(thing, bytes):
yield thing
elif isinstance(thing, str):
# We can only hit this on Python 3, and it's here to guard
# against infinite recursion.
Augie Fackler
formatting: blacken the codebase...
r43346 raise error.ProgrammingError(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 b'Mercurial IO including templates is done'
b' with bytes, not strings, got %r' % thing
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
templateutil: move flatten() from templater...
r37174 elif thing is None:
pass
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 elif not util.safehasattr(thing, b'__iter__'):
Yuya Nishihara
templateutil: move flatten() from templater...
r37174 yield pycompat.bytestr(thing)
else:
for i in thing:
Yuya Nishihara
templater: inline unwraphybrid()...
r38289 if isinstance(i, wrapped):
i = i.show(context, mapping)
Yuya Nishihara
templateutil: move flatten() from templater...
r37174 if isinstance(i, bytes):
yield i
elif i is None:
pass
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 elif not util.safehasattr(i, b'__iter__'):
Yuya Nishihara
templateutil: move flatten() from templater...
r37174 yield pycompat.bytestr(i)
else:
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 for j in flatten(context, mapping, i):
Yuya Nishihara
templateutil: move flatten() from templater...
r37174 yield j
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 def stringify(context, mapping, thing):
Yuya Nishihara
templater: move stringify() to templateutil module...
r36938 """Turn values into bytes by converting into text and concatenating them"""
Yuya Nishihara
templateutil: reimplement stringify() using flatten()
r37175 if isinstance(thing, bytes):
return thing # retain localstr to be round-tripped
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 return b''.join(flatten(context, mapping, thing))
Yuya Nishihara
templater: move stringify() to templateutil module...
r36938
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def findsymbolicname(arg):
"""Find symbolic name for the given compiled expression; returns None
if nothing found reliably"""
while True:
func, data = arg
if func is runsymbol:
return data
elif func is runfilter:
arg = data[0]
else:
return None
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 def _nonempty(xiter):
try:
next(xiter)
return True
except StopIteration:
return False
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract private function to evaluate generator to byte string
r37296 def _unthunk(context, mapping, thing):
"""Evaluate a lazy byte string into value"""
if not isinstance(thing, types.GeneratorType):
return thing
return stringify(context, mapping, thing)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def evalrawexp(context, mapping, arg):
"""Evaluate given argument as a bare template object which may require
further processing (such as folding generator of strings)"""
func, data = arg
return func(context, mapping, data)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 def evalwrapped(context, mapping, arg):
"""Evaluate given argument to wrapped object"""
thing = evalrawexp(context, mapping, arg)
return makewrapped(context, mapping, thing)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: add wrapped types for pure non-list/dict values...
r38228 def makewrapped(context, mapping, thing):
"""Lift object to a wrapped type"""
if isinstance(thing, wrapped):
return thing
thing = _unthunk(context, mapping, thing)
if isinstance(thing, bytes):
return wrappedbytes(thing)
return wrappedvalue(thing)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def evalfuncarg(context, mapping, arg):
"""Evaluate given argument as value type"""
Yuya Nishihara
templater: unify unwrapvalue() with _unwrapvalue()...
r38227 return unwrapvalue(context, mapping, evalrawexp(context, mapping, arg))
Yuya Nishihara
templater: extract type conversion from evalfuncarg()...
r37178
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: unify unwrapvalue() with _unwrapvalue()...
r38227 def unwrapvalue(context, mapping, thing):
"""Move the inner value object out of the wrapper"""
Yuya Nishihara
templater: inline unwrapvalue()...
r38226 if isinstance(thing, wrapped):
return thing.tovalue(context, mapping)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 # evalrawexp() may return string, generator of strings or arbitrary object
# such as date tuple, but filter does not want generator.
Yuya Nishihara
templater: extract private function to evaluate generator to byte string
r37296 return _unthunk(context, mapping, thing)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def evalboolean(context, mapping, arg):
"""Evaluate given argument as boolean, but also takes boolean literals"""
func, data = arg
if func is runsymbol:
thing = func(context, mapping, data, default=None)
if thing is None:
# not a template keyword, takes as a boolean literal
Yuya Nishihara
stringutil: bulk-replace call sites to point to new module...
r37102 thing = stringutil.parsebool(data)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 else:
thing = func(context, mapping, data)
Yuya Nishihara
templater: abstract truth testing to fix {if(list_of_empty_strings)}...
r38308 return makewrapped(context, mapping, thing).tobool(context, mapping)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: factor out function that parses argument as date tuple
r37241 def evaldate(context, mapping, arg, err=None):
"""Evaluate given argument as a date tuple or a date string; returns
a (unixtime, offset) tuple"""
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 thing = evalrawexp(context, mapping, arg)
return unwrapdate(context, mapping, thing, err)
Yuya Nishihara
templater: factor out function that parses argument as date tuple
r37241
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 def unwrapdate(context, mapping, thing, err=None):
Yuya Nishihara
templater: introduce a wrapper for date tuple (BC)...
r38304 if isinstance(thing, date):
return thing.tovalue(context, mapping)
# TODO: update hgweb to not return bare tuple; then just stringify 'thing'
Yuya Nishihara
templater: unify unwrapvalue() with _unwrapvalue()...
r38227 thing = unwrapvalue(context, mapping, thing)
Yuya Nishihara
templater: factor out function that parses argument as date tuple
r37241 try:
return dateutil.parsedate(thing)
except AttributeError:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(err or _(b'not a date tuple nor a string'))
Yuya Nishihara
templatefuncs: use evaldate() where seems appropriate...
r37242 except error.ParseError:
if not err:
raise
raise error.ParseError(err)
Yuya Nishihara
templater: factor out function that parses argument as date tuple
r37241
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def evalinteger(context, mapping, arg, err=None):
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 thing = evalrawexp(context, mapping, arg)
return unwrapinteger(context, mapping, thing, err)
Yuya Nishihara
templater: extract unwrapinteger() function from evalinteger()
r37179
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 def unwrapinteger(context, mapping, thing, err=None):
Yuya Nishihara
templater: unify unwrapvalue() with _unwrapvalue()...
r38227 thing = unwrapvalue(context, mapping, thing)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 try:
Yuya Nishihara
templater: extract unwrapinteger() function from evalinteger()
r37179 return int(thing)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 except (TypeError, ValueError):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ParseError(err or _(b'not an integer'))
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def evalstring(context, mapping, arg):
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 return stringify(context, mapping, evalrawexp(context, mapping, arg))
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def evalstringliteral(context, mapping, arg):
"""Evaluate given argument as string template, but returns symbol name
if it is unknown"""
func, data = arg
if func is runsymbol:
thing = func(context, mapping, data, default=data)
else:
thing = func(context, mapping, data)
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 return stringify(context, mapping, thing)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: factor out unwrapastype() from evalastype()...
r37180 _unwrapfuncbytype = {
Yuya Nishihara
templater: unify unwrapvalue() with _unwrapvalue()...
r38227 None: unwrapvalue,
Yuya Nishihara
templater: factor out unwrapastype() from evalastype()...
r37180 bytes: stringify,
Yuya Nishihara
templatefilters: declare input type as date where appropriate...
r37244 date: unwrapdate,
Yuya Nishihara
templater: factor out unwrapastype() from evalastype()...
r37180 int: unwrapinteger,
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 }
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 def unwrapastype(context, mapping, thing, typ):
Yuya Nishihara
templater: factor out unwrapastype() from evalastype()...
r37180 """Move the inner value object out of the wrapper and coerce its type"""
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 try:
Yuya Nishihara
templater: factor out unwrapastype() from evalastype()...
r37180 f = _unwrapfuncbytype[typ]
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 except KeyError:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.ProgrammingError(b'invalid type specified: %r' % typ)
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 return f(context, mapping, thing)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def runinteger(context, mapping, data):
return int(data)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def runstring(context, mapping, data):
return data
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def _recursivesymbolblocker(key):
Yuya Nishihara
templatekw: deprecate old-style template keyword function (API)...
r38966 def showrecursion(context, mapping):
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.Abort(_(b"recursive reference '%s' in template") % key)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 return showrecursion
Augie Fackler
formatting: blacken the codebase...
r43346
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 def runsymbol(context, mapping, key, default=b''):
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 v = context.symbol(mapping, key)
if v is None:
# put poison to cut recursion. we can't move this to parsing phase
# because "x = {x}" is allowed if "x" is a keyword. (issue4758)
safemapping = mapping.copy()
safemapping[key] = _recursivesymbolblocker(key)
try:
v = context.process(key, safemapping)
except TemplateNotFound:
v = default
if callable(v):
# new templatekw
try:
return v(context, mapping)
except ResourceUnavailable:
# unsupported keyword is mapped to empty just like unknown keyword
return None
return v
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def runtemplate(context, mapping, template):
for arg in template:
yield evalrawexp(context, mapping, arg)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def runfilter(context, mapping, data):
arg, filt = data
Yuya Nishihara
templatefilters: allow declaration of input data type...
r37239 thing = evalrawexp(context, mapping, arg)
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 intype = getattr(filt, '_intype', None)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 try:
Yuya Nishihara
templater: pass (context, mapping) down to unwraphybrid()...
r37290 thing = unwrapastype(context, mapping, thing, intype)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 return filt(thing)
Yuya Nishihara
templater: attach hint to input-type error of runfilter()...
r37243 except error.ParseError as e:
raise error.ParseError(bytes(e), hint=_formatfiltererror(arg, filt))
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: attach hint to input-type error of runfilter()...
r37243 def _formatfiltererror(arg, filt):
fn = pycompat.sysbytes(filt.__name__)
sym = findsymbolicname(arg)
if not sym:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 return _(b"incompatible use of template filter '%s'") % fn
return _(b"template filter '%s' is not compatible with keyword '%s'") % (
Augie Fackler
formatting: blacken the codebase...
r43346 fn,
sym,
)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 def _iteroverlaymaps(context, origmapping, newmappings):
"""Generate combined mappings from the original mapping and an iterable
of partial mappings to override the original"""
for i, nm in enumerate(newmappings):
lm = context.overlaymap(origmapping, nm)
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 lm[b'index'] = i
Yuya Nishihara
templater: add class representing a nested mappings...
r37417 yield lm
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: show slightly better hint on map operation error...
r38290 def _applymap(context, mapping, d, darg, targ):
try:
diter = d.itermaps(context)
except error.ParseError as err:
sym = findsymbolicname(darg)
if not sym:
raise
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 hint = _(b"keyword '%s' does not support map operation") % sym
Yuya Nishihara
templater: show slightly better hint on map operation error...
r38290 raise error.ParseError(bytes(err), hint=hint)
for lm in _iteroverlaymaps(context, mapping, diter):
Yuya Nishihara
templater: wrap result of '%' operation so it never looks like a thunk...
r37517 yield evalrawexp(context, lm, targ)
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def runmap(context, mapping, data):
darg, targ = data
Yuya Nishihara
templater: always map over a wrapped object...
r38231 d = evalwrapped(context, mapping, darg)
Yuya Nishihara
templater: show slightly better hint on map operation error...
r38290 return mappedgenerator(_applymap, args=(mapping, d, darg, targ))
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def runmember(context, mapping, data):
darg, memb = data
Yuya Nishihara
templater: do dict lookup over a wrapped object...
r38258 d = evalwrapped(context, mapping, darg)
Yuya Nishihara
templater: promote tomap() to an interface type...
r38303 if isinstance(d, mappable):
lm = context.overlaymap(mapping, d.tomap(context))
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 return runsymbol(context, lm, memb)
Yuya Nishihara
templater: add try-except stub to runmember()...
r38259 try:
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 return d.getmember(context, mapping, memb)
except error.ParseError as err:
Yuya Nishihara
templater: add try-except stub to runmember()...
r38259 sym = findsymbolicname(darg)
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 if not sym:
raise
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 hint = _(b"keyword '%s' does not support member operation") % sym
Yuya Nishihara
templater: promote getmember() to an interface of wrapped types
r38261 raise error.ParseError(bytes(err), hint=hint)
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def runnegate(context, mapping, data):
Augie Fackler
formatting: blacken the codebase...
r43346 data = evalinteger(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 context, mapping, data, _(b'negation needs an integer argument')
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 return -data
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 def runarithmetic(context, mapping, data):
func, left, right = data
Augie Fackler
formatting: blacken the codebase...
r43346 left = evalinteger(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 context, mapping, left, _(b'arithmetic only defined on integers')
Augie Fackler
formatting: blacken the codebase...
r43346 )
right = evalinteger(
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 context, mapping, right, _(b'arithmetic only defined on integers')
Augie Fackler
formatting: blacken the codebase...
r43346 )
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931 try:
return func(left, right)
except ZeroDivisionError:
Augie Fackler
formatting: byteify all mercurial/ and hgext/ string literals...
r43347 raise error.Abort(_(b'division by zero is not defined'))
Yuya Nishihara
templater: extract template evaluation utility to new module...
r36931
Augie Fackler
formatting: blacken the codebase...
r43346
Yuya Nishihara
templater: factor out generator of join()-ed items...
r37341 def joinitems(itemiter, sep):
"""Join items with the separator; Returns generator of bytes"""
first = True
for x in itemiter:
if first:
first = False
Yuya Nishihara
templater: micro-optimize join() with empty separator
r37342 elif sep:
Yuya Nishihara
templater: factor out generator of join()-ed items...
r37341 yield sep
yield x