Mercurial > libervia-backend
diff frontends/src/jp/output_std.py @ 2156:8f96c242fa89
jp (outputs): added OUTPUT_COMPLEX type for data which are more complex than key/value dictionaries
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Feb 2017 00:51:33 +0100 |
parents | 227a4e617549 |
children | 62dfa6e02f54 |
line wrap: on
line diff
--- a/frontends/src/jp/output_std.py Thu Feb 16 00:49:46 2017 +0100 +++ b/frontends/src/jp/output_std.py Thu Feb 16 00:51:33 2017 +0100 @@ -22,37 +22,46 @@ from sat_frontends.jp.constants import Const as C import json -__outputs__ = ["Default", "Json"] -DEFAULT = u'default' +__outputs__ = ["Simple", "Json"] +SIMPLE = u'simple' JSON = u'json' JSON_RAW = u'json_raw' -class Default(object): +class Simple(object): """Default outputs""" def __init__(self, jp): - jp.register_output(C.OUTPUT_TEXT, DEFAULT, self.text) - jp.register_output(C.OUTPUT_LIST, DEFAULT, self.list) + jp.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print) + jp.register_output(C.OUTPUT_LIST, SIMPLE, self.list) + jp.register_output(C.OUTPUT_DICT, SIMPLE, self.dict) + jp.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print) - def text(self, data): + def simple_print(self, data): print data def list(self, data): print u'\n'.join(data) + def dict(self, data): + for k, v in data: + print(u'{key}: {value}'.format(key=k, value=v)) + class Json(object): """outputs in json format""" def __init__(self, jp): + jp.register_output(C.OUTPUT_TEXT, JSON, self.dump) jp.register_output(C.OUTPUT_LIST, JSON, self.dump_pretty) jp.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump) jp.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty) jp.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump) + jp.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty) + jp.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump) def dump(self, data): - print json.dumps(data) + print json.dumps(data, default=str) def dump_pretty(self, data): - print json.dumps(data, indent=4) + print json.dumps(data, indent=4, default=str)