jp (output): new OUTPUT_MESS output to handle data containing chat messages

This commit is contained in:
Goffi 2018-12-01 10:48:01 +01:00
commit c4997f690c
2 changed files with 31 additions and 0 deletions

View file

@ -31,6 +31,7 @@ class Const(constants.Const):
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 @@ class Const(constants.Const):
OUTPUT_LIST,
OUTPUT_LIST_DICT,
OUTPUT_DICT_DICT,
OUTPUT_MESS,
OUTPUT_COMPLEX,
OUTPUT_XML,
OUTPUT_LIST_XML,

View file

@ -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 @@ class Simple(object):
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 @@ class Simple(object):
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 @@ class Json(object):
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)