comparison libervia/cli/cmd_message.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 47401850dec6
children 472a938a46e3
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
53 ) 53 )
54 self.parser.add_argument( 54 self.parser.add_argument(
55 "-n", 55 "-n",
56 "--new-line", 56 "--new-line",
57 action="store_true", 57 action="store_true",
58 help=_( 58 help=_("add a new line at the beginning of the input"),
59 "add a new line at the beginning of the input"
60 ),
61 ) 59 )
62 self.parser.add_argument( 60 self.parser.add_argument(
63 "-S", 61 "-S",
64 "--subject", 62 "--subject",
65 help=_("subject of the message"), 63 help=_("subject of the message"),
72 "--type", 70 "--type",
73 choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,), 71 choices=C.MESS_TYPE_STANDARD + (C.MESS_TYPE_AUTO,),
74 default=C.MESS_TYPE_AUTO, 72 default=C.MESS_TYPE_AUTO,
75 help=_("type of the message"), 73 help=_("type of the message"),
76 ) 74 )
77 self.parser.add_argument("-e", "--encrypt", metavar="ALGORITHM", 75 self.parser.add_argument(
78 help=_("encrypt message using given algorithm")) 76 "-e",
77 "--encrypt",
78 metavar="ALGORITHM",
79 help=_("encrypt message using given algorithm"),
80 )
79 self.parser.add_argument( 81 self.parser.add_argument(
80 "--encrypt-noreplace", 82 "--encrypt-noreplace",
81 action="store_true", 83 action="store_true",
82 help=_("don't replace encryption algorithm if an other one is already used")) 84 help=_("don't replace encryption algorithm if an other one is already used"),
83 self.parser.add_argument( 85 )
84 "-a", "--attach", dest="attachments", action="append", metavar="FILE_PATH", 86 self.parser.add_argument(
85 help=_("add a file as an attachment") 87 "-a",
88 "--attach",
89 dest="attachments",
90 action="append",
91 metavar="FILE_PATH",
92 help=_("add a file as an attachment"),
86 ) 93 )
87 syntax = self.parser.add_mutually_exclusive_group() 94 syntax = self.parser.add_mutually_exclusive_group()
88 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body")) 95 syntax.add_argument("-x", "--xhtml", action="store_true", help=_("XHTML body"))
89 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body")) 96 syntax.add_argument("-r", "--rich", action="store_true", help=_("rich body"))
90 self.parser.add_argument( 97 self.parser.add_argument("jid", help=_("the destination jid"))
91 "jid", help=_("the destination jid")
92 )
93 98
94 async def send_stdin(self, dest_jid): 99 async def send_stdin(self, dest_jid):
95 """Send incomming data on stdin to jabber contact 100 """Send incomming data on stdin to jabber contact
96 101
97 @param dest_jid: destination jid 102 @param dest_jid: destination jid
98 """ 103 """
99 header = "\n" if self.args.new_line else "" 104 header = "\n" if self.args.new_line else ""
100 # FIXME: stdin is not read asynchronously at the moment 105 # FIXME: stdin is not read asynchronously at the moment
101 stdin_lines = [ 106 stdin_lines = [stream for stream in sys.stdin.readlines()]
102 stream for stream in sys.stdin.readlines()
103 ]
104 extra = {} 107 extra = {}
105 if self.args.subject is None: 108 if self.args.subject is None:
106 subject = {} 109 subject = {}
107 else: 110 else:
108 subject = {self.args.subject_lang: self.args.subject} 111 subject = {self.args.subject_lang: self.args.subject}
132 ) 135 )
133 except Exception as e: 136 except Exception as e:
134 self.disp(f"can't send header: {e}", error=True) 137 self.disp(f"can't send header: {e}", error=True)
135 error = True 138 error = True
136 139
137 to_send.extend({self.args.lang: clean_ustr(l.replace("\n", ""))} 140 to_send.extend(
138 for l in stdin_lines) 141 {self.args.lang: clean_ustr(l.replace("\n", ""))} for l in stdin_lines
142 )
139 else: 143 else:
140 # we sent all in a single message 144 # we sent all in a single message
141 if not (self.args.xhtml or self.args.rich): 145 if not (self.args.xhtml or self.args.rich):
142 msg = {self.args.lang: header + clean_ustr("".join(stdin_lines))} 146 msg = {self.args.lang: header + clean_ustr("".join(stdin_lines))}
143 else: 147 else:
164 dest_jid, 168 dest_jid,
165 msg, 169 msg,
166 subject, 170 subject,
167 self.args.type, 171 self.args.type,
168 data_format.serialise(extra), 172 data_format.serialise(extra),
169 profile_key=self.host.profile) 173 profile_key=self.host.profile,
174 )
170 except Exception as e: 175 except Exception as e:
171 self.disp(f"can't send message {msg!r}: {e}", error=True) 176 self.disp(f"can't send message {msg!r}: {e}", error=True)
172 error = True 177 error = True
173 178
174 if error: 179 if error:
192 self.parser.error("You need to use --encrypt if you use --encrypt-noreplace") 197 self.parser.error("You need to use --encrypt if you use --encrypt-noreplace")
193 198
194 if self.args.encrypt is not None: 199 if self.args.encrypt is not None:
195 try: 200 try:
196 namespace = await self.host.bridge.encryption_namespace_get( 201 namespace = await self.host.bridge.encryption_namespace_get(
197 self.args.encrypt) 202 self.args.encrypt
203 )
198 except Exception as e: 204 except Exception as e:
199 self.disp(f"can't get encryption namespace: {e}", error=True) 205 self.disp(f"can't get encryption namespace: {e}", error=True)
200 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 206 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
201 207
202 try: 208 try:
214 220
215 def __init__(self, host): 221 def __init__(self, host):
216 super().__init__(host, "retract", help=_("retract a message")) 222 super().__init__(host, "retract", help=_("retract a message"))
217 223
218 def add_parser_options(self): 224 def add_parser_options(self):
219 self.parser.add_argument( 225 self.parser.add_argument("message_id", help=_("ID of the message (internal ID)"))
220 "message_id",
221 help=_("ID of the message (internal ID)")
222 )
223 226
224 async def start(self): 227 async def start(self):
225 try: 228 try:
226 await self.host.bridge.message_retract( 229 await self.host.bridge.message_retract(self.args.message_id, self.profile)
227 self.args.message_id,
228 self.profile
229 )
230 except Exception as e: 230 except Exception as e:
231 self.disp(f"can't retract message: {e}", error=True) 231 self.disp(f"can't retract message: {e}", error=True)
232 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 232 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
233 else: 233 else:
234 self.disp( 234 self.disp(
235 "message retraction has been requested, please note that this is a " 235 "message retraction has been requested, please note that this is a "
236 "request which can't be enforced (see documentation for details).") 236 "request which can't be enforced (see documentation for details)."
237 )
237 self.host.quit(C.EXIT_OK) 238 self.host.quit(C.EXIT_OK)
238 239
239 240
240 class MAM(base.CommandBase): 241 class MAM(base.CommandBase):
241 242
242 def __init__(self, host): 243 def __init__(self, host):
243 super(MAM, self).__init__( 244 super(MAM, self).__init__(
244 host, "mam", use_output=C.OUTPUT_MESS, use_verbose=True, 245 host,
245 help=_("query archives using MAM")) 246 "mam",
247 use_output=C.OUTPUT_MESS,
248 use_verbose=True,
249 help=_("query archives using MAM"),
250 )
246 251
247 def add_parser_options(self): 252 def add_parser_options(self):
248 self.parser.add_argument( 253 self.parser.add_argument(
249 "-s", "--service", default="", 254 "-s",
250 help=_("jid of the service (default: profile's server")) 255 "--service",
251 self.parser.add_argument( 256 default="",
252 "-S", "--start", dest="mam_start", type=base.date_decoder, 257 help=_("jid of the service (default: profile's server"),
253 help=_( 258 )
254 "start fetching archive from this date (default: from the beginning)")) 259 self.parser.add_argument(
255 self.parser.add_argument( 260 "-S",
256 "-E", "--end", dest="mam_end", type=base.date_decoder, 261 "--start",
257 help=_("end fetching archive after this date (default: no limit)")) 262 dest="mam_start",
258 self.parser.add_argument( 263 type=base.date_decoder,
259 "-W", "--with", dest="mam_with", 264 help=_("start fetching archive from this date (default: from the beginning)"),
260 help=_("retrieve only archives with this jid")) 265 )
261 self.parser.add_argument( 266 self.parser.add_argument(
262 "-m", "--max", dest="rsm_max", type=int, default=20, 267 "-E",
263 help=_("maximum number of items to retrieve, using RSM (default: 20))")) 268 "--end",
269 dest="mam_end",
270 type=base.date_decoder,
271 help=_("end fetching archive after this date (default: no limit)"),
272 )
273 self.parser.add_argument(
274 "-W",
275 "--with",
276 dest="mam_with",
277 help=_("retrieve only archives with this jid"),
278 )
279 self.parser.add_argument(
280 "-m",
281 "--max",
282 dest="rsm_max",
283 type=int,
284 default=20,
285 help=_("maximum number of items to retrieve, using RSM (default: 20))"),
286 )
264 rsm_page_group = self.parser.add_mutually_exclusive_group() 287 rsm_page_group = self.parser.add_mutually_exclusive_group()
265 rsm_page_group.add_argument( 288 rsm_page_group.add_argument(
266 "-a", "--after", dest="rsm_after", 289 "-a",
267 help=_("find page after this item"), metavar='ITEM_ID') 290 "--after",
291 dest="rsm_after",
292 help=_("find page after this item"),
293 metavar="ITEM_ID",
294 )
268 rsm_page_group.add_argument( 295 rsm_page_group.add_argument(
269 "-b", "--before", dest="rsm_before", 296 "-b",
270 help=_("find page before this item"), metavar='ITEM_ID') 297 "--before",
298 dest="rsm_before",
299 help=_("find page before this item"),
300 metavar="ITEM_ID",
301 )
271 rsm_page_group.add_argument( 302 rsm_page_group.add_argument(
272 "--index", dest="rsm_index", type=int, 303 "--index", dest="rsm_index", type=int, help=_("index of the page to retrieve")
273 help=_("index of the page to retrieve")) 304 )
274 305
275 async def start(self): 306 async def start(self):
276 extra = {} 307 extra = {}
277 if self.args.mam_start is not None: 308 if self.args.mam_start is not None:
278 extra["mam_start"] = float(self.args.mam_start) 309 extra["mam_start"] = float(self.args.mam_start)
279 if self.args.mam_end is not None: 310 if self.args.mam_end is not None:
280 extra["mam_end"] = float(self.args.mam_end) 311 extra["mam_end"] = float(self.args.mam_end)
281 if self.args.mam_with is not None: 312 if self.args.mam_with is not None:
282 extra["mam_with"] = self.args.mam_with 313 extra["mam_with"] = self.args.mam_with
283 for suff in ('max', 'after', 'before', 'index'): 314 for suff in ("max", "after", "before", "index"):
284 key = 'rsm_' + suff 315 key = "rsm_" + suff
285 value = getattr(self.args,key) 316 value = getattr(self.args, key)
286 if value is not None: 317 if value is not None:
287 extra[key] = str(value) 318 extra[key] = str(value)
288 try: 319 try:
289 data, metadata_s, profile = await self.host.bridge.mam_get( 320 data, metadata_s, profile = await self.host.bridge.mam_get(
290 self.args.service, data_format.serialise(extra), self.profile) 321 self.args.service, data_format.serialise(extra), self.profile
322 )
291 except Exception as e: 323 except Exception as e:
292 self.disp(f"can't retrieve MAM archives: {e}", error=True) 324 self.disp(f"can't retrieve MAM archives: {e}", error=True)
293 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 325 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
294 326
295 metadata = data_format.deserialise(metadata_s) 327 metadata = data_format.deserialise(metadata_s)
306 await self.output(data) 338 await self.output(data)
307 339
308 # FIXME: metadata are not displayed correctly and don't play nice with output 340 # FIXME: metadata are not displayed correctly and don't play nice with output
309 # they should be added to output data somehow 341 # they should be added to output data somehow
310 if self.verbosity: 342 if self.verbosity:
311 for value in ("rsm_first", "rsm_last", "rsm_index", "rsm_count", 343 for value in (
312 "mam_complete", "mam_stable"): 344 "rsm_first",
345 "rsm_last",
346 "rsm_index",
347 "rsm_count",
348 "mam_complete",
349 "mam_stable",
350 ):
313 if value in metadata: 351 if value in metadata:
314 label = value.split("_")[1] 352 label = value.split("_")[1]
315 self.disp(A.color( 353 self.disp(A.color(C.A_HEADER, label, ": ", A.RESET, metadata[value]))
316 C.A_HEADER, label, ': ' , A.RESET, metadata[value]))
317 354
318 self.host.quit() 355 self.host.quit()
319 356
320 357
321 class Message(base.CommandBase): 358 class Message(base.CommandBase):