comparison sat_frontends/jp/output_std.py @ 2707:b156b78b8f9a

jp (output): new OUTPUT_MESS output to handle data containing chat messages
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 10:48:01 +0100
parents 56f94936df1e
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2706:bad70aa70c87 2707:b156b78b8f9a
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """Standard outputs""" 19 """Standard outputs"""
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 from sat_frontends.tools import jid
23 from sat.tools.common.ansi import ANSI as A 24 from sat.tools.common.ansi import ANSI as A
25 from sat.tools.common import date_utils
24 import json 26 import json
25 27
26 __outputs__ = ["Simple", "Json"] 28 __outputs__ = ["Simple", "Json"]
27 SIMPLE = u"simple" 29 SIMPLE = u"simple"
28 JSON = u"json" 30 JSON = u"json"
37 host.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print) 39 host.register_output(C.OUTPUT_TEXT, SIMPLE, self.simple_print)
38 host.register_output(C.OUTPUT_LIST, SIMPLE, self.list) 40 host.register_output(C.OUTPUT_LIST, SIMPLE, self.list)
39 host.register_output(C.OUTPUT_DICT, SIMPLE, self.dict) 41 host.register_output(C.OUTPUT_DICT, SIMPLE, self.dict)
40 host.register_output(C.OUTPUT_LIST_DICT, SIMPLE, self.list_dict) 42 host.register_output(C.OUTPUT_LIST_DICT, SIMPLE, self.list_dict)
41 host.register_output(C.OUTPUT_DICT_DICT, SIMPLE, self.dict_dict) 43 host.register_output(C.OUTPUT_DICT_DICT, SIMPLE, self.dict_dict)
44 host.register_output(C.OUTPUT_MESS, SIMPLE, self.messages)
42 host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print) 45 host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print)
43 46
44 def simple_print(self, data): 47 def simple_print(self, data):
45 self.host.disp(unicode(data)) 48 self.host.disp(unicode(data))
46 49
74 def dict_dict(self, data): 77 def dict_dict(self, data):
75 for key, sub_dict in data.iteritems(): 78 for key, sub_dict in data.iteritems():
76 self.host.disp(A.color(C.A_HEADER, key)) 79 self.host.disp(A.color(C.A_HEADER, key))
77 self.dict(sub_dict, indent=4, header_color=C.A_SUBHEADER) 80 self.dict(sub_dict, indent=4, header_color=C.A_SUBHEADER)
78 81
82 def messages(self, data):
83 # TODO: handle lang, and non chat message (normal, headline)
84 for mess_data in data:
85 (uid, timestamp, from_jid, to_jid, message, subject, mess_type,
86 extra) = mess_data
87 time_str = date_utils.date_fmt(timestamp, u"auto_day",
88 tz_info=date_utils.TZ_LOCAL)
89 from_jid = jid.JID(from_jid)
90 if mess_type == C.MESS_TYPE_GROUPCHAT:
91 nick = from_jid.resource
92 else:
93 nick = from_jid.node
94
95 if self.host.own_jid is not None and self.host.own_jid.bare == from_jid.bare:
96 nick_color = A.BOLD + A.FG_BLUE
97 else:
98 nick_color = A.BOLD + A.FG_YELLOW
99 message = message.values()[0] if message else u""
100
101 self.host.disp(A.color(
102 A.FG_CYAN, u'['+time_str+u'] ',
103 nick_color, nick, A.RESET, A.BOLD, u'> ',
104 A.RESET, message))
105
79 106
80 class Json(object): 107 class Json(object):
81 """outputs in json format""" 108 """outputs in json format"""
82 109
83 def __init__(self, host): 110 def __init__(self, host):
89 host.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump) 116 host.register_output(C.OUTPUT_DICT, JSON_RAW, self.dump)
90 host.register_output(C.OUTPUT_LIST_DICT, JSON, self.dump_pretty) 117 host.register_output(C.OUTPUT_LIST_DICT, JSON, self.dump_pretty)
91 host.register_output(C.OUTPUT_LIST_DICT, JSON_RAW, self.dump) 118 host.register_output(C.OUTPUT_LIST_DICT, JSON_RAW, self.dump)
92 host.register_output(C.OUTPUT_DICT_DICT, JSON, self.dump_pretty) 119 host.register_output(C.OUTPUT_DICT_DICT, JSON, self.dump_pretty)
93 host.register_output(C.OUTPUT_DICT_DICT, JSON_RAW, self.dump) 120 host.register_output(C.OUTPUT_DICT_DICT, JSON_RAW, self.dump)
121 host.register_output(C.OUTPUT_MESS, JSON, self.dump_pretty)
122 host.register_output(C.OUTPUT_MESS, JSON_RAW, self.dump)
94 host.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty) 123 host.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty)
95 host.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump) 124 host.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump)
96 125
97 def dump(self, data): 126 def dump(self, data):
98 self.host.disp(json.dumps(data, default=str)) 127 self.host.disp(json.dumps(data, default=str))