Show More
@@ -1,439 +1,443 | |||||
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 | """Generic output formatting for Mercurial |
|
8 | """Generic output formatting for Mercurial | |
9 |
|
9 | |||
10 | The formatter provides API to show data in various ways. The following |
|
10 | The formatter provides API to show data in various ways. The following | |
11 | functions should be used in place of ui.write(): |
|
11 | functions should be used in place of ui.write(): | |
12 |
|
12 | |||
13 | - fm.write() for unconditional output |
|
13 | - fm.write() for unconditional output | |
14 | - fm.condwrite() to show some extra data conditionally in plain output |
|
14 | - fm.condwrite() to show some extra data conditionally in plain output | |
15 | - fm.context() to provide changectx to template output |
|
15 | - fm.context() to provide changectx to template output | |
16 | - fm.data() to provide extra data to JSON or template output |
|
16 | - fm.data() to provide extra data to JSON or template output | |
17 | - fm.plain() to show raw text that isn't provided to JSON or template output |
|
17 | - fm.plain() to show raw text that isn't provided to JSON or template output | |
18 |
|
18 | |||
19 | To show structured data (e.g. date tuples, dicts, lists), apply fm.format*() |
|
19 | To show structured data (e.g. date tuples, dicts, lists), apply fm.format*() | |
20 | beforehand so the data is converted to the appropriate data type. Use |
|
20 | beforehand so the data is converted to the appropriate data type. Use | |
21 | fm.isplain() if you need to convert or format data conditionally which isn't |
|
21 | fm.isplain() if you need to convert or format data conditionally which isn't | |
22 | supported by the formatter API. |
|
22 | supported by the formatter API. | |
23 |
|
23 | |||
24 | To build nested structure (i.e. a list of dicts), use fm.nested(). |
|
24 | To build nested structure (i.e. a list of dicts), use fm.nested(). | |
25 |
|
25 | |||
26 | See also https://www.mercurial-scm.org/wiki/GenericTemplatingPlan |
|
26 | See also https://www.mercurial-scm.org/wiki/GenericTemplatingPlan | |
27 |
|
27 | |||
28 | fm.condwrite() vs 'if cond:': |
|
28 | fm.condwrite() vs 'if cond:': | |
29 |
|
29 | |||
30 | In most cases, use fm.condwrite() so users can selectively show the data |
|
30 | In most cases, use fm.condwrite() so users can selectively show the data | |
31 | in template output. If it's costly to build data, use plain 'if cond:' with |
|
31 | in template output. If it's costly to build data, use plain 'if cond:' with | |
32 | fm.write(). |
|
32 | fm.write(). | |
33 |
|
33 | |||
34 | fm.nested() vs fm.formatdict() (or fm.formatlist()): |
|
34 | fm.nested() vs fm.formatdict() (or fm.formatlist()): | |
35 |
|
35 | |||
36 | fm.nested() should be used to form a tree structure (a list of dicts of |
|
36 | fm.nested() should be used to form a tree structure (a list of dicts of | |
37 | lists of dicts...) which can be accessed through template keywords, e.g. |
|
37 | lists of dicts...) which can be accessed through template keywords, e.g. | |
38 | "{foo % "{bar % {...}} {baz % {...}}"}". On the other hand, fm.formatdict() |
|
38 | "{foo % "{bar % {...}} {baz % {...}}"}". On the other hand, fm.formatdict() | |
39 | exports a dict-type object to template, which can be accessed by e.g. |
|
39 | exports a dict-type object to template, which can be accessed by e.g. | |
40 | "{get(foo, key)}" function. |
|
40 | "{get(foo, key)}" function. | |
41 |
|
41 | |||
42 | Doctest helper: |
|
42 | Doctest helper: | |
43 |
|
43 | |||
44 | >>> def show(fn, verbose=False, **opts): |
|
44 | >>> def show(fn, verbose=False, **opts): | |
45 | ... import sys |
|
45 | ... import sys | |
46 | ... from . import ui as uimod |
|
46 | ... from . import ui as uimod | |
47 | ... ui = uimod.ui() |
|
47 | ... ui = uimod.ui() | |
48 | ... ui.fout = sys.stdout # redirect to doctest |
|
48 | ... ui.fout = sys.stdout # redirect to doctest | |
49 | ... ui.verbose = verbose |
|
49 | ... ui.verbose = verbose | |
50 | ... return fn(ui, ui.formatter(fn.__name__, opts)) |
|
50 | ... return fn(ui, ui.formatter(fn.__name__, opts)) | |
51 |
|
51 | |||
52 | Basic example: |
|
52 | Basic example: | |
53 |
|
53 | |||
54 | >>> def files(ui, fm): |
|
54 | >>> def files(ui, fm): | |
55 | ... files = [('foo', 123, (0, 0)), ('bar', 456, (1, 0))] |
|
55 | ... files = [('foo', 123, (0, 0)), ('bar', 456, (1, 0))] | |
56 | ... for f in files: |
|
56 | ... for f in files: | |
57 | ... fm.startitem() |
|
57 | ... fm.startitem() | |
58 | ... fm.write('path', '%s', f[0]) |
|
58 | ... fm.write('path', '%s', f[0]) | |
59 | ... fm.condwrite(ui.verbose, 'date', ' %s', |
|
59 | ... fm.condwrite(ui.verbose, 'date', ' %s', | |
60 | ... fm.formatdate(f[2], '%Y-%m-%d %H:%M:%S')) |
|
60 | ... fm.formatdate(f[2], '%Y-%m-%d %H:%M:%S')) | |
61 | ... fm.data(size=f[1]) |
|
61 | ... fm.data(size=f[1]) | |
62 | ... fm.plain('\\n') |
|
62 | ... fm.plain('\\n') | |
63 | ... fm.end() |
|
63 | ... fm.end() | |
64 | >>> show(files) |
|
64 | >>> show(files) | |
65 | foo |
|
65 | foo | |
66 | bar |
|
66 | bar | |
67 | >>> show(files, verbose=True) |
|
67 | >>> show(files, verbose=True) | |
68 | foo 1970-01-01 00:00:00 |
|
68 | foo 1970-01-01 00:00:00 | |
69 | bar 1970-01-01 00:00:01 |
|
69 | bar 1970-01-01 00:00:01 | |
70 | >>> show(files, template='json') |
|
70 | >>> show(files, template='json') | |
71 | [ |
|
71 | [ | |
72 | { |
|
72 | { | |
73 | "date": [0, 0], |
|
73 | "date": [0, 0], | |
74 | "path": "foo", |
|
74 | "path": "foo", | |
75 | "size": 123 |
|
75 | "size": 123 | |
76 | }, |
|
76 | }, | |
77 | { |
|
77 | { | |
78 | "date": [1, 0], |
|
78 | "date": [1, 0], | |
79 | "path": "bar", |
|
79 | "path": "bar", | |
80 | "size": 456 |
|
80 | "size": 456 | |
81 | } |
|
81 | } | |
82 | ] |
|
82 | ] | |
83 | >>> show(files, template='path: {path}\\ndate: {date|rfc3339date}\\n') |
|
83 | >>> show(files, template='path: {path}\\ndate: {date|rfc3339date}\\n') | |
84 | path: foo |
|
84 | path: foo | |
85 | date: 1970-01-01T00:00:00+00:00 |
|
85 | date: 1970-01-01T00:00:00+00:00 | |
86 | path: bar |
|
86 | path: bar | |
87 | date: 1970-01-01T00:00:01+00:00 |
|
87 | date: 1970-01-01T00:00:01+00:00 | |
88 |
|
88 | |||
89 | Nested example: |
|
89 | Nested example: | |
90 |
|
90 | |||
91 | >>> def subrepos(ui, fm): |
|
91 | >>> def subrepos(ui, fm): | |
92 | ... fm.startitem() |
|
92 | ... fm.startitem() | |
93 | ... fm.write('repo', '[%s]\\n', 'baz') |
|
93 | ... fm.write('repo', '[%s]\\n', 'baz') | |
94 | ... files(ui, fm.nested('files')) |
|
94 | ... files(ui, fm.nested('files')) | |
95 | ... fm.end() |
|
95 | ... fm.end() | |
96 | >>> show(subrepos) |
|
96 | >>> show(subrepos) | |
97 | [baz] |
|
97 | [baz] | |
98 | foo |
|
98 | foo | |
99 | bar |
|
99 | bar | |
100 | >>> show(subrepos, template='{repo}: {join(files % "{path}", ", ")}\\n') |
|
100 | >>> show(subrepos, template='{repo}: {join(files % "{path}", ", ")}\\n') | |
101 | baz: foo, bar |
|
101 | baz: foo, bar | |
102 | """ |
|
102 | """ | |
103 |
|
103 | |||
104 | from __future__ import absolute_import |
|
104 | from __future__ import absolute_import | |
105 |
|
105 | |||
106 | import os |
|
106 | import os | |
107 |
|
107 | |||
108 | from .i18n import _ |
|
108 | from .i18n import _ | |
109 | from .node import ( |
|
109 | from .node import ( | |
110 | hex, |
|
110 | hex, | |
111 | short, |
|
111 | short, | |
112 | ) |
|
112 | ) | |
113 |
|
113 | |||
114 | from . import ( |
|
114 | from . import ( | |
115 | encoding, |
|
115 | encoding, | |
116 | error, |
|
116 | error, | |
117 | templatekw, |
|
117 | templatekw, | |
118 | templater, |
|
118 | templater, | |
119 | util, |
|
119 | util, | |
120 | ) |
|
120 | ) | |
121 |
|
121 | |||
122 | pickle = util.pickle |
|
122 | pickle = util.pickle | |
123 |
|
123 | |||
124 | class _nullconverter(object): |
|
124 | class _nullconverter(object): | |
125 | '''convert non-primitive data types to be processed by formatter''' |
|
125 | '''convert non-primitive data types to be processed by formatter''' | |
126 | @staticmethod |
|
126 | @staticmethod | |
127 | def formatdate(date, fmt): |
|
127 | def formatdate(date, fmt): | |
128 | '''convert date tuple to appropriate format''' |
|
128 | '''convert date tuple to appropriate format''' | |
129 | return date |
|
129 | return date | |
130 | @staticmethod |
|
130 | @staticmethod | |
131 | def formatdict(data, key, value, fmt, sep): |
|
131 | def formatdict(data, key, value, fmt, sep): | |
132 | '''convert dict or key-value pairs to appropriate dict format''' |
|
132 | '''convert dict or key-value pairs to appropriate dict format''' | |
133 | # use plain dict instead of util.sortdict so that data can be |
|
133 | # use plain dict instead of util.sortdict so that data can be | |
134 | # serialized as a builtin dict in pickle output |
|
134 | # serialized as a builtin dict in pickle output | |
135 | return dict(data) |
|
135 | return dict(data) | |
136 | @staticmethod |
|
136 | @staticmethod | |
137 | def formatlist(data, name, fmt, sep): |
|
137 | def formatlist(data, name, fmt, sep): | |
138 | '''convert iterable to appropriate list format''' |
|
138 | '''convert iterable to appropriate list format''' | |
139 | return list(data) |
|
139 | return list(data) | |
140 |
|
140 | |||
141 | class baseformatter(object): |
|
141 | class baseformatter(object): | |
142 | def __init__(self, ui, topic, opts, converter): |
|
142 | def __init__(self, ui, topic, opts, converter): | |
143 | self._ui = ui |
|
143 | self._ui = ui | |
144 | self._topic = topic |
|
144 | self._topic = topic | |
145 | self._style = opts.get("style") |
|
145 | self._style = opts.get("style") | |
146 | self._template = opts.get("template") |
|
146 | self._template = opts.get("template") | |
147 | self._converter = converter |
|
147 | self._converter = converter | |
148 | self._item = None |
|
148 | self._item = None | |
149 | # function to convert node to string suitable for this output |
|
149 | # function to convert node to string suitable for this output | |
150 | self.hexfunc = hex |
|
150 | self.hexfunc = hex | |
151 | def __enter__(self): |
|
151 | def __enter__(self): | |
152 | return self |
|
152 | return self | |
153 | def __exit__(self, exctype, excvalue, traceback): |
|
153 | def __exit__(self, exctype, excvalue, traceback): | |
154 | if exctype is None: |
|
154 | if exctype is None: | |
155 | self.end() |
|
155 | self.end() | |
156 | def _showitem(self): |
|
156 | def _showitem(self): | |
157 | '''show a formatted item once all data is collected''' |
|
157 | '''show a formatted item once all data is collected''' | |
158 | pass |
|
158 | pass | |
159 | def startitem(self): |
|
159 | def startitem(self): | |
160 | '''begin an item in the format list''' |
|
160 | '''begin an item in the format list''' | |
161 | if self._item is not None: |
|
161 | if self._item is not None: | |
162 | self._showitem() |
|
162 | self._showitem() | |
163 | self._item = {} |
|
163 | self._item = {} | |
164 | def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'): |
|
164 | def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'): | |
165 | '''convert date tuple to appropriate format''' |
|
165 | '''convert date tuple to appropriate format''' | |
166 | return self._converter.formatdate(date, fmt) |
|
166 | return self._converter.formatdate(date, fmt) | |
167 | def formatdict(self, data, key='key', value='value', fmt='%s=%s', sep=' '): |
|
167 | def formatdict(self, data, key='key', value='value', fmt='%s=%s', sep=' '): | |
168 | '''convert dict or key-value pairs to appropriate dict format''' |
|
168 | '''convert dict or key-value pairs to appropriate dict format''' | |
169 | return self._converter.formatdict(data, key, value, fmt, sep) |
|
169 | return self._converter.formatdict(data, key, value, fmt, sep) | |
170 | def formatlist(self, data, name, fmt='%s', sep=' '): |
|
170 | def formatlist(self, data, name, fmt='%s', sep=' '): | |
171 | '''convert iterable to appropriate list format''' |
|
171 | '''convert iterable to appropriate list format''' | |
172 | # name is mandatory argument for now, but it could be optional if |
|
172 | # name is mandatory argument for now, but it could be optional if | |
173 | # we have default template keyword, e.g. {item} |
|
173 | # we have default template keyword, e.g. {item} | |
174 | return self._converter.formatlist(data, name, fmt, sep) |
|
174 | return self._converter.formatlist(data, name, fmt, sep) | |
175 | def context(self, **ctxs): |
|
175 | def context(self, **ctxs): | |
176 | '''insert context objects to be used to render template keywords''' |
|
176 | '''insert context objects to be used to render template keywords''' | |
177 | pass |
|
177 | pass | |
178 | def data(self, **data): |
|
178 | def data(self, **data): | |
179 | '''insert data into item that's not shown in default output''' |
|
179 | '''insert data into item that's not shown in default output''' | |
180 | self._item.update(data) |
|
180 | self._item.update(data) | |
181 | def write(self, fields, deftext, *fielddata, **opts): |
|
181 | def write(self, fields, deftext, *fielddata, **opts): | |
182 | '''do default text output while assigning data to item''' |
|
182 | '''do default text output while assigning data to item''' | |
183 | fieldkeys = fields.split() |
|
183 | fieldkeys = fields.split() | |
184 | assert len(fieldkeys) == len(fielddata) |
|
184 | assert len(fieldkeys) == len(fielddata) | |
185 | self._item.update(zip(fieldkeys, fielddata)) |
|
185 | self._item.update(zip(fieldkeys, fielddata)) | |
186 | def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
|
186 | def condwrite(self, cond, fields, deftext, *fielddata, **opts): | |
187 | '''do conditional write (primarily for plain formatter)''' |
|
187 | '''do conditional write (primarily for plain formatter)''' | |
188 | fieldkeys = fields.split() |
|
188 | fieldkeys = fields.split() | |
189 | assert len(fieldkeys) == len(fielddata) |
|
189 | assert len(fieldkeys) == len(fielddata) | |
190 | self._item.update(zip(fieldkeys, fielddata)) |
|
190 | self._item.update(zip(fieldkeys, fielddata)) | |
191 | def plain(self, text, **opts): |
|
191 | def plain(self, text, **opts): | |
192 | '''show raw text for non-templated mode''' |
|
192 | '''show raw text for non-templated mode''' | |
193 | pass |
|
193 | pass | |
194 | def isplain(self): |
|
194 | def isplain(self): | |
195 | '''check for plain formatter usage''' |
|
195 | '''check for plain formatter usage''' | |
196 | return False |
|
196 | return False | |
197 | def nested(self, field): |
|
197 | def nested(self, field): | |
198 | '''sub formatter to store nested data in the specified field''' |
|
198 | '''sub formatter to store nested data in the specified field''' | |
199 | self._item[field] = data = [] |
|
199 | self._item[field] = data = [] | |
200 | return _nestedformatter(self._ui, self._converter, data) |
|
200 | return _nestedformatter(self._ui, self._converter, data) | |
201 | def end(self): |
|
201 | def end(self): | |
202 | '''end output for the formatter''' |
|
202 | '''end output for the formatter''' | |
203 | if self._item is not None: |
|
203 | if self._item is not None: | |
204 | self._showitem() |
|
204 | self._showitem() | |
205 |
|
205 | |||
206 | class _nestedformatter(baseformatter): |
|
206 | class _nestedformatter(baseformatter): | |
207 | '''build sub items and store them in the parent formatter''' |
|
207 | '''build sub items and store them in the parent formatter''' | |
208 | def __init__(self, ui, converter, data): |
|
208 | def __init__(self, ui, converter, data): | |
209 | baseformatter.__init__(self, ui, topic='', opts={}, converter=converter) |
|
209 | baseformatter.__init__(self, ui, topic='', opts={}, converter=converter) | |
210 | self._data = data |
|
210 | self._data = data | |
211 | def _showitem(self): |
|
211 | def _showitem(self): | |
212 | self._data.append(self._item) |
|
212 | self._data.append(self._item) | |
213 |
|
213 | |||
214 | def _iteritems(data): |
|
214 | def _iteritems(data): | |
215 | '''iterate key-value pairs in stable order''' |
|
215 | '''iterate key-value pairs in stable order''' | |
216 | if isinstance(data, dict): |
|
216 | if isinstance(data, dict): | |
217 | return sorted(data.iteritems()) |
|
217 | return sorted(data.iteritems()) | |
218 | return data |
|
218 | return data | |
219 |
|
219 | |||
220 | class _plainconverter(object): |
|
220 | class _plainconverter(object): | |
221 | '''convert non-primitive data types to text''' |
|
221 | '''convert non-primitive data types to text''' | |
222 | @staticmethod |
|
222 | @staticmethod | |
223 | def formatdate(date, fmt): |
|
223 | def formatdate(date, fmt): | |
224 | '''stringify date tuple in the given format''' |
|
224 | '''stringify date tuple in the given format''' | |
225 | return util.datestr(date, fmt) |
|
225 | return util.datestr(date, fmt) | |
226 | @staticmethod |
|
226 | @staticmethod | |
227 | def formatdict(data, key, value, fmt, sep): |
|
227 | def formatdict(data, key, value, fmt, sep): | |
228 | '''stringify key-value pairs separated by sep''' |
|
228 | '''stringify key-value pairs separated by sep''' | |
229 | return sep.join(fmt % (k, v) for k, v in _iteritems(data)) |
|
229 | return sep.join(fmt % (k, v) for k, v in _iteritems(data)) | |
230 | @staticmethod |
|
230 | @staticmethod | |
231 | def formatlist(data, name, fmt, sep): |
|
231 | def formatlist(data, name, fmt, sep): | |
232 | '''stringify iterable separated by sep''' |
|
232 | '''stringify iterable separated by sep''' | |
233 | return sep.join(fmt % e for e in data) |
|
233 | return sep.join(fmt % e for e in data) | |
234 |
|
234 | |||
235 | class plainformatter(baseformatter): |
|
235 | class plainformatter(baseformatter): | |
236 | '''the default text output scheme''' |
|
236 | '''the default text output scheme''' | |
237 | def __init__(self, ui, topic, opts): |
|
237 | def __init__(self, ui, topic, opts): | |
238 | baseformatter.__init__(self, ui, topic, opts, _plainconverter) |
|
238 | baseformatter.__init__(self, ui, topic, opts, _plainconverter) | |
239 | if ui.debugflag: |
|
239 | if ui.debugflag: | |
240 | self.hexfunc = hex |
|
240 | self.hexfunc = hex | |
241 | else: |
|
241 | else: | |
242 | self.hexfunc = short |
|
242 | self.hexfunc = short | |
243 | def startitem(self): |
|
243 | def startitem(self): | |
244 | pass |
|
244 | pass | |
245 | def data(self, **data): |
|
245 | def data(self, **data): | |
246 | pass |
|
246 | pass | |
247 | def write(self, fields, deftext, *fielddata, **opts): |
|
247 | def write(self, fields, deftext, *fielddata, **opts): | |
248 | self._ui.write(deftext % fielddata, **opts) |
|
248 | self._ui.write(deftext % fielddata, **opts) | |
249 | def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
|
249 | def condwrite(self, cond, fields, deftext, *fielddata, **opts): | |
250 | '''do conditional write''' |
|
250 | '''do conditional write''' | |
251 | if cond: |
|
251 | if cond: | |
252 | self._ui.write(deftext % fielddata, **opts) |
|
252 | self._ui.write(deftext % fielddata, **opts) | |
253 | def plain(self, text, **opts): |
|
253 | def plain(self, text, **opts): | |
254 | self._ui.write(text, **opts) |
|
254 | self._ui.write(text, **opts) | |
255 | def isplain(self): |
|
255 | def isplain(self): | |
256 | return True |
|
256 | return True | |
257 | def nested(self, field): |
|
257 | def nested(self, field): | |
258 | # nested data will be directly written to ui |
|
258 | # nested data will be directly written to ui | |
259 | return self |
|
259 | return self | |
260 | def end(self): |
|
260 | def end(self): | |
261 | pass |
|
261 | pass | |
262 |
|
262 | |||
263 | class debugformatter(baseformatter): |
|
263 | class debugformatter(baseformatter): | |
264 | def __init__(self, ui, topic, opts): |
|
264 | def __init__(self, ui, out, topic, opts): | |
265 | baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
|
265 | baseformatter.__init__(self, ui, topic, opts, _nullconverter) | |
266 | self._ui.write("%s = [\n" % self._topic) |
|
266 | self._out = out | |
|
267 | self._out.write("%s = [\n" % self._topic) | |||
267 | def _showitem(self): |
|
268 | def _showitem(self): | |
268 |
self._ |
|
269 | self._out.write(" " + repr(self._item) + ",\n") | |
269 | def end(self): |
|
270 | def end(self): | |
270 | baseformatter.end(self) |
|
271 | baseformatter.end(self) | |
271 |
self._ |
|
272 | self._out.write("]\n") | |
272 |
|
273 | |||
273 | class pickleformatter(baseformatter): |
|
274 | class pickleformatter(baseformatter): | |
274 | def __init__(self, ui, topic, opts): |
|
275 | def __init__(self, ui, out, topic, opts): | |
275 | baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
|
276 | baseformatter.__init__(self, ui, topic, opts, _nullconverter) | |
|
277 | self._out = out | |||
276 | self._data = [] |
|
278 | self._data = [] | |
277 | def _showitem(self): |
|
279 | def _showitem(self): | |
278 | self._data.append(self._item) |
|
280 | self._data.append(self._item) | |
279 | def end(self): |
|
281 | def end(self): | |
280 | baseformatter.end(self) |
|
282 | baseformatter.end(self) | |
281 |
self._ |
|
283 | self._out.write(pickle.dumps(self._data)) | |
282 |
|
284 | |||
283 | def _jsonifyobj(v): |
|
285 | def _jsonifyobj(v): | |
284 | if isinstance(v, dict): |
|
286 | if isinstance(v, dict): | |
285 | xs = ['"%s": %s' % (encoding.jsonescape(k), _jsonifyobj(u)) |
|
287 | xs = ['"%s": %s' % (encoding.jsonescape(k), _jsonifyobj(u)) | |
286 | for k, u in sorted(v.iteritems())] |
|
288 | for k, u in sorted(v.iteritems())] | |
287 | return '{' + ', '.join(xs) + '}' |
|
289 | return '{' + ', '.join(xs) + '}' | |
288 | elif isinstance(v, (list, tuple)): |
|
290 | elif isinstance(v, (list, tuple)): | |
289 | return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']' |
|
291 | return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']' | |
290 | elif v is None: |
|
292 | elif v is None: | |
291 | return 'null' |
|
293 | return 'null' | |
292 | elif v is True: |
|
294 | elif v is True: | |
293 | return 'true' |
|
295 | return 'true' | |
294 | elif v is False: |
|
296 | elif v is False: | |
295 | return 'false' |
|
297 | return 'false' | |
296 | elif isinstance(v, (int, float)): |
|
298 | elif isinstance(v, (int, float)): | |
297 | return str(v) |
|
299 | return str(v) | |
298 | else: |
|
300 | else: | |
299 | return '"%s"' % encoding.jsonescape(v) |
|
301 | return '"%s"' % encoding.jsonescape(v) | |
300 |
|
302 | |||
301 | class jsonformatter(baseformatter): |
|
303 | class jsonformatter(baseformatter): | |
302 | def __init__(self, ui, topic, opts): |
|
304 | def __init__(self, ui, out, topic, opts): | |
303 | baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
|
305 | baseformatter.__init__(self, ui, topic, opts, _nullconverter) | |
304 |
self._ |
|
306 | self._out = out | |
|
307 | self._out.write("[") | |||
305 | self._ui._first = True |
|
308 | self._ui._first = True | |
306 | def _showitem(self): |
|
309 | def _showitem(self): | |
307 | if self._ui._first: |
|
310 | if self._ui._first: | |
308 | self._ui._first = False |
|
311 | self._ui._first = False | |
309 | else: |
|
312 | else: | |
310 |
self._ |
|
313 | self._out.write(",") | |
311 |
|
314 | |||
312 |
self._ |
|
315 | self._out.write("\n {\n") | |
313 | first = True |
|
316 | first = True | |
314 | for k, v in sorted(self._item.items()): |
|
317 | for k, v in sorted(self._item.items()): | |
315 | if first: |
|
318 | if first: | |
316 | first = False |
|
319 | first = False | |
317 | else: |
|
320 | else: | |
318 |
self._ |
|
321 | self._out.write(",\n") | |
319 |
self._ |
|
322 | self._out.write(' "%s": %s' % (k, _jsonifyobj(v))) | |
320 |
self._ |
|
323 | self._out.write("\n }") | |
321 | def end(self): |
|
324 | def end(self): | |
322 | baseformatter.end(self) |
|
325 | baseformatter.end(self) | |
323 |
self._ |
|
326 | self._out.write("\n]\n") | |
324 |
|
327 | |||
325 | class _templateconverter(object): |
|
328 | class _templateconverter(object): | |
326 | '''convert non-primitive data types to be processed by templater''' |
|
329 | '''convert non-primitive data types to be processed by templater''' | |
327 | @staticmethod |
|
330 | @staticmethod | |
328 | def formatdate(date, fmt): |
|
331 | def formatdate(date, fmt): | |
329 | '''return date tuple''' |
|
332 | '''return date tuple''' | |
330 | return date |
|
333 | return date | |
331 | @staticmethod |
|
334 | @staticmethod | |
332 | def formatdict(data, key, value, fmt, sep): |
|
335 | def formatdict(data, key, value, fmt, sep): | |
333 | '''build object that can be evaluated as either plain string or dict''' |
|
336 | '''build object that can be evaluated as either plain string or dict''' | |
334 | data = util.sortdict(_iteritems(data)) |
|
337 | data = util.sortdict(_iteritems(data)) | |
335 | def f(): |
|
338 | def f(): | |
336 | yield _plainconverter.formatdict(data, key, value, fmt, sep) |
|
339 | yield _plainconverter.formatdict(data, key, value, fmt, sep) | |
337 | return templatekw._hybrid(f(), data, lambda k: {key: k, value: data[k]}, |
|
340 | return templatekw._hybrid(f(), data, lambda k: {key: k, value: data[k]}, | |
338 | lambda d: fmt % (d[key], d[value])) |
|
341 | lambda d: fmt % (d[key], d[value])) | |
339 | @staticmethod |
|
342 | @staticmethod | |
340 | def formatlist(data, name, fmt, sep): |
|
343 | def formatlist(data, name, fmt, sep): | |
341 | '''build object that can be evaluated as either plain string or list''' |
|
344 | '''build object that can be evaluated as either plain string or list''' | |
342 | data = list(data) |
|
345 | data = list(data) | |
343 | def f(): |
|
346 | def f(): | |
344 | yield _plainconverter.formatlist(data, name, fmt, sep) |
|
347 | yield _plainconverter.formatlist(data, name, fmt, sep) | |
345 | return templatekw._hybrid(f(), data, lambda x: {name: x}, |
|
348 | return templatekw._hybrid(f(), data, lambda x: {name: x}, | |
346 | lambda d: fmt % d[name]) |
|
349 | lambda d: fmt % d[name]) | |
347 |
|
350 | |||
348 | class templateformatter(baseformatter): |
|
351 | class templateformatter(baseformatter): | |
349 | def __init__(self, ui, topic, opts): |
|
352 | def __init__(self, ui, out, topic, opts): | |
350 | baseformatter.__init__(self, ui, topic, opts, _templateconverter) |
|
353 | baseformatter.__init__(self, ui, topic, opts, _templateconverter) | |
|
354 | self._out = out | |||
351 | self._topic = topic |
|
355 | self._topic = topic | |
352 | self._t = gettemplater(ui, topic, opts.get('template', ''), |
|
356 | self._t = gettemplater(ui, topic, opts.get('template', ''), | |
353 | cache=templatekw.defaulttempl) |
|
357 | cache=templatekw.defaulttempl) | |
354 | self._cache = {} # for templatekw/funcs to store reusable data |
|
358 | self._cache = {} # for templatekw/funcs to store reusable data | |
355 | def context(self, **ctxs): |
|
359 | def context(self, **ctxs): | |
356 | '''insert context objects to be used to render template keywords''' |
|
360 | '''insert context objects to be used to render template keywords''' | |
357 | assert all(k == 'ctx' for k in ctxs) |
|
361 | assert all(k == 'ctx' for k in ctxs) | |
358 | self._item.update(ctxs) |
|
362 | self._item.update(ctxs) | |
359 | def _showitem(self): |
|
363 | def _showitem(self): | |
360 | # TODO: add support for filectx. probably each template keyword or |
|
364 | # TODO: add support for filectx. probably each template keyword or | |
361 | # function will have to declare dependent resources. e.g. |
|
365 | # function will have to declare dependent resources. e.g. | |
362 | # @templatekeyword(..., requires=('ctx',)) |
|
366 | # @templatekeyword(..., requires=('ctx',)) | |
363 | if 'ctx' in self._item: |
|
367 | if 'ctx' in self._item: | |
364 | props = templatekw.keywords.copy() |
|
368 | props = templatekw.keywords.copy() | |
365 | # explicitly-defined fields precede templatekw |
|
369 | # explicitly-defined fields precede templatekw | |
366 | props.update(self._item) |
|
370 | props.update(self._item) | |
367 | # but template resources must be always available |
|
371 | # but template resources must be always available | |
368 | props['templ'] = self._t |
|
372 | props['templ'] = self._t | |
369 | props['repo'] = props['ctx'].repo() |
|
373 | props['repo'] = props['ctx'].repo() | |
370 | props['revcache'] = {} |
|
374 | props['revcache'] = {} | |
371 | else: |
|
375 | else: | |
372 | props = self._item |
|
376 | props = self._item | |
373 | g = self._t(self._topic, ui=self._ui, cache=self._cache, **props) |
|
377 | g = self._t(self._topic, ui=self._ui, cache=self._cache, **props) | |
374 |
self._ |
|
378 | self._out.write(templater.stringify(g)) | |
375 |
|
379 | |||
376 | def lookuptemplate(ui, topic, tmpl): |
|
380 | def lookuptemplate(ui, topic, tmpl): | |
377 | # looks like a literal template? |
|
381 | # looks like a literal template? | |
378 | if '{' in tmpl: |
|
382 | if '{' in tmpl: | |
379 | return tmpl, None |
|
383 | return tmpl, None | |
380 |
|
384 | |||
381 | # perhaps a stock style? |
|
385 | # perhaps a stock style? | |
382 | if not os.path.split(tmpl)[0]: |
|
386 | if not os.path.split(tmpl)[0]: | |
383 | mapname = (templater.templatepath('map-cmdline.' + tmpl) |
|
387 | mapname = (templater.templatepath('map-cmdline.' + tmpl) | |
384 | or templater.templatepath(tmpl)) |
|
388 | or templater.templatepath(tmpl)) | |
385 | if mapname and os.path.isfile(mapname): |
|
389 | if mapname and os.path.isfile(mapname): | |
386 | return None, mapname |
|
390 | return None, mapname | |
387 |
|
391 | |||
388 | # perhaps it's a reference to [templates] |
|
392 | # perhaps it's a reference to [templates] | |
389 | t = ui.config('templates', tmpl) |
|
393 | t = ui.config('templates', tmpl) | |
390 | if t: |
|
394 | if t: | |
391 | return templater.unquotestring(t), None |
|
395 | return templater.unquotestring(t), None | |
392 |
|
396 | |||
393 | if tmpl == 'list': |
|
397 | if tmpl == 'list': | |
394 | ui.write(_("available styles: %s\n") % templater.stylelist()) |
|
398 | ui.write(_("available styles: %s\n") % templater.stylelist()) | |
395 | raise error.Abort(_("specify a template")) |
|
399 | raise error.Abort(_("specify a template")) | |
396 |
|
400 | |||
397 | # perhaps it's a path to a map or a template |
|
401 | # perhaps it's a path to a map or a template | |
398 | if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl): |
|
402 | if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl): | |
399 | # is it a mapfile for a style? |
|
403 | # is it a mapfile for a style? | |
400 | if os.path.basename(tmpl).startswith("map-"): |
|
404 | if os.path.basename(tmpl).startswith("map-"): | |
401 | return None, os.path.realpath(tmpl) |
|
405 | return None, os.path.realpath(tmpl) | |
402 | tmpl = open(tmpl).read() |
|
406 | tmpl = open(tmpl).read() | |
403 | return tmpl, None |
|
407 | return tmpl, None | |
404 |
|
408 | |||
405 | # constant string? |
|
409 | # constant string? | |
406 | return tmpl, None |
|
410 | return tmpl, None | |
407 |
|
411 | |||
408 | def gettemplater(ui, topic, spec, cache=None): |
|
412 | def gettemplater(ui, topic, spec, cache=None): | |
409 | tmpl, mapfile = lookuptemplate(ui, topic, spec) |
|
413 | tmpl, mapfile = lookuptemplate(ui, topic, spec) | |
410 | assert not (tmpl and mapfile) |
|
414 | assert not (tmpl and mapfile) | |
411 | if mapfile: |
|
415 | if mapfile: | |
412 | return templater.templater.frommapfile(mapfile, cache=cache) |
|
416 | return templater.templater.frommapfile(mapfile, cache=cache) | |
413 | return maketemplater(ui, topic, tmpl, cache=cache) |
|
417 | return maketemplater(ui, topic, tmpl, cache=cache) | |
414 |
|
418 | |||
415 | def maketemplater(ui, topic, tmpl, cache=None): |
|
419 | def maketemplater(ui, topic, tmpl, cache=None): | |
416 | """Create a templater from a string template 'tmpl'""" |
|
420 | """Create a templater from a string template 'tmpl'""" | |
417 | aliases = ui.configitems('templatealias') |
|
421 | aliases = ui.configitems('templatealias') | |
418 | t = templater.templater(cache=cache, aliases=aliases) |
|
422 | t = templater.templater(cache=cache, aliases=aliases) | |
419 | if tmpl: |
|
423 | if tmpl: | |
420 | t.cache[topic] = tmpl |
|
424 | t.cache[topic] = tmpl | |
421 | return t |
|
425 | return t | |
422 |
|
426 | |||
423 | def formatter(ui, topic, opts): |
|
427 | def formatter(ui, topic, opts): | |
424 | template = opts.get("template", "") |
|
428 | template = opts.get("template", "") | |
425 | if template == "json": |
|
429 | if template == "json": | |
426 | return jsonformatter(ui, topic, opts) |
|
430 | return jsonformatter(ui, ui, topic, opts) | |
427 | elif template == "pickle": |
|
431 | elif template == "pickle": | |
428 | return pickleformatter(ui, topic, opts) |
|
432 | return pickleformatter(ui, ui, topic, opts) | |
429 | elif template == "debug": |
|
433 | elif template == "debug": | |
430 | return debugformatter(ui, topic, opts) |
|
434 | return debugformatter(ui, ui, topic, opts) | |
431 | elif template != "": |
|
435 | elif template != "": | |
432 | return templateformatter(ui, topic, opts) |
|
436 | return templateformatter(ui, ui, topic, opts) | |
433 | # developer config: ui.formatdebug |
|
437 | # developer config: ui.formatdebug | |
434 | elif ui.configbool('ui', 'formatdebug'): |
|
438 | elif ui.configbool('ui', 'formatdebug'): | |
435 | return debugformatter(ui, topic, opts) |
|
439 | return debugformatter(ui, ui, topic, opts) | |
436 | # deprecated config: ui.formatjson |
|
440 | # deprecated config: ui.formatjson | |
437 | elif ui.configbool('ui', 'formatjson'): |
|
441 | elif ui.configbool('ui', 'formatjson'): | |
438 | return jsonformatter(ui, topic, opts) |
|
442 | return jsonformatter(ui, ui, topic, opts) | |
439 | return plainformatter(ui, topic, opts) |
|
443 | return plainformatter(ui, topic, opts) |
General Comments 0
You need to be logged in to leave comments.
Login now