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