comparison 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
comparison
equal deleted inserted replaced
41995:6fef387af1da 41996:77ef3498ceb3
128 templater, 128 templater,
129 templateutil, 129 templateutil,
130 util, 130 util,
131 ) 131 )
132 from .utils import ( 132 from .utils import (
133 cborutil,
133 dateutil, 134 dateutil,
134 stringutil, 135 stringutil,
135 ) 136 )
136 137
137 pickle = util.pickle 138 pickle = util.pickle
339 self._data.append(self._item) 340 self._data.append(self._item)
340 def end(self): 341 def end(self):
341 baseformatter.end(self) 342 baseformatter.end(self)
342 self._out.write(pickle.dumps(self._data)) 343 self._out.write(pickle.dumps(self._data))
343 344
345 class cborformatter(baseformatter):
346 '''serialize items as an indefinite-length CBOR array'''
347 def __init__(self, ui, out, topic, opts):
348 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
349 self._out = out
350 self._out.write(cborutil.BEGIN_INDEFINITE_ARRAY)
351 def _showitem(self):
352 self._out.write(b''.join(cborutil.streamencode(self._item)))
353 def end(self):
354 baseformatter.end(self)
355 self._out.write(cborutil.BREAK)
356
344 class jsonformatter(baseformatter): 357 class jsonformatter(baseformatter):
345 def __init__(self, ui, out, topic, opts): 358 def __init__(self, ui, out, topic, opts):
346 baseformatter.__init__(self, ui, topic, opts, _nullconverter) 359 baseformatter.__init__(self, ui, topic, opts, _nullconverter)
347 self._out = out 360 self._out = out
348 self._out.write("[") 361 self._out.write("[")
615 'fctx': _loadfctx, 628 'fctx': _loadfctx,
616 } 629 }
617 630
618 def formatter(ui, out, topic, opts): 631 def formatter(ui, out, topic, opts):
619 template = opts.get("template", "") 632 template = opts.get("template", "")
620 if template == "json": 633 if template == "cbor":
634 return cborformatter(ui, out, topic, opts)
635 elif template == "json":
621 return jsonformatter(ui, out, topic, opts) 636 return jsonformatter(ui, out, topic, opts)
622 elif template == "pickle": 637 elif template == "pickle":
623 return pickleformatter(ui, out, topic, opts) 638 return pickleformatter(ui, out, topic, opts)
624 elif template == "debug": 639 elif template == "debug":
625 return debugformatter(ui, out, topic, opts) 640 return debugformatter(ui, out, topic, opts)