Mercurial > libervia-backend
comparison sat_frontends/jp/cmd_blog.py @ 3308:384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
`data_format.serialise` is now used for `mbGet`, and RSM/MAM values are not transtyped to
strings anymore. A serialised dict is now used, items are put in the `items` key.
Comments handling has been refactored to use a list for the potentially multiple comments
nodes.
`rsm` data are now in a `rsm` key of the dict, and `mam` data are merged with other
metadata.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 16 Jul 2020 09:07:20 +0200 |
parents | 46f0b388eeea |
children | 9dbe2517d131 |
comparison
equal
deleted
inserted
replaced
3307:9f0e28137cd0 | 3308:384283adcce1 |
---|---|
31 from . import base | 31 from . import base |
32 from sat.core.i18n import _ | 32 from sat.core.i18n import _ |
33 from sat_frontends.jp.constants import Const as C | 33 from sat_frontends.jp.constants import Const as C |
34 from sat_frontends.jp import common | 34 from sat_frontends.jp import common |
35 from sat.tools.common.ansi import ANSI as A | 35 from sat.tools.common.ansi import ANSI as A |
36 from sat.tools.common import data_objects | |
37 from sat.tools.common import uri | 36 from sat.tools.common import uri |
38 from sat.tools import config | 37 from sat.tools import config |
39 from configparser import NoSectionError, NoOptionError | 38 from configparser import NoSectionError, NoOptionError |
40 from sat.tools.common import data_format | 39 from sat.tools.common import data_format |
41 | 40 |
260 help=_("microblog data key(s) to display (default: depend of verbosity)"), | 259 help=_("microblog data key(s) to display (default: depend of verbosity)"), |
261 ) | 260 ) |
262 # TODO: add MAM filters | 261 # TODO: add MAM filters |
263 | 262 |
264 def template_data_mapping(self, data): | 263 def template_data_mapping(self, data): |
265 return {"items": data_objects.BlogItems(data, deserialise=False)} | 264 items, blog_items = data |
265 blog_items['items'] = items | |
266 return {"blog_items": blog_items} | |
266 | 267 |
267 def format_comments(self, item, keys): | 268 def format_comments(self, item, keys): |
268 comments_data = data_format.dict2iterdict( | 269 comments_data = data_format.dict2iterdict( |
269 "comments", item, ("node", "service"), pop=True | 270 "comments", item, ("node", "service"), pop=True |
270 ) | 271 ) |
422 | 423 |
423 print(("\n" + sep + "\n")) | 424 print(("\n" + sep + "\n")) |
424 | 425 |
425 async def start(self): | 426 async def start(self): |
426 try: | 427 try: |
427 mb_result = await self.host.bridge.mbGet( | 428 mb_data = data_format.deserialise( |
428 self.args.service, | 429 await self.host.bridge.mbGet( |
429 self.args.node, | 430 self.args.service, |
430 self.args.max, | 431 self.args.node, |
431 self.args.items, | 432 self.args.max, |
432 self.getPubsubExtra(), | 433 self.args.items, |
433 self.profile | 434 self.getPubsubExtra(), |
435 self.profile | |
436 ) | |
434 ) | 437 ) |
435 except Exception as e: | 438 except Exception as e: |
436 self.disp(f"can't get blog items: {e}", error=True) | 439 self.disp(f"can't get blog items: {e}", error=True) |
437 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | 440 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
438 else: | 441 else: |
439 items, metadata = mb_result | 442 items = mb_data.pop('items') |
440 items = [data_format.deserialise(i) for i in items] | 443 await self.output((items, mb_data)) |
441 mb_result = items, metadata | |
442 await self.output(mb_result) | |
443 self.host.quit(C.EXIT_OK) | 444 self.host.quit(C.EXIT_OK) |
444 | 445 |
445 | 446 |
446 class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit): | 447 class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit): |
447 def __init__(self, host): | 448 def __init__(self, host): |
590 return SYNTAX_EXT.get(self.current_syntax, SYNTAX_EXT[""]) | 591 return SYNTAX_EXT.get(self.current_syntax, SYNTAX_EXT[""]) |
591 | 592 |
592 async def getItemData(self, service, node, item): | 593 async def getItemData(self, service, node, item): |
593 items = [item] if item else [] | 594 items = [item] if item else [] |
594 | 595 |
595 mb_data = await self.host.bridge.mbGet( | 596 mb_data = data_format.deserialise( |
596 service, node, 1, items, {}, self.profile) | 597 await self.host.bridge.mbGet( |
597 mb_data = data_format.deserialise(mb_data[0][0]) | 598 service, node, 1, items, {}, self.profile |
599 ) | |
600 ) | |
601 item = mb_data['items'][0] | |
598 | 602 |
599 try: | 603 try: |
600 content = mb_data["content_xhtml"] | 604 content = item["content_xhtml"] |
601 except KeyError: | 605 except KeyError: |
602 content = mb_data["content"] | 606 content = item["content"] |
603 if content: | 607 if content: |
604 content = await self.host.bridge.syntaxConvert( | 608 content = await self.host.bridge.syntaxConvert( |
605 content, "text", SYNTAX_XHTML, False, self.profile | 609 content, "text", SYNTAX_XHTML, False, self.profile |
606 ) | 610 ) |
607 | 611 |
621 else: | 625 else: |
622 parser = etree.XMLParser(remove_blank_text=True) | 626 parser = etree.XMLParser(remove_blank_text=True) |
623 root = etree.fromstring(content, parser) | 627 root = etree.fromstring(content, parser) |
624 content = etree.tostring(root, encoding=str, pretty_print=True) | 628 content = etree.tostring(root, encoding=str, pretty_print=True) |
625 | 629 |
626 return content, mb_data, mb_data["id"] | 630 return content, item, item["id"] |
627 | 631 |
628 async def start(self): | 632 async def start(self): |
629 # if there are user defined extension, we use them | 633 # if there are user defined extension, we use them |
630 SYNTAX_EXT.update(config.getConfig(self.sat_conf, "jp", CONF_SYNTAX_EXT, {})) | 634 SYNTAX_EXT.update(config.getConfig(self.sat_conf, "jp", CONF_SYNTAX_EXT, {})) |
631 self.current_syntax = await self.get_current_syntax() | 635 self.current_syntax = await self.get_current_syntax() |