##// END OF EJS Templates
formatter: add json formatter
Matt Mackall -
r22428:427e80a1 default
parent child Browse files
Show More
@@ -1,80 +1,119
1 1 # formatter.py - generic output formatting for mercurial
2 2 #
3 3 # Copyright 2012 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 from i18n import _
9 import encoding, util
10
8 11 class baseformatter(object):
9 12 def __init__(self, ui, topic, opts):
10 13 self._ui = ui
11 14 self._topic = topic
12 15 self._style = opts.get("style")
13 16 self._template = opts.get("template")
14 17 self._item = None
15 18 def __bool__(self):
16 19 '''return False if we're not doing real templating so we can
17 20 skip extra work'''
18 21 return True
19 22 def _showitem(self):
20 23 '''show a formatted item once all data is collected'''
21 24 pass
22 25 def startitem(self):
23 26 '''begin an item in the format list'''
24 27 if self._item is not None:
25 28 self._showitem()
26 29 self._item = {}
27 30 def data(self, **data):
28 31 '''insert data into item that's not shown in default output'''
29 32 self._item.update(data)
30 33 def write(self, fields, deftext, *fielddata, **opts):
31 34 '''do default text output while assigning data to item'''
32 35 for k, v in zip(fields.split(), fielddata):
33 36 self._item[k] = v
34 37 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
35 38 '''do conditional write (primarily for plain formatter)'''
36 39 for k, v in zip(fields.split(), fielddata):
37 40 self._item[k] = v
38 41 def plain(self, text, **opts):
39 42 '''show raw text for non-templated mode'''
40 43 pass
41 44 def end(self):
42 45 '''end output for the formatter'''
43 46 if self._item is not None:
44 47 self._showitem()
45 48
46 49 class plainformatter(baseformatter):
47 50 '''the default text output scheme'''
48 51 def __init__(self, ui, topic, opts):
49 52 baseformatter.__init__(self, ui, topic, opts)
50 53 def __bool__(self):
51 54 return False
52 55 def startitem(self):
53 56 pass
54 57 def data(self, **data):
55 58 pass
56 59 def write(self, fields, deftext, *fielddata, **opts):
57 60 self._ui.write(deftext % fielddata, **opts)
58 61 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
59 62 '''do conditional write'''
60 63 if cond:
61 64 self._ui.write(deftext % fielddata, **opts)
62 65 def plain(self, text, **opts):
63 66 self._ui.write(text, **opts)
64 67 def end(self):
65 68 pass
66 69
67 70 class debugformatter(baseformatter):
68 71 def __init__(self, ui, topic, opts):
69 72 baseformatter.__init__(self, ui, topic, opts)
70 73 self._ui.write("%s = [\n" % self._topic)
71 74 def _showitem(self):
72 75 self._ui.write(" " + repr(self._item) + ",\n")
73 76 def end(self):
74 77 baseformatter.end(self)
75 78 self._ui.write("]\n")
76 79
80 class jsonformatter(baseformatter):
81 def __init__(self, ui, topic, opts):
82 baseformatter.__init__(self, ui, topic, opts)
83 self._ui.write("[")
84 self._ui._first = True
85 def _showitem(self):
86 if self._ui._first:
87 self._ui._first = False
88 else:
89 self._ui.write(",")
90
91 self._ui.write("\n {\n")
92 first = True
93 for k, v in sorted(self._item.items()):
94 if first:
95 first = False
96 else:
97 self._ui.write(",\n")
98 if isinstance(v, int):
99 self._ui.write(' "%s": %d' % (k, v))
100 else:
101 self._ui.write(' "%s": "%s"' % (k, encoding.jsonescape(v)))
102 self._ui.write("\n }")
103 def end(self):
104 baseformatter.end(self)
105 self._ui.write("\n]\n")
106
77 107 def formatter(ui, topic, opts):
78 if ui.configbool('ui', 'formatdebug'):
108 template = opts.get("template", "")
109 if template == "json":
110 return jsonformatter(ui, topic, opts)
111 elif template == "debug":
79 112 return debugformatter(ui, topic, opts)
113 elif template != "":
114 raise util.Abort(_("custom templates not yet supported"))
115 elif ui.configbool('ui', 'formatdebug'):
116 return debugformatter(ui, topic, opts)
117 elif ui.configbool('ui', 'formatjson'):
118 return jsonformatter(ui, topic, opts)
80 119 return plainformatter(ui, topic, opts)
General Comments 0
You need to be logged in to leave comments. Login now