author | David M. Carr <david@carrclan.us> |
Sat, 15 Sep 2012 22:50:34 -0400 | |
changeset 17597 | 772b3764d3e8 |
parent 16134 | 3c0327ea20c0 |
child 17630 | ff5ed1ecd43a |
permissions | -rw-r--r-- |
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 |
||
8 |
class baseformatter(object): |
|
9 |
def __init__(self, ui, topic, opts): |
|
10 |
self._ui = ui |
|
11 |
self._topic = topic |
|
12 |
self._style = opts.get("style") |
|
13 |
self._template = opts.get("template") |
|
14 |
self._item = None |
|
15 |
def __bool__(self): |
|
16 |
'''return False if we're not doing real templating so we can |
|
17 |
skip extra work''' |
|
18 |
return True |
|
19 |
def _showitem(self): |
|
20 |
'''show a formatted item once all data is collected''' |
|
21 |
pass |
|
22 |
def startitem(self): |
|
23 |
'''begin an item in the format list''' |
|
24 |
if self._item is not None: |
|
25 |
self._showitem() |
|
26 |
self._item = {} |
|
27 |
def data(self, **data): |
|
28 |
'''insert data into item that's not shown in default output''' |
|
17597
772b3764d3e8
formatter: add base implementation of data method
David M. Carr <david@carrclan.us>
parents:
16134
diff
changeset
|
29 |
for k, v in data.iteritems(): |
772b3764d3e8
formatter: add base implementation of data method
David M. Carr <david@carrclan.us>
parents:
16134
diff
changeset
|
30 |
self._item[k] = v |
16134 | 31 |
def write(self, fields, deftext, *fielddata, **opts): |
32 |
'''do default text output while assigning data to item''' |
|
33 |
for k, v in zip(fields.split(), fielddata): |
|
34 |
self._item[k] = v |
|
35 |
def plain(self, text, **opts): |
|
36 |
'''show raw text for non-templated mode''' |
|
37 |
pass |
|
38 |
def end(self): |
|
39 |
'''end output for the formatter''' |
|
40 |
if self._item is not None: |
|
41 |
self._showitem() |
|
42 |
||
43 |
class plainformatter(baseformatter): |
|
44 |
'''the default text output scheme''' |
|
45 |
def __init__(self, ui, topic, opts): |
|
46 |
baseformatter.__init__(self, ui, topic, opts) |
|
47 |
def __bool__(self): |
|
48 |
return False |
|
49 |
def startitem(self): |
|
50 |
pass |
|
51 |
def data(self, **data): |
|
52 |
pass |
|
53 |
def write(self, fields, deftext, *fielddata, **opts): |
|
54 |
self._ui.write(deftext % fielddata, **opts) |
|
55 |
def plain(self, text, **opts): |
|
56 |
self._ui.write(text, **opts) |
|
57 |
def end(self): |
|
58 |
pass |
|
59 |
||
60 |
class debugformatter(baseformatter): |
|
61 |
def __init__(self, ui, topic, opts): |
|
62 |
baseformatter.__init__(self, ui, topic, opts) |
|
63 |
self._ui.write("%s = {\n" % self._topic) |
|
64 |
def _showitem(self): |
|
65 |
self._ui.write(" " + repr(self._item) + ",\n") |
|
66 |
def end(self): |
|
67 |
baseformatter.end(self) |
|
68 |
self._ui.write("}\n") |
|
69 |
||
70 |
def formatter(ui, topic, opts): |
|
71 |
if ui.configbool('ui', 'formatdebug'): |
|
72 |
return debugformatter(ui, topic, opts) |
|
73 |
return plainformatter(ui, topic, opts) |