mercurial/formatter.py
branchstable
changeset 49366 288de6f5d724
parent 48946 642e31cb55f0
equal deleted inserted replaced
49364:e8ea403b1c46 49366:288de6f5d724
   103 bar
   103 bar
   104 >>> show(subrepos, template=b'{reponame}: {join(files % "{path}", ", ")}\\n')
   104 >>> show(subrepos, template=b'{reponame}: {join(files % "{path}", ", ")}\\n')
   105 baz: foo, bar
   105 baz: foo, bar
   106 """
   106 """
   107 
   107 
   108 from __future__ import absolute_import, print_function
       
   109 
   108 
   110 import contextlib
   109 import contextlib
   111 import itertools
   110 import itertools
   112 import os
   111 import os
       
   112 import pickle
   113 
   113 
   114 from .i18n import _
   114 from .i18n import _
   115 from .node import (
   115 from .node import (
   116     hex,
   116     hex,
   117     short,
   117     short,
   131     cborutil,
   131     cborutil,
   132     dateutil,
   132     dateutil,
   133     stringutil,
   133     stringutil,
   134 )
   134 )
   135 
   135 
   136 pickle = util.pickle
       
   137 
       
   138 
   136 
   139 def isprintable(obj):
   137 def isprintable(obj):
   140     """Check if the given object can be directly passed in to formatter's
   138     """Check if the given object can be directly passed in to formatter's
   141     write() and data() functions
   139     write() and data() functions
   142 
   140 
   143     Returns False if the object is unsupported or must be pre-processed by
   141     Returns False if the object is unsupported or must be pre-processed by
   144     formatdate(), formatdict(), or formatlist().
   142     formatdate(), formatdict(), or formatlist().
   145     """
   143     """
   146     return isinstance(obj, (type(None), bool, int, pycompat.long, float, bytes))
   144     return isinstance(obj, (type(None), bool, int, int, float, bytes))
   147 
   145 
   148 
   146 
   149 class _nullconverter(object):
   147 class _nullconverter:
   150     '''convert non-primitive data types to be processed by formatter'''
   148     '''convert non-primitive data types to be processed by formatter'''
   151 
   149 
   152     # set to True if context object should be stored as item
   150     # set to True if context object should be stored as item
   153     storecontext = False
   151     storecontext = False
   154 
   152 
   175     def formatlist(data, name, fmt, sep):
   173     def formatlist(data, name, fmt, sep):
   176         '''convert iterable to appropriate list format'''
   174         '''convert iterable to appropriate list format'''
   177         return list(data)
   175         return list(data)
   178 
   176 
   179 
   177 
   180 class baseformatter(object):
   178 class baseformatter:
   181 
   179 
   182     # set to True if the formater output a strict format that does not support
   180     # set to True if the formater output a strict format that does not support
   183     # arbitrary output in the stream.
   181     # arbitrary output in the stream.
   184     strict_format = False
   182     strict_format = False
   185 
   183 
   293 
   291 
   294 
   292 
   295 def _iteritems(data):
   293 def _iteritems(data):
   296     '''iterate key-value pairs in stable order'''
   294     '''iterate key-value pairs in stable order'''
   297     if isinstance(data, dict):
   295     if isinstance(data, dict):
   298         return sorted(pycompat.iteritems(data))
   296         return sorted(data.items())
   299     return data
   297     return data
   300 
   298 
   301 
   299 
   302 class _plainconverter(object):
   300 class _plainconverter:
   303     '''convert non-primitive data types to text'''
   301     '''convert non-primitive data types to text'''
   304 
   302 
   305     storecontext = False
   303     storecontext = False
   306 
   304 
   307     @staticmethod
   305     @staticmethod
   452     def end(self):
   450     def end(self):
   453         baseformatter.end(self)
   451         baseformatter.end(self)
   454         self._out.write(b"\n]\n")
   452         self._out.write(b"\n]\n")
   455 
   453 
   456 
   454 
   457 class _templateconverter(object):
   455 class _templateconverter:
   458     '''convert non-primitive data types to be processed by templater'''
   456     '''convert non-primitive data types to be processed by templater'''
   459 
   457 
   460     storecontext = True
   458     storecontext = True
   461 
   459 
   462     @staticmethod
   460     @staticmethod
   541         baseformatter.end(self)
   539         baseformatter.end(self)
   542         self._renderitem(b'docfooter', {})
   540         self._renderitem(b'docfooter', {})
   543 
   541 
   544 
   542 
   545 @attr.s(frozen=True)
   543 @attr.s(frozen=True)
   546 class templatespec(object):
   544 class templatespec:
   547     ref = attr.ib()
   545     ref = attr.ib()
   548     tmpl = attr.ib()
   546     tmpl = attr.ib()
   549     mapfile = attr.ib()
   547     mapfile = attr.ib()
   550     refargs = attr.ib(default=None)
   548     refargs = attr.ib(default=None)
   551     fp = attr.ib(default=None)
   549     fp = attr.ib(default=None)
   558 def reference_templatespec(ref, refargs=None):
   556 def reference_templatespec(ref, refargs=None):
   559     return templatespec(ref, None, None, refargs)
   557     return templatespec(ref, None, None, refargs)
   560 
   558 
   561 
   559 
   562 def literal_templatespec(tmpl):
   560 def literal_templatespec(tmpl):
   563     if pycompat.ispy3:
   561     assert not isinstance(tmpl, str), b'tmpl must not be a str'
   564         assert not isinstance(tmpl, str), b'tmpl must not be a str'
       
   565     return templatespec(b'', tmpl, None)
   562     return templatespec(b'', tmpl, None)
   566 
   563 
   567 
   564 
   568 def mapfile_templatespec(topic, mapfile, fp=None):
   565 def mapfile_templatespec(topic, mapfile, fp=None):
   569     return templatespec(topic, None, mapfile, fp=fp)
   566     return templatespec(topic, None, mapfile, fp=fp)