comparison frontends/src/jp/output_std.py @ 2296:1a64fd7b648d

jp (standard output): added simple, json and json_raw for OUTPUT_DICT_DICT
author Goffi <goffi@goffi.org>
date Sun, 02 Jul 2017 19:52:21 +0200
parents bf998d8626d9
children 8b37a62336c3
comparison
equal deleted inserted replaced
2295:31f586d6ab16 2296:1a64fd7b648d
36 self.host = host 36 self.host = host
37 host.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print) 37 host.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print)
38 host.register_output(C.OUTPUT_LIST, SIMPLE, self.list) 38 host.register_output(C.OUTPUT_LIST, SIMPLE, self.list)
39 host.register_output(C.OUTPUT_DICT, SIMPLE, self.dict) 39 host.register_output(C.OUTPUT_DICT, SIMPLE, self.dict)
40 host.register_output(C.OUTPUT_LIST_DICT, SIMPLE, self.list_dict) 40 host.register_output(C.OUTPUT_LIST_DICT, SIMPLE, self.list_dict)
41 host.register_output(C.OUTPUT_DICT_DICT, SIMPLE, self.dict_dict)
41 host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print) 42 host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print)
42 43
43 def simple_print(self, data): 44 def simple_print(self, data):
44 self.host.disp(unicode(data)) 45 self.host.disp(unicode(data))
45 46
46 def list(self, data): 47 def list(self, data):
47 self.host.disp(u'\n'.join(data)) 48 self.host.disp(u'\n'.join(data))
48 49
49 def dict(self, data): 50 def dict(self, data, indent=0, header_color=C.A_HEADER):
50 options = self.host.parse_output_options() 51 options = self.host.parse_output_options()
51 self.host.check_output_options({u'no-header'}, options) 52 self.host.check_output_options({u'no-header'}, options)
52 show_header = not u'no-header' in options 53 show_header = not u'no-header' in options
53 for k, v in data.iteritems(): 54 for k, v in data.iteritems():
54 if show_header: 55 if show_header:
55 header = A.color(A.BOLD, A.FG_YELLOW, k) + u': ' 56 header = A.color(header_color, k) + u': '
56 else: 57 else:
57 header = u'' 58 header = u''
58 59
59 self.host.disp((u'{header}{value}'.format(header=header, value=v))) 60 self.host.disp((u'{indent}{header}{value}'.format(
61 indent=indent*u' ',
62 header=header,
63 value=v)))
60 64
61 def list_dict(self, data): 65 def list_dict(self, data):
62 for idx, datum in enumerate(data): 66 for idx, datum in enumerate(data):
63 if idx: 67 if idx:
64 self.host.disp(u'\n') 68 self.host.disp(u'\n')
65 self.dict(datum) 69 self.dict(datum)
70
71 def dict_dict(self, data):
72 for key, sub_dict in data.iteritems():
73 self.host.disp(A.color(C.A_HEADER, key))
74 self.dict(sub_dict, indent=4, header_color=C.A_SUBHEADER)
66 75
67 76
68 class Json(object): 77 class Json(object):
69 """outputs in json format""" 78 """outputs in json format"""
70 79
75 host.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump) 84 host.register_output(C.OUTPUT_LIST, JSON_RAW, self.dump)
76 host.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty) 85 host.register_output(C.OUTPUT_DICT, JSON, self.dump_pretty)
77 host.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump) 86 host.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump)
78 host.register_output(C.OUTPUT_LIST_DICT, JSON, self.dump_pretty) 87 host.register_output(C.OUTPUT_LIST_DICT, JSON, self.dump_pretty)
79 host.register_output(C.OUTPUT_LIST_DICT, JSON_RAW, self.dump) 88 host.register_output(C.OUTPUT_LIST_DICT, JSON_RAW, self.dump)
89 host.register_output(C.OUTPUT_DICT_DICT, JSON, self.dump_pretty)
90 host.register_output(C.OUTPUT_DICT_DICT, JSON_RAW, self.dump)
80 host.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty) 91 host.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty)
81 host.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump) 92 host.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump)
82 93
83 def dump(self, data): 94 def dump(self, data):
84 self.host.disp(json.dumps(data, default=str)) 95 self.host.disp(json.dumps(data, default=str))