mercurial/templateutil.py
branchstable
changeset 49366 288de6f5d724
parent 48946 642e31cb55f0
child 50568 82ef5410f2aa
equal deleted inserted replaced
49364:e8ea403b1c46 49366:288de6f5d724
     3 # Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
     3 # Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
     4 #
     4 #
     5 # This software may be used and distributed according to the terms of the
     5 # This software may be used and distributed according to the terms of the
     6 # GNU General Public License version 2 or any later version.
     6 # GNU General Public License version 2 or any later version.
     7 
     7 
     8 from __future__ import absolute_import
       
     9 
     8 
    10 import abc
     9 import abc
    11 import types
    10 import types
    12 
    11 
    13 from .i18n import _
    12 from .i18n import _
    30 
    29 
    31 class TemplateNotFound(error.Abort):
    30 class TemplateNotFound(error.Abort):
    32     pass
    31     pass
    33 
    32 
    34 
    33 
    35 class wrapped(object):  # pytype: disable=ignored-metaclass
    34 class wrapped:  # pytype: disable=ignored-metaclass
    36     """Object requiring extra conversion prior to displaying or processing
    35     """Object requiring extra conversion prior to displaying or processing
    37     as value
    36     as value
    38 
    37 
    39     Use unwrapvalue() or unwrapastype() to obtain the inner object.
    38     Use unwrapvalue() or unwrapastype() to obtain the inner object.
    40     """
    39     """
   107 
   106 
   108         A returned value must be serializable by templaterfilters.json().
   107         A returned value must be serializable by templaterfilters.json().
   109         """
   108         """
   110 
   109 
   111 
   110 
   112 class mappable(object):  # pytype: disable=ignored-metaclass
   111 class mappable:  # pytype: disable=ignored-metaclass
   113     """Object which can be converted to a single template mapping"""
   112     """Object which can be converted to a single template mapping"""
   114 
   113 
   115     __metaclass__ = abc.ABCMeta
   114     __metaclass__ = abc.ABCMeta
   116 
   115 
   117     def itermaps(self, context):
   116     def itermaps(self, context):
   309 
   308 
   310     def filter(self, context, mapping, select):
   309     def filter(self, context, mapping, select):
   311         if util.safehasattr(self._values, b'get'):
   310         if util.safehasattr(self._values, b'get'):
   312             values = {
   311             values = {
   313                 k: v
   312                 k: v
   314                 for k, v in pycompat.iteritems(self._values)
   313                 for k, v in self._values.items()
   315                 if select(self._wrapvalue(k, v))
   314                 if select(self._wrapvalue(k, v))
   316             }
   315             }
   317         else:
   316         else:
   318             values = [v for v in self._values if select(self._wrapvalue(v, v))]
   317             values = [v for v in self._values if select(self._wrapvalue(v, v))]
   319         return hybrid(None, values, self._makemap, self._joinfmt, self._keytype)
   318         return hybrid(None, values, self._makemap, self._joinfmt, self._keytype)
   341 
   340 
   342     def tovalue(self, context, mapping):
   341     def tovalue(self, context, mapping):
   343         # TODO: make it non-recursive for trivial lists/dicts
   342         # TODO: make it non-recursive for trivial lists/dicts
   344         xs = self._values
   343         xs = self._values
   345         if util.safehasattr(xs, b'get'):
   344         if util.safehasattr(xs, b'get'):
   346             return {
   345             return {k: unwrapvalue(context, mapping, v) for k, v in xs.items()}
   347                 k: unwrapvalue(context, mapping, v)
       
   348                 for k, v in pycompat.iteritems(xs)
       
   349             }
       
   350         return [unwrapvalue(context, mapping, x) for x in xs]
   346         return [unwrapvalue(context, mapping, x) for x in xs]
   351 
   347 
   352 
   348 
   353 class hybriditem(mappable, wrapped):
   349 class hybriditem(mappable, wrapped):
   354     """Wrapper for non-list/dict object to support map operation
   350     """Wrapper for non-list/dict object to support map operation
   536             # drop internal resources (recursively) which shouldn't be displayed
   532             # drop internal resources (recursively) which shouldn't be displayed
   537             lm = context.overlaymap(mapping, nm)
   533             lm = context.overlaymap(mapping, nm)
   538             items.append(
   534             items.append(
   539                 {
   535                 {
   540                     k: unwrapvalue(context, lm, v)
   536                     k: unwrapvalue(context, lm, v)
   541                     for k, v in pycompat.iteritems(nm)
   537                     for k, v in nm.items()
   542                     if k not in knownres
   538                     if k not in knownres
   543                 }
   539                 }
   544             )
   540             )
   545         return items
   541         return items
   546 
   542 
   714     """Wrap data like hybriddict(), but also supports old-style list template
   710     """Wrap data like hybriddict(), but also supports old-style list template
   715 
   711 
   716     This exists for backward compatibility with the old-style template. Use
   712     This exists for backward compatibility with the old-style template. Use
   717     hybriddict() for new template keywords.
   713     hybriddict() for new template keywords.
   718     """
   714     """
   719     c = [{key: k, value: v} for k, v in pycompat.iteritems(data)]
   715     c = [{key: k, value: v} for k, v in data.items()]
   720     f = _showcompatlist(context, mapping, name, c, plural, separator)
   716     f = _showcompatlist(context, mapping, name, c, plural, separator)
   721     return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
   717     return hybriddict(data, key=key, value=value, fmt=fmt, gen=f)
   722 
   718 
   723 
   719 
   724 def compatlist(
   720 def compatlist(