changeset 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 bad70aa70c87
children 0b5deb9a35fd
files sat_frontends/jp/constants.py sat_frontends/jp/output_std.py
diffstat 2 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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,
--- 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)