Mercurial > public > mercurial-scm > hg
comparison mercurial/formatter.py @ 22430:968247e8f4ac
formatter: add pickle format
This gives convenient Python2 output. Python 3 users will need encoding=bytes.
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Fri, 12 Sep 2014 18:36:38 -0500 |
parents | 427e80a18ef8 |
children | 2642ce9be6ef |
comparison
equal
deleted
inserted
replaced
22429:7a7eed5176a4 | 22430:968247e8f4ac |
---|---|
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 import cPickle | |
8 from i18n import _ | 9 from i18n import _ |
9 import encoding, util | 10 import encoding, util |
10 | 11 |
11 class baseformatter(object): | 12 class baseformatter(object): |
12 def __init__(self, ui, topic, opts): | 13 def __init__(self, ui, topic, opts): |
75 self._ui.write(" " + repr(self._item) + ",\n") | 76 self._ui.write(" " + repr(self._item) + ",\n") |
76 def end(self): | 77 def end(self): |
77 baseformatter.end(self) | 78 baseformatter.end(self) |
78 self._ui.write("]\n") | 79 self._ui.write("]\n") |
79 | 80 |
81 class pickleformatter(baseformatter): | |
82 def __init__(self, ui, topic, opts): | |
83 baseformatter.__init__(self, ui, topic, opts) | |
84 self._data = [] | |
85 def _showitem(self): | |
86 self._data.append(self._item) | |
87 def end(self): | |
88 baseformatter.end(self) | |
89 self._ui.write(cPickle.dumps(self._data)) | |
90 | |
80 class jsonformatter(baseformatter): | 91 class jsonformatter(baseformatter): |
81 def __init__(self, ui, topic, opts): | 92 def __init__(self, ui, topic, opts): |
82 baseformatter.__init__(self, ui, topic, opts) | 93 baseformatter.__init__(self, ui, topic, opts) |
83 self._ui.write("[") | 94 self._ui.write("[") |
84 self._ui._first = True | 95 self._ui._first = True |
106 | 117 |
107 def formatter(ui, topic, opts): | 118 def formatter(ui, topic, opts): |
108 template = opts.get("template", "") | 119 template = opts.get("template", "") |
109 if template == "json": | 120 if template == "json": |
110 return jsonformatter(ui, topic, opts) | 121 return jsonformatter(ui, topic, opts) |
122 elif template == "pickle": | |
123 return pickleformatter(ui, topic, opts) | |
111 elif template == "debug": | 124 elif template == "debug": |
112 return debugformatter(ui, topic, opts) | 125 return debugformatter(ui, topic, opts) |
113 elif template != "": | 126 elif template != "": |
114 raise util.Abort(_("custom templates not yet supported")) | 127 raise util.Abort(_("custom templates not yet supported")) |
115 elif ui.configbool('ui', 'formatdebug'): | 128 elif ui.configbool('ui', 'formatdebug'): |