Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
2155:cee6561ad086 | 2156:8f96c242fa89 |
---|---|
20 | 20 |
21 | 21 |
22 from sat_frontends.jp.constants import Const as C | 22 from sat_frontends.jp.constants import Const as C |
23 import json | 23 import json |
24 | 24 |
25 __outputs__ = ["Default", "Json"] | 25 __outputs__ = ["Simple", "Json"] |
26 DEFAULT = u'default' | 26 SIMPLE = u'simple' |
27 JSON = u'json' | 27 JSON = u'json' |
28 JSON_RAW = u'json_raw' | 28 JSON_RAW = u'json_raw' |
29 | 29 |
30 | 30 |
31 class Default(object): | 31 class Simple(object): |
32 """Default outputs""" | 32 """Default outputs""" |
33 | 33 |
34 def __init__(self, jp): | 34 def __init__(self, jp): |
35 jp.register_output(C.OUTPUT_TEXT, DEFAULT, self.text) | 35 jp.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print) |
36 jp.register_output(C.OUTPUT_LIST, DEFAULT, self.list) | 36 jp.register_output(C.OUTPUT_LIST, SIMPLE, self.list) |
37 jp.register_output(C.OUTPUT_DICT, SIMPLE, self.dict) | |
38 jp.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print) | |
37 | 39 |
38 def text(self, data): | 40 def simple_print(self, data): |
39 print data | 41 print data |
40 | 42 |
41 def list(self, data): | 43 def list(self, data): |
42 print u'\n'.join(data) | 44 print u'\n'.join(data) |
45 | |
46 def dict(self, data): | |
47 for k, v in data: | |
48 print(u'{key}: {value}'.format(key=k, value=v)) | |
43 | 49 |
44 | 50 |
45 class Json(object): | 51 class Json(object): |
46 """outputs in json format""" | 52 """outputs in json format""" |
47 | 53 |
48 def __init__(self, jp): | 54 def __init__(self, jp): |
55 jp.register_output(C.OUTPUT_TEXT, JSON, self.dump) | |
49 jp.register_output(C.OUTPUT_LIST, JSON, self.dump_pretty) | 56 jp.register_output(C.OUTPUT_LIST, JSON, self.dump_pretty) |
50 jp.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump) | 57 jp.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump) |
51 jp.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty) | 58 jp.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty) |
52 jp.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump) | 59 jp.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump) |
60 jp.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty) | |
61 jp.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump) | |
53 | 62 |
54 def dump(self, data): | 63 def dump(self, data): |
55 print json.dumps(data) | 64 print json.dumps(data, default=str) |
56 | 65 |
57 def dump_pretty(self, data): | 66 def dump_pretty(self, data): |
58 print json.dumps(data, indent=4) | 67 print json.dumps(data, indent=4, default=str) |