##// END OF EJS Templates
formatter: add fm.nested(field) to either write or build sub items...
Yuya Nishihara -
r29837:5b886289 default
parent child Browse files
Show More
@@ -1,302 +1,317
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 os
11 11
12 12 from .i18n import _
13 13 from .node import (
14 14 hex,
15 15 short,
16 16 )
17 17
18 18 from . import (
19 19 encoding,
20 20 error,
21 21 templatekw,
22 22 templater,
23 23 util,
24 24 )
25 25
26 26 pickle = util.pickle
27 27
28 28 class _nullconverter(object):
29 29 '''convert non-primitive data types to be processed by formatter'''
30 30 @staticmethod
31 31 def formatdate(date, fmt):
32 32 '''convert date tuple to appropriate format'''
33 33 return date
34 34 @staticmethod
35 35 def formatdict(data, key, value, fmt, sep):
36 36 '''convert dict or key-value pairs to appropriate dict format'''
37 37 # use plain dict instead of util.sortdict so that data can be
38 38 # serialized as a builtin dict in pickle output
39 39 return dict(data)
40 40 @staticmethod
41 41 def formatlist(data, name, fmt, sep):
42 42 '''convert iterable to appropriate list format'''
43 43 return list(data)
44 44
45 45 class baseformatter(object):
46 46 def __init__(self, ui, topic, opts, converter):
47 47 self._ui = ui
48 48 self._topic = topic
49 49 self._style = opts.get("style")
50 50 self._template = opts.get("template")
51 51 self._converter = converter
52 52 self._item = None
53 53 # function to convert node to string suitable for this output
54 54 self.hexfunc = hex
55 55 def __nonzero__(self):
56 56 '''return False if we're not doing real templating so we can
57 57 skip extra work'''
58 58 return True
59 59 def _showitem(self):
60 60 '''show a formatted item once all data is collected'''
61 61 pass
62 62 def startitem(self):
63 63 '''begin an item in the format list'''
64 64 if self._item is not None:
65 65 self._showitem()
66 66 self._item = {}
67 67 def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'):
68 68 '''convert date tuple to appropriate format'''
69 69 return self._converter.formatdate(date, fmt)
70 70 def formatdict(self, data, key='key', value='value', fmt='%s=%s', sep=' '):
71 71 '''convert dict or key-value pairs to appropriate dict format'''
72 72 return self._converter.formatdict(data, key, value, fmt, sep)
73 73 def formatlist(self, data, name, fmt='%s', sep=' '):
74 74 '''convert iterable to appropriate list format'''
75 75 # name is mandatory argument for now, but it could be optional if
76 76 # we have default template keyword, e.g. {item}
77 77 return self._converter.formatlist(data, name, fmt, sep)
78 78 def data(self, **data):
79 79 '''insert data into item that's not shown in default output'''
80 80 self._item.update(data)
81 81 def write(self, fields, deftext, *fielddata, **opts):
82 82 '''do default text output while assigning data to item'''
83 83 fieldkeys = fields.split()
84 84 assert len(fieldkeys) == len(fielddata)
85 85 self._item.update(zip(fieldkeys, fielddata))
86 86 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
87 87 '''do conditional write (primarily for plain formatter)'''
88 88 fieldkeys = fields.split()
89 89 assert len(fieldkeys) == len(fielddata)
90 90 self._item.update(zip(fieldkeys, fielddata))
91 91 def plain(self, text, **opts):
92 92 '''show raw text for non-templated mode'''
93 93 pass
94 def nested(self, field):
95 '''sub formatter to store nested data in the specified field'''
96 self._item[field] = data = []
97 return _nestedformatter(self._ui, self._converter, data)
94 98 def end(self):
95 99 '''end output for the formatter'''
96 100 if self._item is not None:
97 101 self._showitem()
98 102
103 class _nestedformatter(baseformatter):
104 '''build sub items and store them in the parent formatter'''
105 def __init__(self, ui, converter, data):
106 baseformatter.__init__(self, ui, topic='', opts={}, converter=converter)
107 self._data = data
108 def _showitem(self):
109 self._data.append(self._item)
110
99 111 def _iteritems(data):
100 112 '''iterate key-value pairs in stable order'''
101 113 if isinstance(data, dict):
102 114 return sorted(data.iteritems())
103 115 return data
104 116
105 117 class _plainconverter(object):
106 118 '''convert non-primitive data types to text'''
107 119 @staticmethod
108 120 def formatdate(date, fmt):
109 121 '''stringify date tuple in the given format'''
110 122 return util.datestr(date, fmt)
111 123 @staticmethod
112 124 def formatdict(data, key, value, fmt, sep):
113 125 '''stringify key-value pairs separated by sep'''
114 126 return sep.join(fmt % (k, v) for k, v in _iteritems(data))
115 127 @staticmethod
116 128 def formatlist(data, name, fmt, sep):
117 129 '''stringify iterable separated by sep'''
118 130 return sep.join(fmt % e for e in data)
119 131
120 132 class plainformatter(baseformatter):
121 133 '''the default text output scheme'''
122 134 def __init__(self, ui, topic, opts):
123 135 baseformatter.__init__(self, ui, topic, opts, _plainconverter)
124 136 if ui.debugflag:
125 137 self.hexfunc = hex
126 138 else:
127 139 self.hexfunc = short
128 140 def __nonzero__(self):
129 141 return False
130 142 def startitem(self):
131 143 pass
132 144 def data(self, **data):
133 145 pass
134 146 def write(self, fields, deftext, *fielddata, **opts):
135 147 self._ui.write(deftext % fielddata, **opts)
136 148 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
137 149 '''do conditional write'''
138 150 if cond:
139 151 self._ui.write(deftext % fielddata, **opts)
140 152 def plain(self, text, **opts):
141 153 self._ui.write(text, **opts)
154 def nested(self, field):
155 # nested data will be directly written to ui
156 return self
142 157 def end(self):
143 158 pass
144 159
145 160 class debugformatter(baseformatter):
146 161 def __init__(self, ui, topic, opts):
147 162 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
148 163 self._ui.write("%s = [\n" % self._topic)
149 164 def _showitem(self):
150 165 self._ui.write(" " + repr(self._item) + ",\n")
151 166 def end(self):
152 167 baseformatter.end(self)
153 168 self._ui.write("]\n")
154 169
155 170 class pickleformatter(baseformatter):
156 171 def __init__(self, ui, topic, opts):
157 172 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
158 173 self._data = []
159 174 def _showitem(self):
160 175 self._data.append(self._item)
161 176 def end(self):
162 177 baseformatter.end(self)
163 178 self._ui.write(pickle.dumps(self._data))
164 179
165 180 def _jsonifyobj(v):
166 181 if isinstance(v, dict):
167 182 xs = ['"%s": %s' % (encoding.jsonescape(k), _jsonifyobj(u))
168 183 for k, u in sorted(v.iteritems())]
169 184 return '{' + ', '.join(xs) + '}'
170 185 elif isinstance(v, (list, tuple)):
171 186 return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']'
172 187 elif v is None:
173 188 return 'null'
174 189 elif v is True:
175 190 return 'true'
176 191 elif v is False:
177 192 return 'false'
178 193 elif isinstance(v, (int, float)):
179 194 return str(v)
180 195 else:
181 196 return '"%s"' % encoding.jsonescape(v)
182 197
183 198 class jsonformatter(baseformatter):
184 199 def __init__(self, ui, topic, opts):
185 200 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
186 201 self._ui.write("[")
187 202 self._ui._first = True
188 203 def _showitem(self):
189 204 if self._ui._first:
190 205 self._ui._first = False
191 206 else:
192 207 self._ui.write(",")
193 208
194 209 self._ui.write("\n {\n")
195 210 first = True
196 211 for k, v in sorted(self._item.items()):
197 212 if first:
198 213 first = False
199 214 else:
200 215 self._ui.write(",\n")
201 216 self._ui.write(' "%s": %s' % (k, _jsonifyobj(v)))
202 217 self._ui.write("\n }")
203 218 def end(self):
204 219 baseformatter.end(self)
205 220 self._ui.write("\n]\n")
206 221
207 222 class _templateconverter(object):
208 223 '''convert non-primitive data types to be processed by templater'''
209 224 @staticmethod
210 225 def formatdate(date, fmt):
211 226 '''return date tuple'''
212 227 return date
213 228 @staticmethod
214 229 def formatdict(data, key, value, fmt, sep):
215 230 '''build object that can be evaluated as either plain string or dict'''
216 231 data = util.sortdict(_iteritems(data))
217 232 def f():
218 233 yield _plainconverter.formatdict(data, key, value, fmt, sep)
219 234 return templatekw._hybrid(f(), data, lambda k: {key: k, value: data[k]},
220 235 lambda d: fmt % (d[key], d[value]))
221 236 @staticmethod
222 237 def formatlist(data, name, fmt, sep):
223 238 '''build object that can be evaluated as either plain string or list'''
224 239 data = list(data)
225 240 def f():
226 241 yield _plainconverter.formatlist(data, name, fmt, sep)
227 242 return templatekw._hybrid(f(), data, lambda x: {name: x},
228 243 lambda d: fmt % d[name])
229 244
230 245 class templateformatter(baseformatter):
231 246 def __init__(self, ui, topic, opts):
232 247 baseformatter.__init__(self, ui, topic, opts, _templateconverter)
233 248 self._topic = topic
234 249 self._t = gettemplater(ui, topic, opts.get('template', ''))
235 250 def _showitem(self):
236 251 g = self._t(self._topic, ui=self._ui, **self._item)
237 252 self._ui.write(templater.stringify(g))
238 253
239 254 def lookuptemplate(ui, topic, tmpl):
240 255 # looks like a literal template?
241 256 if '{' in tmpl:
242 257 return tmpl, None
243 258
244 259 # perhaps a stock style?
245 260 if not os.path.split(tmpl)[0]:
246 261 mapname = (templater.templatepath('map-cmdline.' + tmpl)
247 262 or templater.templatepath(tmpl))
248 263 if mapname and os.path.isfile(mapname):
249 264 return None, mapname
250 265
251 266 # perhaps it's a reference to [templates]
252 267 t = ui.config('templates', tmpl)
253 268 if t:
254 269 return templater.unquotestring(t), None
255 270
256 271 if tmpl == 'list':
257 272 ui.write(_("available styles: %s\n") % templater.stylelist())
258 273 raise error.Abort(_("specify a template"))
259 274
260 275 # perhaps it's a path to a map or a template
261 276 if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl):
262 277 # is it a mapfile for a style?
263 278 if os.path.basename(tmpl).startswith("map-"):
264 279 return None, os.path.realpath(tmpl)
265 280 tmpl = open(tmpl).read()
266 281 return tmpl, None
267 282
268 283 # constant string?
269 284 return tmpl, None
270 285
271 286 def gettemplater(ui, topic, spec):
272 287 tmpl, mapfile = lookuptemplate(ui, topic, spec)
273 288 assert not (tmpl and mapfile)
274 289 if mapfile:
275 290 return templater.templater.frommapfile(mapfile)
276 291 return maketemplater(ui, topic, tmpl)
277 292
278 293 def maketemplater(ui, topic, tmpl, filters=None, cache=None):
279 294 """Create a templater from a string template 'tmpl'"""
280 295 aliases = ui.configitems('templatealias')
281 296 t = templater.templater(filters=filters, cache=cache, aliases=aliases)
282 297 if tmpl:
283 298 t.cache[topic] = tmpl
284 299 return t
285 300
286 301 def formatter(ui, topic, opts):
287 302 template = opts.get("template", "")
288 303 if template == "json":
289 304 return jsonformatter(ui, topic, opts)
290 305 elif template == "pickle":
291 306 return pickleformatter(ui, topic, opts)
292 307 elif template == "debug":
293 308 return debugformatter(ui, topic, opts)
294 309 elif template != "":
295 310 return templateformatter(ui, topic, opts)
296 311 # developer config: ui.formatdebug
297 312 elif ui.configbool('ui', 'formatdebug'):
298 313 return debugformatter(ui, topic, opts)
299 314 # deprecated config: ui.formatjson
300 315 elif ui.configbool('ui', 'formatjson'):
301 316 return jsonformatter(ui, topic, opts)
302 317 return plainformatter(ui, topic, opts)
General Comments 0
You need to be logged in to leave comments. Login now