Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/formatter.py @ 29886:307b20e5e505
formatter: add context manager interface for convenience
And port "hg files" to test it.
As you can see, extra indent is necessary to port to this API. I don't think
we should switch every fm.formatter() call to "with" statement.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 29 Aug 2016 00:00:05 +0900 |
parents | 5b886289a1ca |
children | e7cacb45c4be |
rev | line source |
---|---|
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 | |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
8 from __future__ import absolute_import |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
9 |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
10 import os |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
11 |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
12 from .i18n import _ |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
13 from .node import ( |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
14 hex, |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
15 short, |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
16 ) |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
17 |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
18 from . import ( |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
19 encoding, |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26373
diff
changeset
|
20 error, |
29690
c3a9cd78b151
formatter: add function to convert list to appropriate format (issue5217)
Yuya Nishihara <yuya@tcha.org>
parents:
29324
diff
changeset
|
21 templatekw, |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
22 templater, |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
23 util, |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
24 ) |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
25 |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
26 pickle = util.pickle |
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
27 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
28 class _nullconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
29 '''convert non-primitive data types to be processed by formatter''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
30 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
31 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
32 '''convert date tuple to appropriate format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
33 return date |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
34 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
35 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
36 '''convert dict or key-value pairs to appropriate dict format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
37 # use plain dict instead of util.sortdict so that data can be |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
38 # serialized as a builtin dict in pickle output |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
39 return dict(data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
40 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
41 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
42 '''convert iterable to appropriate list format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
43 return list(data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
44 |
16134 | 45 class baseformatter(object): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
46 def __init__(self, ui, topic, opts, converter): |
16134 | 47 self._ui = ui |
48 self._topic = topic | |
49 self._style = opts.get("style") | |
50 self._template = opts.get("template") | |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
51 self._converter = converter |
16134 | 52 self._item = None |
22701
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
53 # function to convert node to string suitable for this output |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
54 self.hexfunc = hex |
29886
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
55 def __enter__(self): |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
56 return self |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
57 def __exit__(self, exctype, excvalue, traceback): |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
58 if exctype is None: |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
59 self.end() |
22447
2642ce9be6ef
formatter: correct bool testing which should be __nonzero__ in Python 2
Yuya Nishihara <yuya@tcha.org>
parents:
22430
diff
changeset
|
60 def __nonzero__(self): |
16134 | 61 '''return False if we're not doing real templating so we can |
62 skip extra work''' | |
63 return True | |
64 def _showitem(self): | |
65 '''show a formatted item once all data is collected''' | |
66 pass | |
67 def startitem(self): | |
68 '''begin an item in the format list''' | |
69 if self._item is not None: | |
70 self._showitem() | |
71 self._item = {} | |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
72 def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'): |
29692
2f3f18ad55a2
formatter: add function to convert date tuple to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29690
diff
changeset
|
73 '''convert date tuple to appropriate format''' |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
74 return self._converter.formatdate(date, fmt) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
75 def formatdict(self, data, key='key', value='value', fmt='%s=%s', sep=' '): |
29805
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
76 '''convert dict or key-value pairs to appropriate dict format''' |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
77 return self._converter.formatdict(data, key, value, fmt, sep) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
78 def formatlist(self, data, name, fmt='%s', sep=' '): |
29690
c3a9cd78b151
formatter: add function to convert list to appropriate format (issue5217)
Yuya Nishihara <yuya@tcha.org>
parents:
29324
diff
changeset
|
79 '''convert iterable to appropriate list format''' |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
80 # name is mandatory argument for now, but it could be optional if |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
81 # we have default template keyword, e.g. {item} |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
82 return self._converter.formatlist(data, name, fmt, sep) |
16134 | 83 def data(self, **data): |
84 '''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
|
85 self._item.update(data) |
16134 | 86 def write(self, fields, deftext, *fielddata, **opts): |
87 '''do default text output while assigning data to item''' | |
26372
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
88 fieldkeys = fields.split() |
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
89 assert len(fieldkeys) == len(fielddata) |
26373
aa610ffad4e8
formatter: use dict.update() to set arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
26372
diff
changeset
|
90 self._item.update(zip(fieldkeys, fielddata)) |
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
91 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
92 '''do conditional write (primarily for plain formatter)''' |
26372
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
93 fieldkeys = fields.split() |
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
94 assert len(fieldkeys) == len(fielddata) |
26373
aa610ffad4e8
formatter: use dict.update() to set arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
26372
diff
changeset
|
95 self._item.update(zip(fieldkeys, fielddata)) |
16134 | 96 def plain(self, text, **opts): |
97 '''show raw text for non-templated mode''' | |
98 pass | |
29848
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
99 def nested(self, field): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
100 '''sub formatter to store nested data in the specified field''' |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
101 self._item[field] = data = [] |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
102 return _nestedformatter(self._ui, self._converter, data) |
16134 | 103 def end(self): |
104 '''end output for the formatter''' | |
105 if self._item is not None: | |
106 self._showitem() | |
107 | |
29848
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
108 class _nestedformatter(baseformatter): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
109 '''build sub items and store them in the parent formatter''' |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
110 def __init__(self, ui, converter, data): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
111 baseformatter.__init__(self, ui, topic='', opts={}, converter=converter) |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
112 self._data = data |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
113 def _showitem(self): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
114 self._data.append(self._item) |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
115 |
29805
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
116 def _iteritems(data): |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
117 '''iterate key-value pairs in stable order''' |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
118 if isinstance(data, dict): |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
119 return sorted(data.iteritems()) |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
120 return data |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
121 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
122 class _plainconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
123 '''convert non-primitive data types to text''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
124 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
125 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
126 '''stringify date tuple in the given format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
127 return util.datestr(date, fmt) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
128 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
129 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
130 '''stringify key-value pairs separated by sep''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
131 return sep.join(fmt % (k, v) for k, v in _iteritems(data)) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
132 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
133 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
134 '''stringify iterable separated by sep''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
135 return sep.join(fmt % e for e in data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
136 |
16134 | 137 class plainformatter(baseformatter): |
138 '''the default text output scheme''' | |
139 def __init__(self, ui, topic, opts): | |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
140 baseformatter.__init__(self, ui, topic, opts, _plainconverter) |
22701
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
141 if ui.debugflag: |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
142 self.hexfunc = hex |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
143 else: |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
144 self.hexfunc = short |
22447
2642ce9be6ef
formatter: correct bool testing which should be __nonzero__ in Python 2
Yuya Nishihara <yuya@tcha.org>
parents:
22430
diff
changeset
|
145 def __nonzero__(self): |
16134 | 146 return False |
147 def startitem(self): | |
148 pass | |
149 def data(self, **data): | |
150 pass | |
151 def write(self, fields, deftext, *fielddata, **opts): | |
152 self._ui.write(deftext % fielddata, **opts) | |
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
153 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
154 '''do conditional write''' |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
155 if cond: |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
156 self._ui.write(deftext % fielddata, **opts) |
16134 | 157 def plain(self, text, **opts): |
158 self._ui.write(text, **opts) | |
29848
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
159 def nested(self, field): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
160 # nested data will be directly written to ui |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
161 return self |
16134 | 162 def end(self): |
163 pass | |
164 | |
165 class debugformatter(baseformatter): | |
166 def __init__(self, ui, topic, opts): | |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
167 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
22424
1f72226064b8
formatter: make debug style match Python syntax
Matt Mackall <mpm@selenic.com>
parents:
17909
diff
changeset
|
168 self._ui.write("%s = [\n" % self._topic) |
16134 | 169 def _showitem(self): |
170 self._ui.write(" " + repr(self._item) + ",\n") | |
171 def end(self): | |
172 baseformatter.end(self) | |
22424
1f72226064b8
formatter: make debug style match Python syntax
Matt Mackall <mpm@selenic.com>
parents:
17909
diff
changeset
|
173 self._ui.write("]\n") |
16134 | 174 |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
175 class pickleformatter(baseformatter): |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
176 def __init__(self, ui, topic, opts): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
177 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
178 self._data = [] |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
179 def _showitem(self): |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
180 self._data.append(self._item) |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
181 def end(self): |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
182 baseformatter.end(self) |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
183 self._ui.write(pickle.dumps(self._data)) |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
184 |
22474
9da0ef363861
formatter: extract function that encode values to json string
Yuya Nishihara <yuya@tcha.org>
parents:
22447
diff
changeset
|
185 def _jsonifyobj(v): |
29805
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
186 if isinstance(v, dict): |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
187 xs = ['"%s": %s' % (encoding.jsonescape(k), _jsonifyobj(u)) |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
188 for k, u in sorted(v.iteritems())] |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
189 return '{' + ', '.join(xs) + '}' |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
190 elif isinstance(v, (list, tuple)): |
22475
17eeda31e52b
formatter: have jsonformatter accept tuple as value
Yuya Nishihara <yuya@tcha.org>
parents:
22474
diff
changeset
|
191 return '[' + ', '.join(_jsonifyobj(e) for e in v) + ']' |
24321
0a714a1f7d5c
formatter: convert None to json null
Yuya Nishihara <yuya@tcha.org>
parents:
22701
diff
changeset
|
192 elif v is None: |
0a714a1f7d5c
formatter: convert None to json null
Yuya Nishihara <yuya@tcha.org>
parents:
22701
diff
changeset
|
193 return 'null' |
22674
06c8b58647b9
formatter: convert booleans to json
Yuya Nishihara <yuya@tcha.org>
parents:
22476
diff
changeset
|
194 elif v is True: |
06c8b58647b9
formatter: convert booleans to json
Yuya Nishihara <yuya@tcha.org>
parents:
22476
diff
changeset
|
195 return 'true' |
06c8b58647b9
formatter: convert booleans to json
Yuya Nishihara <yuya@tcha.org>
parents:
22476
diff
changeset
|
196 elif v is False: |
06c8b58647b9
formatter: convert booleans to json
Yuya Nishihara <yuya@tcha.org>
parents:
22476
diff
changeset
|
197 return 'false' |
22476
a0829ec34dbd
formatter: convert float value to json
Yuya Nishihara <yuya@tcha.org>
parents:
22475
diff
changeset
|
198 elif isinstance(v, (int, float)): |
a0829ec34dbd
formatter: convert float value to json
Yuya Nishihara <yuya@tcha.org>
parents:
22475
diff
changeset
|
199 return str(v) |
22474
9da0ef363861
formatter: extract function that encode values to json string
Yuya Nishihara <yuya@tcha.org>
parents:
22447
diff
changeset
|
200 else: |
9da0ef363861
formatter: extract function that encode values to json string
Yuya Nishihara <yuya@tcha.org>
parents:
22447
diff
changeset
|
201 return '"%s"' % encoding.jsonescape(v) |
9da0ef363861
formatter: extract function that encode values to json string
Yuya Nishihara <yuya@tcha.org>
parents:
22447
diff
changeset
|
202 |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
203 class jsonformatter(baseformatter): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
204 def __init__(self, ui, topic, opts): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
205 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
206 self._ui.write("[") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
207 self._ui._first = True |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
208 def _showitem(self): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
209 if self._ui._first: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
210 self._ui._first = False |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
211 else: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
212 self._ui.write(",") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
213 |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
214 self._ui.write("\n {\n") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
215 first = True |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
216 for k, v in sorted(self._item.items()): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
217 if first: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
218 first = False |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
219 else: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
220 self._ui.write(",\n") |
22474
9da0ef363861
formatter: extract function that encode values to json string
Yuya Nishihara <yuya@tcha.org>
parents:
22447
diff
changeset
|
221 self._ui.write(' "%s": %s' % (k, _jsonifyobj(v))) |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
222 self._ui.write("\n }") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
223 def end(self): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
224 baseformatter.end(self) |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
225 self._ui.write("\n]\n") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
226 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
227 class _templateconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
228 '''convert non-primitive data types to be processed by templater''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
229 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
230 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
231 '''return date tuple''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
232 return date |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
233 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
234 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
235 '''build object that can be evaluated as either plain string or dict''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
236 data = util.sortdict(_iteritems(data)) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
237 def f(): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
238 yield _plainconverter.formatdict(data, key, value, fmt, sep) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
239 return templatekw._hybrid(f(), data, lambda k: {key: k, value: data[k]}, |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
240 lambda d: fmt % (d[key], d[value])) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
241 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
242 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
243 '''build object that can be evaluated as either plain string or list''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
244 data = list(data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
245 def f(): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
246 yield _plainconverter.formatlist(data, name, fmt, sep) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
247 return templatekw._hybrid(f(), data, lambda x: {name: x}, |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
248 lambda d: fmt % d[name]) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
249 |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
250 class templateformatter(baseformatter): |
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
251 def __init__(self, ui, topic, opts): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
252 baseformatter.__init__(self, ui, topic, opts, _templateconverter) |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
253 self._topic = topic |
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
254 self._t = gettemplater(ui, topic, opts.get('template', '')) |
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
255 def _showitem(self): |
28384
3356bf61fa25
formatter: make labels work with templated output
Kostia Balytskyi <ikostia@fb.com>
parents:
26587
diff
changeset
|
256 g = self._t(self._topic, ui=self._ui, **self._item) |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
257 self._ui.write(templater.stringify(g)) |
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
258 |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
259 def lookuptemplate(ui, topic, tmpl): |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
260 # looks like a literal template? |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
261 if '{' in tmpl: |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
262 return tmpl, None |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
263 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
264 # perhaps a stock style? |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
265 if not os.path.split(tmpl)[0]: |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
266 mapname = (templater.templatepath('map-cmdline.' + tmpl) |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
267 or templater.templatepath(tmpl)) |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
268 if mapname and os.path.isfile(mapname): |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
269 return None, mapname |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
270 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
271 # perhaps it's a reference to [templates] |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
272 t = ui.config('templates', tmpl) |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
273 if t: |
28630
bf35644b9f3a
templater: relax unquotestring() to fall back to bare string
Yuya Nishihara <yuya@tcha.org>
parents:
28384
diff
changeset
|
274 return templater.unquotestring(t), None |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
275 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
276 if tmpl == 'list': |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
277 ui.write(_("available styles: %s\n") % templater.stylelist()) |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26373
diff
changeset
|
278 raise error.Abort(_("specify a template")) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
279 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
280 # perhaps it's a path to a map or a template |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
281 if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl): |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
282 # is it a mapfile for a style? |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
283 if os.path.basename(tmpl).startswith("map-"): |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
284 return None, os.path.realpath(tmpl) |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
285 tmpl = open(tmpl).read() |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
286 return tmpl, None |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
287 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
288 # constant string? |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
289 return tmpl, None |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
290 |
25512
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
291 def gettemplater(ui, topic, spec): |
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
292 tmpl, mapfile = lookuptemplate(ui, topic, spec) |
28954
f97a0bcfd7a1
templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents:
28630
diff
changeset
|
293 assert not (tmpl and mapfile) |
f97a0bcfd7a1
templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents:
28630
diff
changeset
|
294 if mapfile: |
f97a0bcfd7a1
templater: separate function to create templater from map file (API)
Yuya Nishihara <yuya@tcha.org>
parents:
28630
diff
changeset
|
295 return templater.templater.frommapfile(mapfile) |
28955
78759f78a44e
templater: factor out function that creates templater from string template
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
296 return maketemplater(ui, topic, tmpl) |
78759f78a44e
templater: factor out function that creates templater from string template
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
297 |
78759f78a44e
templater: factor out function that creates templater from string template
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
298 def maketemplater(ui, topic, tmpl, filters=None, cache=None): |
78759f78a44e
templater: factor out function that creates templater from string template
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
299 """Create a templater from a string template 'tmpl'""" |
28957
d813132ea361
templater: load and expand aliases by template engine (API) (issue4842)
Yuya Nishihara <yuya@tcha.org>
parents:
28955
diff
changeset
|
300 aliases = ui.configitems('templatealias') |
d813132ea361
templater: load and expand aliases by template engine (API) (issue4842)
Yuya Nishihara <yuya@tcha.org>
parents:
28955
diff
changeset
|
301 t = templater.templater(filters=filters, cache=cache, aliases=aliases) |
25512
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
302 if tmpl: |
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
303 t.cache[topic] = tmpl |
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
304 return t |
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
305 |
16134 | 306 def formatter(ui, topic, opts): |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
307 template = opts.get("template", "") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
308 if template == "json": |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
309 return jsonformatter(ui, topic, opts) |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
310 elif template == "pickle": |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
311 return pickleformatter(ui, topic, opts) |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
312 elif template == "debug": |
16134 | 313 return debugformatter(ui, topic, opts) |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
314 elif template != "": |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
315 return templateformatter(ui, topic, opts) |
25838
31137258ae8b
formatter: mark developer options
Matt Mackall <mpm@selenic.com>
parents:
25513
diff
changeset
|
316 # developer config: ui.formatdebug |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
317 elif ui.configbool('ui', 'formatdebug'): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
318 return debugformatter(ui, topic, opts) |
25838
31137258ae8b
formatter: mark developer options
Matt Mackall <mpm@selenic.com>
parents:
25513
diff
changeset
|
319 # deprecated config: ui.formatjson |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
320 elif ui.configbool('ui', 'formatjson'): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
321 return jsonformatter(ui, topic, opts) |
16134 | 322 return plainformatter(ui, topic, opts) |