Mercurial > public > mercurial-scm > hg-stable
diff mercurial/formatter.py @ 41996:77ef3498ceb3
template: add CBOR output format
The whole output is wrapped as an array just like the other serialization
formats. It's an indefinite-length array since the size is unknown while
encoding. Maybe we can add 'cbor-stream' (and 'pickle-stream') as needed.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 10 Mar 2019 12:57:24 +0900 |
parents | 1159031ada1e |
children | 2372284d9457 |
line wrap: on
line diff
--- a/mercurial/formatter.py Tue Mar 19 23:00:07 2019 -0700 +++ b/mercurial/formatter.py Sun Mar 10 12:57:24 2019 +0900 @@ -130,6 +130,7 @@ util, ) from .utils import ( + cborutil, dateutil, stringutil, ) @@ -341,6 +342,18 @@ baseformatter.end(self) self._out.write(pickle.dumps(self._data)) +class cborformatter(baseformatter): + '''serialize items as an indefinite-length CBOR array''' + def __init__(self, ui, out, topic, opts): + baseformatter.__init__(self, ui, topic, opts, _nullconverter) + self._out = out + self._out.write(cborutil.BEGIN_INDEFINITE_ARRAY) + def _showitem(self): + self._out.write(b''.join(cborutil.streamencode(self._item))) + def end(self): + baseformatter.end(self) + self._out.write(cborutil.BREAK) + class jsonformatter(baseformatter): def __init__(self, ui, out, topic, opts): baseformatter.__init__(self, ui, topic, opts, _nullconverter) @@ -617,7 +630,9 @@ def formatter(ui, out, topic, opts): template = opts.get("template", "") - if template == "json": + if template == "cbor": + return cborformatter(ui, out, topic, opts) + elif template == "json": return jsonformatter(ui, out, topic, opts) elif template == "pickle": return pickleformatter(ui, out, topic, opts)