author | Tim Henigan <tim.henigan@gmail.com> |
Tue, 27 Nov 2012 08:47:35 -0500 | |
branch | stable |
changeset 17984 | b74361cf7c0a |
parent 17630 | ff5ed1ecd43a |
child 17909 | 3326fd05eb1f |
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''' |
|
17630
ff5ed1ecd43a
formatter: improve implementation of data method
David M. Carr <david@carrclan.us>
parents:
17597
diff
changeset
|
29 |
self._item.update(data) |
16134 | 30 |
def write(self, fields, deftext, *fielddata, **opts): |
31 |
'''do default text output while assigning data to item''' |
|
32 |
for k, v in zip(fields.split(), fielddata): |
|
33 |
self._item[k] = v |
|
34 |
def plain(self, text, **opts): |
|
35 |
'''show raw text for non-templated mode''' |
|
36 |
pass |
|
37 |
def end(self): |
|
38 |
'''end output for the formatter''' |
|
39 |
if self._item is not None: |
|
40 |
self._showitem() |
|
41 |
||
42 |
class plainformatter(baseformatter): |
|
43 |
'''the default text output scheme''' |
|
44 |
def __init__(self, ui, topic, opts): |
|
45 |
baseformatter.__init__(self, ui, topic, opts) |
|
46 |
def __bool__(self): |
|
47 |
return False |
|
48 |
def startitem(self): |
|
49 |
pass |
|
50 |
def data(self, **data): |
|
51 |
pass |
|
52 |
def write(self, fields, deftext, *fielddata, **opts): |
|
53 |
self._ui.write(deftext % fielddata, **opts) |
|
54 |
def plain(self, text, **opts): |
|
55 |
self._ui.write(text, **opts) |
|
56 |
def end(self): |
|
57 |
pass |
|
58 |
||
59 |
class debugformatter(baseformatter): |
|
60 |
def __init__(self, ui, topic, opts): |
|
61 |
baseformatter.__init__(self, ui, topic, opts) |
|
62 |
self._ui.write("%s = {\n" % self._topic) |
|
63 |
def _showitem(self): |
|
64 |
self._ui.write(" " + repr(self._item) + ",\n") |
|
65 |
def end(self): |
|
66 |
baseformatter.end(self) |
|
67 |
self._ui.write("}\n") |
|
68 |
||
69 |
def formatter(ui, topic, opts): |
|
70 |
if ui.configbool('ui', 'formatdebug'): |
|
71 |
return debugformatter(ui, topic, opts) |
|
72 |
return plainformatter(ui, topic, opts) |