Mercurial > hg-stable
annotate mercurial/formatter.py @ 22428:427e80a18ef8
formatter: add json formatter
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Fri, 12 Sep 2014 18:29:29 -0500 |
parents | 1f72226064b8 |
children | 968247e8f4ac |
rev | line source |
---|---|
16134 | 1 # formatter.py - generic output formatting for mercurial |
2 # | |
3 # Copyright 2012 Matt Mackall <mpm@selenic.com> | |
4 # | |
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. | |
7 | |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
8 from i18n import _ |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
9 import encoding, util |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
10 |
16134 | 11 class baseformatter(object): |
12 def __init__(self, ui, topic, opts): | |
13 self._ui = ui | |
14 self._topic = topic | |
15 self._style = opts.get("style") | |
16 self._template = opts.get("template") | |
17 self._item = None | |
18 def __bool__(self): | |
19 '''return False if we're not doing real templating so we can | |
20 skip extra work''' | |
21 return True | |
22 def _showitem(self): | |
23 '''show a formatted item once all data is collected''' | |
24 pass | |
25 def startitem(self): | |
26 '''begin an item in the format list''' | |
27 if self._item is not None: | |
28 self._showitem() | |
29 self._item = {} | |
30 def data(self, **data): | |
31 '''insert data into item that's not shown in default output''' | |
17630
ff5ed1ecd43a
formatter: improve implementation of data method
David M. Carr <david@carrclan.us>
parents:
17597
diff
changeset
|
32 self._item.update(data) |
16134 | 33 def write(self, fields, deftext, *fielddata, **opts): |
34 '''do default text output while assigning data to item''' | |
35 for k, v in zip(fields.split(), fielddata): | |
36 self._item[k] = v | |
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
37 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
38 '''do conditional write (primarily for plain formatter)''' |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
39 for k, v in zip(fields.split(), fielddata): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
40 self._item[k] = v |
16134 | 41 def plain(self, text, **opts): |
42 '''show raw text for non-templated mode''' | |
43 pass | |
44 def end(self): | |
45 '''end output for the formatter''' | |
46 if self._item is not None: | |
47 self._showitem() | |
48 | |
49 class plainformatter(baseformatter): | |
50 '''the default text output scheme''' | |
51 def __init__(self, ui, topic, opts): | |
52 baseformatter.__init__(self, ui, topic, opts) | |
53 def __bool__(self): | |
54 return False | |
55 def startitem(self): | |
56 pass | |
57 def data(self, **data): | |
58 pass | |
59 def write(self, fields, deftext, *fielddata, **opts): | |
60 self._ui.write(deftext % fielddata, **opts) | |
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
61 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
62 '''do conditional write''' |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
63 if cond: |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
64 self._ui.write(deftext % fielddata, **opts) |
16134 | 65 def plain(self, text, **opts): |
66 self._ui.write(text, **opts) | |
67 def end(self): | |
68 pass | |
69 | |
70 class debugformatter(baseformatter): | |
71 def __init__(self, ui, topic, opts): | |
72 baseformatter.__init__(self, ui, topic, opts) | |
22424
1f72226064b8
formatter: make debug style match Python syntax
Matt Mackall <mpm@selenic.com>
parents:
17909
diff
changeset
|
73 self._ui.write("%s = [\n" % self._topic) |
16134 | 74 def _showitem(self): |
75 self._ui.write(" " + repr(self._item) + ",\n") | |
76 def end(self): | |
77 baseformatter.end(self) | |
22424
1f72226064b8
formatter: make debug style match Python syntax
Matt Mackall <mpm@selenic.com>
parents:
17909
diff
changeset
|
78 self._ui.write("]\n") |
16134 | 79 |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
80 class jsonformatter(baseformatter): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
81 def __init__(self, ui, topic, opts): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
82 baseformatter.__init__(self, ui, topic, opts) |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
83 self._ui.write("[") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
84 self._ui._first = True |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
85 def _showitem(self): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
86 if self._ui._first: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
87 self._ui._first = False |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
88 else: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
89 self._ui.write(",") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
90 |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
91 self._ui.write("\n {\n") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
92 first = True |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
93 for k, v in sorted(self._item.items()): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
94 if first: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
95 first = False |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
96 else: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
97 self._ui.write(",\n") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
98 if isinstance(v, int): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
99 self._ui.write(' "%s": %d' % (k, v)) |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
100 else: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
101 self._ui.write(' "%s": "%s"' % (k, encoding.jsonescape(v))) |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
102 self._ui.write("\n }") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
103 def end(self): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
104 baseformatter.end(self) |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
105 self._ui.write("\n]\n") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
106 |
16134 | 107 def formatter(ui, topic, opts): |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
108 template = opts.get("template", "") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
109 if template == "json": |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
110 return jsonformatter(ui, topic, opts) |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
111 elif template == "debug": |
16134 | 112 return debugformatter(ui, topic, opts) |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
113 elif template != "": |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
114 raise util.Abort(_("custom templates not yet supported")) |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
115 elif ui.configbool('ui', 'formatdebug'): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
116 return debugformatter(ui, topic, opts) |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
117 elif ui.configbool('ui', 'formatjson'): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
118 return jsonformatter(ui, topic, opts) |
16134 | 119 return plainformatter(ui, topic, opts) |