##// END OF EJS Templates
formatter: convert None to json null...
Yuya Nishihara -
r24321:0a714a1f default
parent child Browse files
Show More
@@ -1,148 +1,150 b''
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 8 import cPickle
9 9 from node import hex, short
10 10 from i18n import _
11 11 import encoding, util
12 12
13 13 class baseformatter(object):
14 14 def __init__(self, ui, topic, opts):
15 15 self._ui = ui
16 16 self._topic = topic
17 17 self._style = opts.get("style")
18 18 self._template = opts.get("template")
19 19 self._item = None
20 20 # function to convert node to string suitable for this output
21 21 self.hexfunc = hex
22 22 def __nonzero__(self):
23 23 '''return False if we're not doing real templating so we can
24 24 skip extra work'''
25 25 return True
26 26 def _showitem(self):
27 27 '''show a formatted item once all data is collected'''
28 28 pass
29 29 def startitem(self):
30 30 '''begin an item in the format list'''
31 31 if self._item is not None:
32 32 self._showitem()
33 33 self._item = {}
34 34 def data(self, **data):
35 35 '''insert data into item that's not shown in default output'''
36 36 self._item.update(data)
37 37 def write(self, fields, deftext, *fielddata, **opts):
38 38 '''do default text output while assigning data to item'''
39 39 for k, v in zip(fields.split(), fielddata):
40 40 self._item[k] = v
41 41 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
42 42 '''do conditional write (primarily for plain formatter)'''
43 43 for k, v in zip(fields.split(), fielddata):
44 44 self._item[k] = v
45 45 def plain(self, text, **opts):
46 46 '''show raw text for non-templated mode'''
47 47 pass
48 48 def end(self):
49 49 '''end output for the formatter'''
50 50 if self._item is not None:
51 51 self._showitem()
52 52
53 53 class plainformatter(baseformatter):
54 54 '''the default text output scheme'''
55 55 def __init__(self, ui, topic, opts):
56 56 baseformatter.__init__(self, ui, topic, opts)
57 57 if ui.debugflag:
58 58 self.hexfunc = hex
59 59 else:
60 60 self.hexfunc = short
61 61 def __nonzero__(self):
62 62 return False
63 63 def startitem(self):
64 64 pass
65 65 def data(self, **data):
66 66 pass
67 67 def write(self, fields, deftext, *fielddata, **opts):
68 68 self._ui.write(deftext % fielddata, **opts)
69 69 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
70 70 '''do conditional write'''
71 71 if cond:
72 72 self._ui.write(deftext % fielddata, **opts)
73 73 def plain(self, text, **opts):
74 74 self._ui.write(text, **opts)
75 75 def end(self):
76 76 pass
77 77
78 78 class debugformatter(baseformatter):
79 79 def __init__(self, ui, topic, opts):
80 80 baseformatter.__init__(self, ui, topic, opts)
81 81 self._ui.write("%s = [\n" % self._topic)
82 82 def _showitem(self):
83 83 self._ui.write(" " + repr(self._item) + ",\n")
84 84 def end(self):
85 85 baseformatter.end(self)
86 86 self._ui.write("]\n")
87 87
88 88 class pickleformatter(baseformatter):
89 89 def __init__(self, ui, topic, opts):
90 90 baseformatter.__init__(self, ui, topic, opts)
91 91 self._data = []
92 92 def _showitem(self):
93 93 self._data.append(self._item)
94 94 def end(self):
95 95 baseformatter.end(self)
96 96 self._ui.write(cPickle.dumps(self._data))
97 97
98 98 def _jsonifyobj(v):
99 99 if isinstance(v, tuple):
100 100 return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
101 elif v is None:
102 return 'null'
101 103 elif v is True:
102 104 return 'true'
103 105 elif v is False:
104 106 return 'false'
105 107 elif isinstance(v, (int, float)):
106 108 return str(v)
107 109 else:
108 110 return '"%s"' % encoding.jsonescape(v)
109 111
110 112 class jsonformatter(baseformatter):
111 113 def __init__(self, ui, topic, opts):
112 114 baseformatter.__init__(self, ui, topic, opts)
113 115 self._ui.write("[")
114 116 self._ui._first = True
115 117 def _showitem(self):
116 118 if self._ui._first:
117 119 self._ui._first = False
118 120 else:
119 121 self._ui.write(",")
120 122
121 123 self._ui.write("\n {\n")
122 124 first = True
123 125 for k, v in sorted(self._item.items()):
124 126 if first:
125 127 first = False
126 128 else:
127 129 self._ui.write(",\n")
128 130 self._ui.write(' "%s": %s' % (k, _jsonifyobj(v)))
129 131 self._ui.write("\n }")
130 132 def end(self):
131 133 baseformatter.end(self)
132 134 self._ui.write("\n]\n")
133 135
134 136 def formatter(ui, topic, opts):
135 137 template = opts.get("template", "")
136 138 if template == "json":
137 139 return jsonformatter(ui, topic, opts)
138 140 elif template == "pickle":
139 141 return pickleformatter(ui, topic, opts)
140 142 elif template == "debug":
141 143 return debugformatter(ui, topic, opts)
142 144 elif template != "":
143 145 raise util.Abort(_("custom templates not yet supported"))
144 146 elif ui.configbool('ui', 'formatdebug'):
145 147 return debugformatter(ui, topic, opts)
146 148 elif ui.configbool('ui', 'formatjson'):
147 149 return jsonformatter(ui, topic, opts)
148 150 return plainformatter(ui, topic, opts)
General Comments 0
You need to be logged in to leave comments. Login now