Show More
@@ -1,220 +1,218 | |||
|
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 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import cPickle |
|
11 | 11 | import os |
|
12 | 12 | |
|
13 | 13 | from .i18n import _ |
|
14 | 14 | from .node import ( |
|
15 | 15 | hex, |
|
16 | 16 | short, |
|
17 | 17 | ) |
|
18 | 18 | |
|
19 | 19 | from . import ( |
|
20 | 20 | encoding, |
|
21 | 21 | templater, |
|
22 | 22 | util, |
|
23 | 23 | ) |
|
24 | 24 | |
|
25 | 25 | class baseformatter(object): |
|
26 | 26 | def __init__(self, ui, topic, opts): |
|
27 | 27 | self._ui = ui |
|
28 | 28 | self._topic = topic |
|
29 | 29 | self._style = opts.get("style") |
|
30 | 30 | self._template = opts.get("template") |
|
31 | 31 | self._item = None |
|
32 | 32 | # function to convert node to string suitable for this output |
|
33 | 33 | self.hexfunc = hex |
|
34 | 34 | def __nonzero__(self): |
|
35 | 35 | '''return False if we're not doing real templating so we can |
|
36 | 36 | skip extra work''' |
|
37 | 37 | return True |
|
38 | 38 | def _showitem(self): |
|
39 | 39 | '''show a formatted item once all data is collected''' |
|
40 | 40 | pass |
|
41 | 41 | def startitem(self): |
|
42 | 42 | '''begin an item in the format list''' |
|
43 | 43 | if self._item is not None: |
|
44 | 44 | self._showitem() |
|
45 | 45 | self._item = {} |
|
46 | 46 | def data(self, **data): |
|
47 | 47 | '''insert data into item that's not shown in default output''' |
|
48 | 48 | self._item.update(data) |
|
49 | 49 | def write(self, fields, deftext, *fielddata, **opts): |
|
50 | 50 | '''do default text output while assigning data to item''' |
|
51 | 51 | fieldkeys = fields.split() |
|
52 | 52 | assert len(fieldkeys) == len(fielddata) |
|
53 |
|
|
|
54 | self._item[k] = v | |
|
53 | self._item.update(zip(fieldkeys, fielddata)) | |
|
55 | 54 | def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
|
56 | 55 | '''do conditional write (primarily for plain formatter)''' |
|
57 | 56 | fieldkeys = fields.split() |
|
58 | 57 | assert len(fieldkeys) == len(fielddata) |
|
59 |
|
|
|
60 | self._item[k] = v | |
|
58 | self._item.update(zip(fieldkeys, fielddata)) | |
|
61 | 59 | def plain(self, text, **opts): |
|
62 | 60 | '''show raw text for non-templated mode''' |
|
63 | 61 | pass |
|
64 | 62 | def end(self): |
|
65 | 63 | '''end output for the formatter''' |
|
66 | 64 | if self._item is not None: |
|
67 | 65 | self._showitem() |
|
68 | 66 | |
|
69 | 67 | class plainformatter(baseformatter): |
|
70 | 68 | '''the default text output scheme''' |
|
71 | 69 | def __init__(self, ui, topic, opts): |
|
72 | 70 | baseformatter.__init__(self, ui, topic, opts) |
|
73 | 71 | if ui.debugflag: |
|
74 | 72 | self.hexfunc = hex |
|
75 | 73 | else: |
|
76 | 74 | self.hexfunc = short |
|
77 | 75 | def __nonzero__(self): |
|
78 | 76 | return False |
|
79 | 77 | def startitem(self): |
|
80 | 78 | pass |
|
81 | 79 | def data(self, **data): |
|
82 | 80 | pass |
|
83 | 81 | def write(self, fields, deftext, *fielddata, **opts): |
|
84 | 82 | self._ui.write(deftext % fielddata, **opts) |
|
85 | 83 | def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
|
86 | 84 | '''do conditional write''' |
|
87 | 85 | if cond: |
|
88 | 86 | self._ui.write(deftext % fielddata, **opts) |
|
89 | 87 | def plain(self, text, **opts): |
|
90 | 88 | self._ui.write(text, **opts) |
|
91 | 89 | def end(self): |
|
92 | 90 | pass |
|
93 | 91 | |
|
94 | 92 | class debugformatter(baseformatter): |
|
95 | 93 | def __init__(self, ui, topic, opts): |
|
96 | 94 | baseformatter.__init__(self, ui, topic, opts) |
|
97 | 95 | self._ui.write("%s = [\n" % self._topic) |
|
98 | 96 | def _showitem(self): |
|
99 | 97 | self._ui.write(" " + repr(self._item) + ",\n") |
|
100 | 98 | def end(self): |
|
101 | 99 | baseformatter.end(self) |
|
102 | 100 | self._ui.write("]\n") |
|
103 | 101 | |
|
104 | 102 | class pickleformatter(baseformatter): |
|
105 | 103 | def __init__(self, ui, topic, opts): |
|
106 | 104 | baseformatter.__init__(self, ui, topic, opts) |
|
107 | 105 | self._data = [] |
|
108 | 106 | def _showitem(self): |
|
109 | 107 | self._data.append(self._item) |
|
110 | 108 | def end(self): |
|
111 | 109 | baseformatter.end(self) |
|
112 | 110 | self._ui.write(cPickle.dumps(self._data)) |
|
113 | 111 | |
|
114 | 112 | def _jsonifyobj(v): |
|
115 | 113 | if isinstance(v, tuple): |
|
116 | 114 | return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']' |
|
117 | 115 | elif v is None: |
|
118 | 116 | return 'null' |
|
119 | 117 | elif v is True: |
|
120 | 118 | return 'true' |
|
121 | 119 | elif v is False: |
|
122 | 120 | return 'false' |
|
123 | 121 | elif isinstance(v, (int, float)): |
|
124 | 122 | return str(v) |
|
125 | 123 | else: |
|
126 | 124 | return '"%s"' % encoding.jsonescape(v) |
|
127 | 125 | |
|
128 | 126 | class jsonformatter(baseformatter): |
|
129 | 127 | def __init__(self, ui, topic, opts): |
|
130 | 128 | baseformatter.__init__(self, ui, topic, opts) |
|
131 | 129 | self._ui.write("[") |
|
132 | 130 | self._ui._first = True |
|
133 | 131 | def _showitem(self): |
|
134 | 132 | if self._ui._first: |
|
135 | 133 | self._ui._first = False |
|
136 | 134 | else: |
|
137 | 135 | self._ui.write(",") |
|
138 | 136 | |
|
139 | 137 | self._ui.write("\n {\n") |
|
140 | 138 | first = True |
|
141 | 139 | for k, v in sorted(self._item.items()): |
|
142 | 140 | if first: |
|
143 | 141 | first = False |
|
144 | 142 | else: |
|
145 | 143 | self._ui.write(",\n") |
|
146 | 144 | self._ui.write(' "%s": %s' % (k, _jsonifyobj(v))) |
|
147 | 145 | self._ui.write("\n }") |
|
148 | 146 | def end(self): |
|
149 | 147 | baseformatter.end(self) |
|
150 | 148 | self._ui.write("\n]\n") |
|
151 | 149 | |
|
152 | 150 | class templateformatter(baseformatter): |
|
153 | 151 | def __init__(self, ui, topic, opts): |
|
154 | 152 | baseformatter.__init__(self, ui, topic, opts) |
|
155 | 153 | self._topic = topic |
|
156 | 154 | self._t = gettemplater(ui, topic, opts.get('template', '')) |
|
157 | 155 | def _showitem(self): |
|
158 | 156 | g = self._t(self._topic, **self._item) |
|
159 | 157 | self._ui.write(templater.stringify(g)) |
|
160 | 158 | |
|
161 | 159 | def lookuptemplate(ui, topic, tmpl): |
|
162 | 160 | # looks like a literal template? |
|
163 | 161 | if '{' in tmpl: |
|
164 | 162 | return tmpl, None |
|
165 | 163 | |
|
166 | 164 | # perhaps a stock style? |
|
167 | 165 | if not os.path.split(tmpl)[0]: |
|
168 | 166 | mapname = (templater.templatepath('map-cmdline.' + tmpl) |
|
169 | 167 | or templater.templatepath(tmpl)) |
|
170 | 168 | if mapname and os.path.isfile(mapname): |
|
171 | 169 | return None, mapname |
|
172 | 170 | |
|
173 | 171 | # perhaps it's a reference to [templates] |
|
174 | 172 | t = ui.config('templates', tmpl) |
|
175 | 173 | if t: |
|
176 | 174 | try: |
|
177 | 175 | tmpl = templater.unquotestring(t) |
|
178 | 176 | except SyntaxError: |
|
179 | 177 | tmpl = t |
|
180 | 178 | return tmpl, None |
|
181 | 179 | |
|
182 | 180 | if tmpl == 'list': |
|
183 | 181 | ui.write(_("available styles: %s\n") % templater.stylelist()) |
|
184 | 182 | raise util.Abort(_("specify a template")) |
|
185 | 183 | |
|
186 | 184 | # perhaps it's a path to a map or a template |
|
187 | 185 | if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl): |
|
188 | 186 | # is it a mapfile for a style? |
|
189 | 187 | if os.path.basename(tmpl).startswith("map-"): |
|
190 | 188 | return None, os.path.realpath(tmpl) |
|
191 | 189 | tmpl = open(tmpl).read() |
|
192 | 190 | return tmpl, None |
|
193 | 191 | |
|
194 | 192 | # constant string? |
|
195 | 193 | return tmpl, None |
|
196 | 194 | |
|
197 | 195 | def gettemplater(ui, topic, spec): |
|
198 | 196 | tmpl, mapfile = lookuptemplate(ui, topic, spec) |
|
199 | 197 | t = templater.templater(mapfile, {}) |
|
200 | 198 | if tmpl: |
|
201 | 199 | t.cache[topic] = tmpl |
|
202 | 200 | return t |
|
203 | 201 | |
|
204 | 202 | def formatter(ui, topic, opts): |
|
205 | 203 | template = opts.get("template", "") |
|
206 | 204 | if template == "json": |
|
207 | 205 | return jsonformatter(ui, topic, opts) |
|
208 | 206 | elif template == "pickle": |
|
209 | 207 | return pickleformatter(ui, topic, opts) |
|
210 | 208 | elif template == "debug": |
|
211 | 209 | return debugformatter(ui, topic, opts) |
|
212 | 210 | elif template != "": |
|
213 | 211 | return templateformatter(ui, topic, opts) |
|
214 | 212 | # developer config: ui.formatdebug |
|
215 | 213 | elif ui.configbool('ui', 'formatdebug'): |
|
216 | 214 | return debugformatter(ui, topic, opts) |
|
217 | 215 | # deprecated config: ui.formatjson |
|
218 | 216 | elif ui.configbool('ui', 'formatjson'): |
|
219 | 217 | return jsonformatter(ui, topic, opts) |
|
220 | 218 | return plainformatter(ui, topic, opts) |
General Comments 0
You need to be logged in to leave comments.
Login now