Show More
@@ -1,137 +1,137 | |||
|
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 | import cPickle |
|
9 | 9 | from i18n import _ |
|
10 | 10 | import encoding, util |
|
11 | 11 | |
|
12 | 12 | class baseformatter(object): |
|
13 | 13 | def __init__(self, ui, topic, opts): |
|
14 | 14 | self._ui = ui |
|
15 | 15 | self._topic = topic |
|
16 | 16 | self._style = opts.get("style") |
|
17 | 17 | self._template = opts.get("template") |
|
18 | 18 | self._item = None |
|
19 | 19 | def __nonzero__(self): |
|
20 | 20 | '''return False if we're not doing real templating so we can |
|
21 | 21 | skip extra work''' |
|
22 | 22 | return True |
|
23 | 23 | def _showitem(self): |
|
24 | 24 | '''show a formatted item once all data is collected''' |
|
25 | 25 | pass |
|
26 | 26 | def startitem(self): |
|
27 | 27 | '''begin an item in the format list''' |
|
28 | 28 | if self._item is not None: |
|
29 | 29 | self._showitem() |
|
30 | 30 | self._item = {} |
|
31 | 31 | def data(self, **data): |
|
32 | 32 | '''insert data into item that's not shown in default output''' |
|
33 | 33 | self._item.update(data) |
|
34 | 34 | def write(self, fields, deftext, *fielddata, **opts): |
|
35 | 35 | '''do default text output while assigning data to item''' |
|
36 | 36 | for k, v in zip(fields.split(), fielddata): |
|
37 | 37 | self._item[k] = v |
|
38 | 38 | def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
|
39 | 39 | '''do conditional write (primarily for plain formatter)''' |
|
40 | 40 | for k, v in zip(fields.split(), fielddata): |
|
41 | 41 | self._item[k] = v |
|
42 | 42 | def plain(self, text, **opts): |
|
43 | 43 | '''show raw text for non-templated mode''' |
|
44 | 44 | pass |
|
45 | 45 | def end(self): |
|
46 | 46 | '''end output for the formatter''' |
|
47 | 47 | if self._item is not None: |
|
48 | 48 | self._showitem() |
|
49 | 49 | |
|
50 | 50 | class plainformatter(baseformatter): |
|
51 | 51 | '''the default text output scheme''' |
|
52 | 52 | def __init__(self, ui, topic, opts): |
|
53 | 53 | baseformatter.__init__(self, ui, topic, opts) |
|
54 | 54 | def __nonzero__(self): |
|
55 | 55 | return False |
|
56 | 56 | def startitem(self): |
|
57 | 57 | pass |
|
58 | 58 | def data(self, **data): |
|
59 | 59 | pass |
|
60 | 60 | def write(self, fields, deftext, *fielddata, **opts): |
|
61 | 61 | self._ui.write(deftext % fielddata, **opts) |
|
62 | 62 | def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
|
63 | 63 | '''do conditional write''' |
|
64 | 64 | if cond: |
|
65 | 65 | self._ui.write(deftext % fielddata, **opts) |
|
66 | 66 | def plain(self, text, **opts): |
|
67 | 67 | self._ui.write(text, **opts) |
|
68 | 68 | def end(self): |
|
69 | 69 | pass |
|
70 | 70 | |
|
71 | 71 | class debugformatter(baseformatter): |
|
72 | 72 | def __init__(self, ui, topic, opts): |
|
73 | 73 | baseformatter.__init__(self, ui, topic, opts) |
|
74 | 74 | self._ui.write("%s = [\n" % self._topic) |
|
75 | 75 | def _showitem(self): |
|
76 | 76 | self._ui.write(" " + repr(self._item) + ",\n") |
|
77 | 77 | def end(self): |
|
78 | 78 | baseformatter.end(self) |
|
79 | 79 | self._ui.write("]\n") |
|
80 | 80 | |
|
81 | 81 | class pickleformatter(baseformatter): |
|
82 | 82 | def __init__(self, ui, topic, opts): |
|
83 | 83 | baseformatter.__init__(self, ui, topic, opts) |
|
84 | 84 | self._data = [] |
|
85 | 85 | def _showitem(self): |
|
86 | 86 | self._data.append(self._item) |
|
87 | 87 | def end(self): |
|
88 | 88 | baseformatter.end(self) |
|
89 | 89 | self._ui.write(cPickle.dumps(self._data)) |
|
90 | 90 | |
|
91 | 91 | def _jsonifyobj(v): |
|
92 | 92 | if isinstance(v, tuple): |
|
93 | 93 | return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']' |
|
94 | elif isinstance(v, int): | |
|
95 |
return |
|
|
94 | elif isinstance(v, (int, float)): | |
|
95 | return str(v) | |
|
96 | 96 | else: |
|
97 | 97 | return '"%s"' % encoding.jsonescape(v) |
|
98 | 98 | |
|
99 | 99 | class jsonformatter(baseformatter): |
|
100 | 100 | def __init__(self, ui, topic, opts): |
|
101 | 101 | baseformatter.__init__(self, ui, topic, opts) |
|
102 | 102 | self._ui.write("[") |
|
103 | 103 | self._ui._first = True |
|
104 | 104 | def _showitem(self): |
|
105 | 105 | if self._ui._first: |
|
106 | 106 | self._ui._first = False |
|
107 | 107 | else: |
|
108 | 108 | self._ui.write(",") |
|
109 | 109 | |
|
110 | 110 | self._ui.write("\n {\n") |
|
111 | 111 | first = True |
|
112 | 112 | for k, v in sorted(self._item.items()): |
|
113 | 113 | if first: |
|
114 | 114 | first = False |
|
115 | 115 | else: |
|
116 | 116 | self._ui.write(",\n") |
|
117 | 117 | self._ui.write(' "%s": %s' % (k, _jsonifyobj(v))) |
|
118 | 118 | self._ui.write("\n }") |
|
119 | 119 | def end(self): |
|
120 | 120 | baseformatter.end(self) |
|
121 | 121 | self._ui.write("\n]\n") |
|
122 | 122 | |
|
123 | 123 | def formatter(ui, topic, opts): |
|
124 | 124 | template = opts.get("template", "") |
|
125 | 125 | if template == "json": |
|
126 | 126 | return jsonformatter(ui, topic, opts) |
|
127 | 127 | elif template == "pickle": |
|
128 | 128 | return pickleformatter(ui, topic, opts) |
|
129 | 129 | elif template == "debug": |
|
130 | 130 | return debugformatter(ui, topic, opts) |
|
131 | 131 | elif template != "": |
|
132 | 132 | raise util.Abort(_("custom templates not yet supported")) |
|
133 | 133 | elif ui.configbool('ui', 'formatdebug'): |
|
134 | 134 | return debugformatter(ui, topic, opts) |
|
135 | 135 | elif ui.configbool('ui', 'formatjson'): |
|
136 | 136 | return jsonformatter(ui, topic, opts) |
|
137 | 137 | return plainformatter(ui, topic, opts) |
General Comments 0
You need to be logged in to leave comments.
Login now