# HG changeset patch # User Goffi # Date 1543657681 -3600 # Node ID b156b78b8f9aeb46de267ff2c1f86e003f238b2f # Parent bad70aa70c87161b40b70be322ed3a68b8203d52 jp (output): new OUTPUT_MESS output to handle data containing chat messages diff -r bad70aa70c87 -r b156b78b8f9a sat_frontends/jp/constants.py --- a/sat_frontends/jp/constants.py Sat Dec 01 10:47:59 2018 +0100 +++ b/sat_frontends/jp/constants.py Sat Dec 01 10:48:01 2018 +0100 @@ -31,6 +31,7 @@ OUTPUT_LIST = u"list" OUTPUT_LIST_DICT = u"list_dict" # list of dictionaries OUTPUT_DICT_DICT = u"dict_dict" # dict of nested dictionaries + OUTPUT_MESS = u"mess" # messages (chat) OUTPUT_COMPLEX = u"complex" # complex data (e.g. multi-level dictionary) OUTPUT_XML = u"xml" # XML node (as unicode string) OUTPUT_LIST_XML = u"list_xml" # list of XML nodes (as unicode strings) @@ -42,6 +43,7 @@ OUTPUT_LIST, OUTPUT_LIST_DICT, OUTPUT_DICT_DICT, + OUTPUT_MESS, OUTPUT_COMPLEX, OUTPUT_XML, OUTPUT_LIST_XML, diff -r bad70aa70c87 -r b156b78b8f9a sat_frontends/jp/output_std.py --- a/sat_frontends/jp/output_std.py Sat Dec 01 10:47:59 2018 +0100 +++ b/sat_frontends/jp/output_std.py Sat Dec 01 10:48:01 2018 +0100 @@ -20,7 +20,9 @@ from sat_frontends.jp.constants import Const as C +from sat_frontends.tools import jid from sat.tools.common.ansi import ANSI as A +from sat.tools.common import date_utils import json __outputs__ = ["Simple", "Json"] @@ -39,6 +41,7 @@ host.register_output(C.OUTPUT_DICT, SIMPLE, self.dict) host.register_output(C.OUTPUT_LIST_DICT, SIMPLE, self.list_dict) host.register_output(C.OUTPUT_DICT_DICT, SIMPLE, self.dict_dict) + host.register_output(C.OUTPUT_MESS, SIMPLE, self.messages) host.register_output(C.OUTPUT_COMPLEX, SIMPLE, self.simple_print) def simple_print(self, data): @@ -76,6 +79,30 @@ self.host.disp(A.color(C.A_HEADER, key)) self.dict(sub_dict, indent=4, header_color=C.A_SUBHEADER) + def messages(self, data): + # TODO: handle lang, and non chat message (normal, headline) + for mess_data in data: + (uid, timestamp, from_jid, to_jid, message, subject, mess_type, + extra) = mess_data + time_str = date_utils.date_fmt(timestamp, u"auto_day", + tz_info=date_utils.TZ_LOCAL) + from_jid = jid.JID(from_jid) + if mess_type == C.MESS_TYPE_GROUPCHAT: + nick = from_jid.resource + else: + nick = from_jid.node + + if self.host.own_jid is not None and self.host.own_jid.bare == from_jid.bare: + nick_color = A.BOLD + A.FG_BLUE + else: + nick_color = A.BOLD + A.FG_YELLOW + message = message.values()[0] if message else u"" + + self.host.disp(A.color( + A.FG_CYAN, u'['+time_str+u'] ', + nick_color, nick, A.RESET, A.BOLD, u'> ', + A.RESET, message)) + class Json(object): """outputs in json format""" @@ -91,6 +118,8 @@ host.register_output(C.OUTPUT_LIST_DICT, JSON_RAW, self.dump) host.register_output(C.OUTPUT_DICT_DICT, JSON, self.dump_pretty) host.register_output(C.OUTPUT_DICT_DICT, JSON_RAW, self.dump) + host.register_output(C.OUTPUT_MESS, JSON, self.dump_pretty) + host.register_output(C.OUTPUT_MESS, JSON_RAW, self.dump) host.register_output(C.OUTPUT_COMPLEX, JSON, self.dump_pretty) host.register_output(C.OUTPUT_COMPLEX, JSON_RAW, self.dump)