comparison sat_frontends/jp/cmd_pubsub.py @ 3100:cea52c9ddfd9

plugin XEP-0060, jp (pubsub/set): publish-options implementation: - publishing options (XEP-0060 ยง7.1.5) implementation. - jp pubsub/set can specify publish-options using `-f` and `-F` - doc has been updated to explain that - psItemSend and psItemsSend now use serialisation for "extra" - publish-options can be specified in extra['publish-options'] with those methods
author Goffi <goffi@goffi.org>
date Sat, 28 Dec 2019 20:02:18 +0100
parents fee60f17ebac
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3099:0b6d56a8f7e3 3100:cea52c9ddfd9
30 from sat_frontends.jp.constants import Const as C 30 from sat_frontends.jp.constants import Const as C
31 from sat_frontends.jp import common 31 from sat_frontends.jp import common
32 from sat_frontends.jp import arg_tools 32 from sat_frontends.jp import arg_tools
33 from sat_frontends.jp import xml_tools 33 from sat_frontends.jp import xml_tools
34 from functools import partial 34 from functools import partial
35 from sat.tools.common import data_format
35 from sat.tools.common import uri 36 from sat.tools.common import uri
36 from sat.tools.common.ansi import ANSI as A 37 from sat.tools.common.ansi import ANSI as A
37 from sat_frontends.tools import jid, strings 38 from sat_frontends.tools import jid, strings
38 39
39 __commands__ = ["Pubsub"] 40 __commands__ = ["Pubsub"]
102 pubsub_flags={C.NODE}, 103 pubsub_flags={C.NODE},
103 use_verbose=True, 104 use_verbose=True,
104 help=_("create a node"), 105 help=_("create a node"),
105 ) 106 )
106 107
107 def add_parser_options(self): 108 @staticmethod
108 self.parser.add_argument( 109 def add_node_config_options(parser):
110 parser.add_argument(
109 "-f", 111 "-f",
110 "--field", 112 "--field",
111 action="append", 113 action="append",
112 nargs=2, 114 nargs=2,
113 dest="fields", 115 dest="fields",
114 default=[], 116 default=[],
115 metavar=("KEY", "VALUE"), 117 metavar=("KEY", "VALUE"),
116 help=_("configuration field to set"), 118 help=_("configuration field to set"),
117 ) 119 )
118 self.parser.add_argument( 120 parser.add_argument(
119 "-F", 121 "-F",
120 "--full-prefix", 122 "--full-prefix",
121 action="store_true", 123 action="store_true",
122 help=_('don\'t prepend "pubsub#" prefix to field names'), 124 help=_('don\'t prepend "pubsub#" prefix to field names'),
123 ) 125 )
124 126
125 async def start(self): 127 def add_parser_options(self):
126 if not self.args.full_prefix: 128 self.add_node_config_options(self.parser)
127 options = {"pubsub#" + k: v for k, v in self.args.fields} 129
128 else: 130 @staticmethod
129 options = dict(self.args.fields) 131 def get_config_options(args):
132 if not args.full_prefix:
133 return {"pubsub#" + k: v for k, v in args.fields}
134 else:
135 return dict(args.fields)
136
137 async def start(self):
138 options = self.get_config_options(self.args)
130 try: 139 try:
131 node_id = await self.host.bridge.psNodeCreate( 140 node_id = await self.host.bridge.psNodeCreate(
132 self.args.service, 141 self.args.service,
133 self.args.node, 142 self.args.node,
134 options, 143 options,
728 pubsub_flags={C.NODE}, 737 pubsub_flags={C.NODE},
729 help=_("publish a new item or update an existing one"), 738 help=_("publish a new item or update an existing one"),
730 ) 739 )
731 740
732 def add_parser_options(self): 741 def add_parser_options(self):
742 NodeCreate.add_node_config_options(self.parser)
733 self.parser.add_argument( 743 self.parser.add_argument(
734 "item", 744 "item",
735 nargs="?", 745 nargs="?",
736 default="", 746 default="",
737 help=_("id, URL of the item to update, keyword, or nothing for new item"), 747 help=_("id, URL of the item to update, keyword, or nothing for new item"),
739 749
740 async def start(self): 750 async def start(self):
741 element, etree = xml_tools.etreeParse(self, sys.stdin) 751 element, etree = xml_tools.etreeParse(self, sys.stdin)
742 element = xml_tools.getPayload(self, element) 752 element = xml_tools.getPayload(self, element)
743 payload = etree.tostring(element, encoding="unicode") 753 payload = etree.tostring(element, encoding="unicode")
754 extra = {}
755 publish_options = NodeCreate.get_config_options(self.args)
756 if publish_options:
757 extra['publish_options'] = publish_options
744 758
745 try: 759 try:
746 published_id = await self.host.bridge.psItemSend( 760 published_id = await self.host.bridge.psItemSend(
747 self.args.service, 761 self.args.service,
748 self.args.node, 762 self.args.node,
749 payload, 763 payload,
750 self.args.item, 764 self.args.item,
751 {}, 765 data_format.serialise(extra),
752 self.profile, 766 self.profile,
753 ) 767 )
754 except Exception as e: 768 except Exception as e:
755 self.disp(_(f"can't send item: {e}"), error=True) 769 self.disp(_(f"can't send item: {e}"), error=True)
756 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 770 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
867 published_id = await self.host.bridge.psItemSend( 881 published_id = await self.host.bridge.psItemSend(
868 self.pubsub_service, 882 self.pubsub_service,
869 self.pubsub_node, 883 self.pubsub_node,
870 content, 884 content,
871 self.pubsub_item or "", 885 self.pubsub_item or "",
872 {}, 886 "",
873 self.profile, 887 self.profile,
874 ) 888 )
875 if published_id: 889 if published_id:
876 self.disp("Item published at {pub_id}".format(pub_id=published_id)) 890 self.disp("Item published at {pub_id}".format(pub_id=published_id))
877 else: 891 else: