author | Matt Mackall <mpm@selenic.com> |
Sat, 01 Jun 2013 17:19:00 -0500 | |
changeset 19261 | 043abd6a91d1 |
parent 17909 | 3326fd05eb1f |
child 22424 | 1f72226064b8 |
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 |
|
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
34 |
def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
35 |
'''do conditional write (primarily for plain formatter)''' |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
36 |
for k, v in zip(fields.split(), fielddata): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
37 |
self._item[k] = v |
16134 | 38 |
def plain(self, text, **opts): |
39 |
'''show raw text for non-templated mode''' |
|
40 |
pass |
|
41 |
def end(self): |
|
42 |
'''end output for the formatter''' |
|
43 |
if self._item is not None: |
|
44 |
self._showitem() |
|
45 |
||
46 |
class plainformatter(baseformatter): |
|
47 |
'''the default text output scheme''' |
|
48 |
def __init__(self, ui, topic, opts): |
|
49 |
baseformatter.__init__(self, ui, topic, opts) |
|
50 |
def __bool__(self): |
|
51 |
return False |
|
52 |
def startitem(self): |
|
53 |
pass |
|
54 |
def data(self, **data): |
|
55 |
pass |
|
56 |
def write(self, fields, deftext, *fielddata, **opts): |
|
57 |
self._ui.write(deftext % fielddata, **opts) |
|
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
58 |
def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
59 |
'''do conditional write''' |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
60 |
if cond: |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
61 |
self._ui.write(deftext % fielddata, **opts) |
16134 | 62 |
def plain(self, text, **opts): |
63 |
self._ui.write(text, **opts) |
|
64 |
def end(self): |
|
65 |
pass |
|
66 |
||
67 |
class debugformatter(baseformatter): |
|
68 |
def __init__(self, ui, topic, opts): |
|
69 |
baseformatter.__init__(self, ui, topic, opts) |
|
70 |
self._ui.write("%s = {\n" % self._topic) |
|
71 |
def _showitem(self): |
|
72 |
self._ui.write(" " + repr(self._item) + ",\n") |
|
73 |
def end(self): |
|
74 |
baseformatter.end(self) |
|
75 |
self._ui.write("}\n") |
|
76 |
||
77 |
def formatter(ui, topic, opts): |
|
78 |
if ui.configbool('ui', 'formatdebug'): |
|
79 |
return debugformatter(ui, topic, opts) |
|
80 |
return plainformatter(ui, topic, opts) |