##// END OF EJS Templates
formatter: add condwrite method...
Matt Mackall -
r17909:3326fd05 default
parent child Browse files
Show More
@@ -1,72 +1,80 b''
1 # formatter.py - generic output formatting for mercurial
1 # formatter.py - generic output formatting for mercurial
2 #
2 #
3 # Copyright 2012 Matt Mackall <mpm@selenic.com>
3 # Copyright 2012 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
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.
6 # GNU General Public License version 2 or any later version.
7
7
8 class baseformatter(object):
8 class baseformatter(object):
9 def __init__(self, ui, topic, opts):
9 def __init__(self, ui, topic, opts):
10 self._ui = ui
10 self._ui = ui
11 self._topic = topic
11 self._topic = topic
12 self._style = opts.get("style")
12 self._style = opts.get("style")
13 self._template = opts.get("template")
13 self._template = opts.get("template")
14 self._item = None
14 self._item = None
15 def __bool__(self):
15 def __bool__(self):
16 '''return False if we're not doing real templating so we can
16 '''return False if we're not doing real templating so we can
17 skip extra work'''
17 skip extra work'''
18 return True
18 return True
19 def _showitem(self):
19 def _showitem(self):
20 '''show a formatted item once all data is collected'''
20 '''show a formatted item once all data is collected'''
21 pass
21 pass
22 def startitem(self):
22 def startitem(self):
23 '''begin an item in the format list'''
23 '''begin an item in the format list'''
24 if self._item is not None:
24 if self._item is not None:
25 self._showitem()
25 self._showitem()
26 self._item = {}
26 self._item = {}
27 def data(self, **data):
27 def data(self, **data):
28 '''insert data into item that's not shown in default output'''
28 '''insert data into item that's not shown in default output'''
29 self._item.update(data)
29 self._item.update(data)
30 def write(self, fields, deftext, *fielddata, **opts):
30 def write(self, fields, deftext, *fielddata, **opts):
31 '''do default text output while assigning data to item'''
31 '''do default text output while assigning data to item'''
32 for k, v in zip(fields.split(), fielddata):
32 for k, v in zip(fields.split(), fielddata):
33 self._item[k] = v
33 self._item[k] = v
34 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
35 '''do conditional write (primarily for plain formatter)'''
36 for k, v in zip(fields.split(), fielddata):
37 self._item[k] = v
34 def plain(self, text, **opts):
38 def plain(self, text, **opts):
35 '''show raw text for non-templated mode'''
39 '''show raw text for non-templated mode'''
36 pass
40 pass
37 def end(self):
41 def end(self):
38 '''end output for the formatter'''
42 '''end output for the formatter'''
39 if self._item is not None:
43 if self._item is not None:
40 self._showitem()
44 self._showitem()
41
45
42 class plainformatter(baseformatter):
46 class plainformatter(baseformatter):
43 '''the default text output scheme'''
47 '''the default text output scheme'''
44 def __init__(self, ui, topic, opts):
48 def __init__(self, ui, topic, opts):
45 baseformatter.__init__(self, ui, topic, opts)
49 baseformatter.__init__(self, ui, topic, opts)
46 def __bool__(self):
50 def __bool__(self):
47 return False
51 return False
48 def startitem(self):
52 def startitem(self):
49 pass
53 pass
50 def data(self, **data):
54 def data(self, **data):
51 pass
55 pass
52 def write(self, fields, deftext, *fielddata, **opts):
56 def write(self, fields, deftext, *fielddata, **opts):
53 self._ui.write(deftext % fielddata, **opts)
57 self._ui.write(deftext % fielddata, **opts)
58 def condwrite(self, cond, fields, deftext, *fielddata, **opts):
59 '''do conditional write'''
60 if cond:
61 self._ui.write(deftext % fielddata, **opts)
54 def plain(self, text, **opts):
62 def plain(self, text, **opts):
55 self._ui.write(text, **opts)
63 self._ui.write(text, **opts)
56 def end(self):
64 def end(self):
57 pass
65 pass
58
66
59 class debugformatter(baseformatter):
67 class debugformatter(baseformatter):
60 def __init__(self, ui, topic, opts):
68 def __init__(self, ui, topic, opts):
61 baseformatter.__init__(self, ui, topic, opts)
69 baseformatter.__init__(self, ui, topic, opts)
62 self._ui.write("%s = {\n" % self._topic)
70 self._ui.write("%s = {\n" % self._topic)
63 def _showitem(self):
71 def _showitem(self):
64 self._ui.write(" " + repr(self._item) + ",\n")
72 self._ui.write(" " + repr(self._item) + ",\n")
65 def end(self):
73 def end(self):
66 baseformatter.end(self)
74 baseformatter.end(self)
67 self._ui.write("}\n")
75 self._ui.write("}\n")
68
76
69 def formatter(ui, topic, opts):
77 def formatter(ui, topic, opts):
70 if ui.configbool('ui', 'formatdebug'):
78 if ui.configbool('ui', 'formatdebug'):
71 return debugformatter(ui, topic, opts)
79 return debugformatter(ui, topic, opts)
72 return plainformatter(ui, topic, opts)
80 return plainformatter(ui, topic, opts)
General Comments 0
You need to be logged in to leave comments. Login now