comparison libervia/cli/output_std.py @ 4075:47401850dec6

refactoring: rename `libervia.frontends.jp` to `libervia.cli`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:54:26 +0200
parents libervia/frontends/jp/output_std.py@26b7ed2817da
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4074:26b7ed2817da 4075:47401850dec6
1 #! /usr/bin/env python3
2
3
4 # Libervia CLI
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """Standard outputs"""
20
21
22 from libervia.cli.constants import Const as C
23 from libervia.frontends.tools import jid
24 from libervia.backend.tools.common.ansi import ANSI as A
25 from libervia.backend.tools.common import date_utils
26 import json
27
28 __outputs__ = ["Simple", "Json"]
29
30
31 class Simple(object):
32 """Default outputs"""
33
34 def __init__(self, host):
35 self.host = host
36 host.register_output(C.OUTPUT_TEXT, C.OUTPUT_NAME_SIMPLE, self.simple_print)
37 host.register_output(C.OUTPUT_LIST, C.OUTPUT_NAME_SIMPLE, self.list)
38 host.register_output(C.OUTPUT_DICT, C.OUTPUT_NAME_SIMPLE, self.dict)
39 host.register_output(C.OUTPUT_LIST_DICT, C.OUTPUT_NAME_SIMPLE, self.list_dict)
40 host.register_output(C.OUTPUT_DICT_DICT, C.OUTPUT_NAME_SIMPLE, self.dict_dict)
41 host.register_output(C.OUTPUT_MESS, C.OUTPUT_NAME_SIMPLE, self.messages)
42 host.register_output(C.OUTPUT_COMPLEX, C.OUTPUT_NAME_SIMPLE, self.simple_print)
43
44 def simple_print(self, data):
45 self.host.disp(str(data))
46
47 def list(self, data):
48 self.host.disp("\n".join(data))
49
50 def dict(self, data, indent=0, header_color=C.A_HEADER):
51 options = self.host.parse_output_options()
52 self.host.check_output_options({"no-header"}, options)
53 show_header = not "no-header" in options
54 for k, v in data.items():
55 if show_header:
56 header = A.color(header_color, k) + ": "
57 else:
58 header = ""
59
60 self.host.disp(
61 (
62 "{indent}{header}{value}".format(
63 indent=indent * " ", header=header, value=v
64 )
65 )
66 )
67
68 def list_dict(self, data):
69 for idx, datum in enumerate(data):
70 if idx:
71 self.host.disp("\n")
72 self.dict(datum)
73
74 def dict_dict(self, data):
75 for key, sub_dict in data.items():
76 self.host.disp(A.color(C.A_HEADER, key))
77 self.dict(sub_dict, indent=4, header_color=C.A_SUBHEADER)
78
79 def messages(self, data):
80 # TODO: handle lang, and non chat message (normal, headline)
81 for mess_data in data:
82 (uid, timestamp, from_jid, to_jid, message, subject, mess_type,
83 extra) = mess_data
84 time_str = date_utils.date_fmt(timestamp, "auto_day",
85 tz_info=date_utils.TZ_LOCAL)
86 from_jid = jid.JID(from_jid)
87 if mess_type == C.MESS_TYPE_GROUPCHAT:
88 nick = from_jid.resource
89 else:
90 nick = from_jid.node
91
92 if self.host.own_jid is not None and self.host.own_jid.bare == from_jid.bare:
93 nick_color = A.BOLD + A.FG_BLUE
94 else:
95 nick_color = A.BOLD + A.FG_YELLOW
96 message = list(message.values())[0] if message else ""
97
98 self.host.disp(A.color(
99 A.FG_CYAN, '['+time_str+'] ',
100 nick_color, nick, A.RESET, A.BOLD, '> ',
101 A.RESET, message))
102
103
104 class Json(object):
105 """outputs in json format"""
106
107 def __init__(self, host):
108 self.host = host
109 host.register_output(C.OUTPUT_TEXT, C.OUTPUT_NAME_JSON, self.dump)
110 host.register_output(C.OUTPUT_LIST, C.OUTPUT_NAME_JSON, self.dump_pretty)
111 host.register_output(C.OUTPUT_LIST, C.OUTPUT_NAME_JSON_RAW, self.dump)
112 host.register_output(C.OUTPUT_DICT, C.OUTPUT_NAME_JSON, self.dump_pretty)
113 host.register_output(C.OUTPUT_DICT, C.OUTPUT_NAME_JSON_RAW, self.dump)
114 host.register_output(C.OUTPUT_LIST_DICT, C.OUTPUT_NAME_JSON, self.dump_pretty)
115 host.register_output(C.OUTPUT_LIST_DICT, C.OUTPUT_NAME_JSON_RAW, self.dump)
116 host.register_output(C.OUTPUT_DICT_DICT, C.OUTPUT_NAME_JSON, self.dump_pretty)
117 host.register_output(C.OUTPUT_DICT_DICT, C.OUTPUT_NAME_JSON_RAW, self.dump)
118 host.register_output(C.OUTPUT_MESS, C.OUTPUT_NAME_JSON, self.dump_pretty)
119 host.register_output(C.OUTPUT_MESS, C.OUTPUT_NAME_JSON_RAW, self.dump)
120 host.register_output(C.OUTPUT_COMPLEX, C.OUTPUT_NAME_JSON, self.dump_pretty)
121 host.register_output(C.OUTPUT_COMPLEX, C.OUTPUT_NAME_JSON_RAW, self.dump)
122
123 def dump(self, data):
124 self.host.disp(json.dumps(data, default=str))
125
126 def dump_pretty(self, data):
127 self.host.disp(json.dumps(data, indent=4, default=str))