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