2012-02-21 02:42:47 +04:00
|
|
|
# formatter.py - generic output formatting for mercurial
|
|
|
|
#
|
|
|
|
# Copyright 2012 Matt Mackall <mpm@selenic.com>
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
|
2015-08-09 05:17:40 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2015-06-10 22:29:13 +03:00
|
|
|
import os
|
2014-09-13 03:29:29 +04:00
|
|
|
|
2015-08-09 05:17:40 +03:00
|
|
|
from .i18n import _
|
|
|
|
from .node import (
|
|
|
|
hex,
|
|
|
|
short,
|
|
|
|
)
|
|
|
|
|
|
|
|
from . import (
|
|
|
|
encoding,
|
2015-10-08 22:55:45 +03:00
|
|
|
error,
|
formatter: add function to convert list to appropriate format (issue5217)
Before, it wasn't possible for formatter to handle array structure other
than date tuple. We've discussed that at the last sprint, which ended we
would probably want to allow only templatable data structure, i.e. a list
of dicts:
data(tags=[{'tag': a}, {'tag': b}, ...])
Unfortunately, it turned out not working well with template functions:
"{ifcontains(a, tags, ...)}"
^^^^^^^^^^^^^^^^^^
"a in tags", where tags should be a plain list/set of tags
So the formatter must at least know if the type [{}] was constructed from
a plain list or was actually a list of dicts.
This patch introduces new explicit interface to convert an array structure
to an appropriate data type for the current formatter, which can be used
as follows:
fm.write('tags', _('tags: %s\n'), fm.formatlist(tags, name='tag'))
No separate fm.data() call should be necessary.
2016-07-10 15:03:06 +03:00
|
|
|
templatekw,
|
2015-08-09 05:17:40 +03:00
|
|
|
templater,
|
2016-06-04 12:08:00 +03:00
|
|
|
util,
|
2015-08-09 05:17:40 +03:00
|
|
|
)
|
|
|
|
|
2016-06-04 12:08:00 +03:00
|
|
|
pickle = util.pickle
|
|
|
|
|
2012-02-21 02:42:47 +04:00
|
|
|
class baseformatter(object):
|
|
|
|
def __init__(self, ui, topic, opts):
|
|
|
|
self._ui = ui
|
|
|
|
self._topic = topic
|
|
|
|
self._style = opts.get("style")
|
|
|
|
self._template = opts.get("template")
|
|
|
|
self._item = None
|
2014-10-03 17:20:02 +04:00
|
|
|
# function to convert node to string suitable for this output
|
|
|
|
self.hexfunc = hex
|
2014-09-16 19:31:03 +04:00
|
|
|
def __nonzero__(self):
|
2012-02-21 02:42:47 +04:00
|
|
|
'''return False if we're not doing real templating so we can
|
|
|
|
skip extra work'''
|
|
|
|
return True
|
|
|
|
def _showitem(self):
|
|
|
|
'''show a formatted item once all data is collected'''
|
|
|
|
pass
|
|
|
|
def startitem(self):
|
|
|
|
'''begin an item in the format list'''
|
|
|
|
if self._item is not None:
|
|
|
|
self._showitem()
|
|
|
|
self._item = {}
|
formatter: add function to convert list to appropriate format (issue5217)
Before, it wasn't possible for formatter to handle array structure other
than date tuple. We've discussed that at the last sprint, which ended we
would probably want to allow only templatable data structure, i.e. a list
of dicts:
data(tags=[{'tag': a}, {'tag': b}, ...])
Unfortunately, it turned out not working well with template functions:
"{ifcontains(a, tags, ...)}"
^^^^^^^^^^^^^^^^^^
"a in tags", where tags should be a plain list/set of tags
So the formatter must at least know if the type [{}] was constructed from
a plain list or was actually a list of dicts.
This patch introduces new explicit interface to convert an array structure
to an appropriate data type for the current formatter, which can be used
as follows:
fm.write('tags', _('tags: %s\n'), fm.formatlist(tags, name='tag'))
No separate fm.data() call should be necessary.
2016-07-10 15:03:06 +03:00
|
|
|
@staticmethod
|
|
|
|
def formatlist(data, name, fmt='%s', sep=' '):
|
|
|
|
'''convert iterable to appropriate list format'''
|
|
|
|
return list(data)
|
2012-02-21 02:42:47 +04:00
|
|
|
def data(self, **data):
|
|
|
|
'''insert data into item that's not shown in default output'''
|
2012-09-21 07:30:59 +04:00
|
|
|
self._item.update(data)
|
2012-02-21 02:42:47 +04:00
|
|
|
def write(self, fields, deftext, *fielddata, **opts):
|
|
|
|
'''do default text output while assigning data to item'''
|
2015-09-23 15:51:48 +03:00
|
|
|
fieldkeys = fields.split()
|
|
|
|
assert len(fieldkeys) == len(fielddata)
|
2015-09-23 15:54:47 +03:00
|
|
|
self._item.update(zip(fieldkeys, fielddata))
|
2012-11-03 23:37:50 +04:00
|
|
|
def condwrite(self, cond, fields, deftext, *fielddata, **opts):
|
|
|
|
'''do conditional write (primarily for plain formatter)'''
|
2015-09-23 15:51:48 +03:00
|
|
|
fieldkeys = fields.split()
|
|
|
|
assert len(fieldkeys) == len(fielddata)
|
2015-09-23 15:54:47 +03:00
|
|
|
self._item.update(zip(fieldkeys, fielddata))
|
2012-02-21 02:42:47 +04:00
|
|
|
def plain(self, text, **opts):
|
|
|
|
'''show raw text for non-templated mode'''
|
|
|
|
pass
|
|
|
|
def end(self):
|
|
|
|
'''end output for the formatter'''
|
|
|
|
if self._item is not None:
|
|
|
|
self._showitem()
|
|
|
|
|
|
|
|
class plainformatter(baseformatter):
|
|
|
|
'''the default text output scheme'''
|
|
|
|
def __init__(self, ui, topic, opts):
|
|
|
|
baseformatter.__init__(self, ui, topic, opts)
|
2014-10-03 17:20:02 +04:00
|
|
|
if ui.debugflag:
|
|
|
|
self.hexfunc = hex
|
|
|
|
else:
|
|
|
|
self.hexfunc = short
|
2014-09-16 19:31:03 +04:00
|
|
|
def __nonzero__(self):
|
2012-02-21 02:42:47 +04:00
|
|
|
return False
|
|
|
|
def startitem(self):
|
|
|
|
pass
|
formatter: add function to convert list to appropriate format (issue5217)
Before, it wasn't possible for formatter to handle array structure other
than date tuple. We've discussed that at the last sprint, which ended we
would probably want to allow only templatable data structure, i.e. a list
of dicts:
data(tags=[{'tag': a}, {'tag': b}, ...])
Unfortunately, it turned out not working well with template functions:
"{ifcontains(a, tags, ...)}"
^^^^^^^^^^^^^^^^^^
"a in tags", where tags should be a plain list/set of tags
So the formatter must at least know if the type [{}] was constructed from
a plain list or was actually a list of dicts.
This patch introduces new explicit interface to convert an array structure
to an appropriate data type for the current formatter, which can be used
as follows:
fm.write('tags', _('tags: %s\n'), fm.formatlist(tags, name='tag'))
No separate fm.data() call should be necessary.
2016-07-10 15:03:06 +03:00
|
|
|
@staticmethod
|
|
|
|
def formatlist(data, name, fmt='%s', sep=' '):
|
|
|
|
'''stringify iterable separated by sep'''
|
|
|
|
return sep.join(fmt % e for e in data)
|
2012-02-21 02:42:47 +04:00
|
|
|
def data(self, **data):
|
|
|
|
pass
|
|
|
|
def write(self, fields, deftext, *fielddata, **opts):
|
|
|
|
self._ui.write(deftext % fielddata, **opts)
|
2012-11-03 23:37:50 +04:00
|
|
|
def condwrite(self, cond, fields, deftext, *fielddata, **opts):
|
|
|
|
'''do conditional write'''
|
|
|
|
if cond:
|
|
|
|
self._ui.write(deftext % fielddata, **opts)
|
2012-02-21 02:42:47 +04:00
|
|
|
def plain(self, text, **opts):
|
|
|
|
self._ui.write(text, **opts)
|
|
|
|
def end(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class debugformatter(baseformatter):
|
|
|
|
def __init__(self, ui, topic, opts):
|
|
|
|
baseformatter.__init__(self, ui, topic, opts)
|
2014-09-13 04:06:11 +04:00
|
|
|
self._ui.write("%s = [\n" % self._topic)
|
2012-02-21 02:42:47 +04:00
|
|
|
def _showitem(self):
|
|
|
|
self._ui.write(" " + repr(self._item) + ",\n")
|
|
|
|
def end(self):
|
|
|
|
baseformatter.end(self)
|
2014-09-13 04:06:11 +04:00
|
|
|
self._ui.write("]\n")
|
2012-02-21 02:42:47 +04:00
|
|
|
|
2014-09-13 03:36:38 +04:00
|
|
|
class pickleformatter(baseformatter):
|
|
|
|
def __init__(self, ui, topic, opts):
|
|
|
|
baseformatter.__init__(self, ui, topic, opts)
|
|
|
|
self._data = []
|
|
|
|
def _showitem(self):
|
|
|
|
self._data.append(self._item)
|
|
|
|
def end(self):
|
|
|
|
baseformatter.end(self)
|
2016-06-04 12:08:00 +03:00
|
|
|
self._ui.write(pickle.dumps(self._data))
|
2014-09-13 03:36:38 +04:00
|
|
|
|
2014-09-17 16:15:43 +04:00
|
|
|
def _jsonifyobj(v):
|
formatter: add function to convert list to appropriate format (issue5217)
Before, it wasn't possible for formatter to handle array structure other
than date tuple. We've discussed that at the last sprint, which ended we
would probably want to allow only templatable data structure, i.e. a list
of dicts:
data(tags=[{'tag': a}, {'tag': b}, ...])
Unfortunately, it turned out not working well with template functions:
"{ifcontains(a, tags, ...)}"
^^^^^^^^^^^^^^^^^^
"a in tags", where tags should be a plain list/set of tags
So the formatter must at least know if the type [{}] was constructed from
a plain list or was actually a list of dicts.
This patch introduces new explicit interface to convert an array structure
to an appropriate data type for the current formatter, which can be used
as follows:
fm.write('tags', _('tags: %s\n'), fm.formatlist(tags, name='tag'))
No separate fm.data() call should be necessary.
2016-07-10 15:03:06 +03:00
|
|
|
if isinstance(v, (list, tuple)):
|
2014-09-17 16:30:22 +04:00
|
|
|
return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
|
2014-09-17 17:34:34 +04:00
|
|
|
elif v is None:
|
|
|
|
return 'null'
|
2014-10-02 18:20:59 +04:00
|
|
|
elif v is True:
|
|
|
|
return 'true'
|
|
|
|
elif v is False:
|
|
|
|
return 'false'
|
2014-09-17 17:21:01 +04:00
|
|
|
elif isinstance(v, (int, float)):
|
|
|
|
return str(v)
|
2014-09-17 16:15:43 +04:00
|
|
|
else:
|
|
|
|
return '"%s"' % encoding.jsonescape(v)
|
|
|
|
|
2014-09-13 03:29:29 +04:00
|
|
|
class jsonformatter(baseformatter):
|
|
|
|
def __init__(self, ui, topic, opts):
|
|
|
|
baseformatter.__init__(self, ui, topic, opts)
|
|
|
|
self._ui.write("[")
|
|
|
|
self._ui._first = True
|
|
|
|
def _showitem(self):
|
|
|
|
if self._ui._first:
|
|
|
|
self._ui._first = False
|
|
|
|
else:
|
|
|
|
self._ui.write(",")
|
|
|
|
|
|
|
|
self._ui.write("\n {\n")
|
|
|
|
first = True
|
|
|
|
for k, v in sorted(self._item.items()):
|
|
|
|
if first:
|
|
|
|
first = False
|
|
|
|
else:
|
|
|
|
self._ui.write(",\n")
|
2014-09-17 16:15:43 +04:00
|
|
|
self._ui.write(' "%s": %s' % (k, _jsonifyobj(v)))
|
2014-09-13 03:29:29 +04:00
|
|
|
self._ui.write("\n }")
|
|
|
|
def end(self):
|
|
|
|
baseformatter.end(self)
|
|
|
|
self._ui.write("\n]\n")
|
|
|
|
|
2015-06-10 22:33:38 +03:00
|
|
|
class templateformatter(baseformatter):
|
|
|
|
def __init__(self, ui, topic, opts):
|
|
|
|
baseformatter.__init__(self, ui, topic, opts)
|
|
|
|
self._topic = topic
|
|
|
|
self._t = gettemplater(ui, topic, opts.get('template', ''))
|
|
|
|
def _showitem(self):
|
2016-03-08 15:08:33 +03:00
|
|
|
g = self._t(self._topic, ui=self._ui, **self._item)
|
2015-06-10 22:33:38 +03:00
|
|
|
self._ui.write(templater.stringify(g))
|
formatter: add function to convert list to appropriate format (issue5217)
Before, it wasn't possible for formatter to handle array structure other
than date tuple. We've discussed that at the last sprint, which ended we
would probably want to allow only templatable data structure, i.e. a list
of dicts:
data(tags=[{'tag': a}, {'tag': b}, ...])
Unfortunately, it turned out not working well with template functions:
"{ifcontains(a, tags, ...)}"
^^^^^^^^^^^^^^^^^^
"a in tags", where tags should be a plain list/set of tags
So the formatter must at least know if the type [{}] was constructed from
a plain list or was actually a list of dicts.
This patch introduces new explicit interface to convert an array structure
to an appropriate data type for the current formatter, which can be used
as follows:
fm.write('tags', _('tags: %s\n'), fm.formatlist(tags, name='tag'))
No separate fm.data() call should be necessary.
2016-07-10 15:03:06 +03:00
|
|
|
@staticmethod
|
|
|
|
def formatlist(data, name, fmt='%s', sep=' '):
|
|
|
|
'''build object that can be evaluated as either plain string or list'''
|
|
|
|
# name is mandatory argument for now, but it could be optional if
|
|
|
|
# we have default template keyword, e.g. {item}
|
|
|
|
data = list(data)
|
|
|
|
def f():
|
|
|
|
yield plainformatter.formatlist(data, name, fmt, sep)
|
|
|
|
return templatekw._hybrid(f(), data, lambda x: {name: x},
|
|
|
|
lambda d: fmt % d[name])
|
2015-06-10 22:33:38 +03:00
|
|
|
|
2015-06-10 22:29:13 +03:00
|
|
|
def lookuptemplate(ui, topic, tmpl):
|
|
|
|
# looks like a literal template?
|
|
|
|
if '{' in tmpl:
|
|
|
|
return tmpl, None
|
|
|
|
|
|
|
|
# perhaps a stock style?
|
|
|
|
if not os.path.split(tmpl)[0]:
|
|
|
|
mapname = (templater.templatepath('map-cmdline.' + tmpl)
|
|
|
|
or templater.templatepath(tmpl))
|
|
|
|
if mapname and os.path.isfile(mapname):
|
|
|
|
return None, mapname
|
|
|
|
|
|
|
|
# perhaps it's a reference to [templates]
|
|
|
|
t = ui.config('templates', tmpl)
|
|
|
|
if t:
|
2016-03-26 12:12:12 +03:00
|
|
|
return templater.unquotestring(t), None
|
2015-06-10 22:29:13 +03:00
|
|
|
|
|
|
|
if tmpl == 'list':
|
|
|
|
ui.write(_("available styles: %s\n") % templater.stylelist())
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_("specify a template"))
|
2015-06-10 22:29:13 +03:00
|
|
|
|
|
|
|
# perhaps it's a path to a map or a template
|
|
|
|
if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl):
|
|
|
|
# is it a mapfile for a style?
|
|
|
|
if os.path.basename(tmpl).startswith("map-"):
|
|
|
|
return None, os.path.realpath(tmpl)
|
|
|
|
tmpl = open(tmpl).read()
|
|
|
|
return tmpl, None
|
|
|
|
|
|
|
|
# constant string?
|
|
|
|
return tmpl, None
|
|
|
|
|
2015-06-10 22:30:18 +03:00
|
|
|
def gettemplater(ui, topic, spec):
|
|
|
|
tmpl, mapfile = lookuptemplate(ui, topic, spec)
|
2016-04-03 17:26:48 +03:00
|
|
|
assert not (tmpl and mapfile)
|
|
|
|
if mapfile:
|
|
|
|
return templater.templater.frommapfile(mapfile)
|
2016-04-10 11:23:09 +03:00
|
|
|
return maketemplater(ui, topic, tmpl)
|
|
|
|
|
|
|
|
def maketemplater(ui, topic, tmpl, filters=None, cache=None):
|
|
|
|
"""Create a templater from a string template 'tmpl'"""
|
2016-03-27 14:59:36 +03:00
|
|
|
aliases = ui.configitems('templatealias')
|
|
|
|
t = templater.templater(filters=filters, cache=cache, aliases=aliases)
|
2015-06-10 22:30:18 +03:00
|
|
|
if tmpl:
|
|
|
|
t.cache[topic] = tmpl
|
|
|
|
return t
|
|
|
|
|
2012-02-21 02:42:47 +04:00
|
|
|
def formatter(ui, topic, opts):
|
2014-09-13 03:29:29 +04:00
|
|
|
template = opts.get("template", "")
|
|
|
|
if template == "json":
|
|
|
|
return jsonformatter(ui, topic, opts)
|
2014-09-13 03:36:38 +04:00
|
|
|
elif template == "pickle":
|
|
|
|
return pickleformatter(ui, topic, opts)
|
2014-09-13 03:29:29 +04:00
|
|
|
elif template == "debug":
|
|
|
|
return debugformatter(ui, topic, opts)
|
|
|
|
elif template != "":
|
2015-06-10 22:33:38 +03:00
|
|
|
return templateformatter(ui, topic, opts)
|
2015-06-26 01:49:11 +03:00
|
|
|
# developer config: ui.formatdebug
|
2014-09-13 03:29:29 +04:00
|
|
|
elif ui.configbool('ui', 'formatdebug'):
|
2012-02-21 02:42:47 +04:00
|
|
|
return debugformatter(ui, topic, opts)
|
2015-06-26 01:49:11 +03:00
|
|
|
# deprecated config: ui.formatjson
|
2014-09-13 03:29:29 +04:00
|
|
|
elif ui.configbool('ui', 'formatjson'):
|
|
|
|
return jsonformatter(ui, topic, opts)
|
2012-02-21 02:42:47 +04:00
|
|
|
return plainformatter(ui, topic, opts)
|