diff 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
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_pubsub.py	Fri Dec 27 13:30:20 2019 +0100
+++ b/sat_frontends/jp/cmd_pubsub.py	Sat Dec 28 20:02:18 2019 +0100
@@ -32,6 +32,7 @@
 from sat_frontends.jp import arg_tools
 from sat_frontends.jp import xml_tools
 from functools import partial
+from sat.tools.common import data_format
 from sat.tools.common import uri
 from sat.tools.common.ansi import ANSI as A
 from sat_frontends.tools import jid, strings
@@ -104,8 +105,9 @@
             help=_("create a node"),
         )
 
-    def add_parser_options(self):
-        self.parser.add_argument(
+    @staticmethod
+    def add_node_config_options(parser):
+        parser.add_argument(
             "-f",
             "--field",
             action="append",
@@ -115,18 +117,25 @@
             metavar=("KEY", "VALUE"),
             help=_("configuration field to set"),
         )
-        self.parser.add_argument(
+        parser.add_argument(
             "-F",
             "--full-prefix",
             action="store_true",
             help=_('don\'t prepend "pubsub#" prefix to field names'),
         )
 
+    def add_parser_options(self):
+        self.add_node_config_options(self.parser)
+
+    @staticmethod
+    def get_config_options(args):
+        if not args.full_prefix:
+            return {"pubsub#" + k: v for k, v in args.fields}
+        else:
+            return dict(args.fields)
+
     async def start(self):
-        if not self.args.full_prefix:
-            options = {"pubsub#" + k: v for k, v in self.args.fields}
-        else:
-            options = dict(self.args.fields)
+        options = self.get_config_options(self.args)
         try:
             node_id = await self.host.bridge.psNodeCreate(
                 self.args.service,
@@ -730,6 +739,7 @@
         )
 
     def add_parser_options(self):
+        NodeCreate.add_node_config_options(self.parser)
         self.parser.add_argument(
             "item",
             nargs="?",
@@ -741,6 +751,10 @@
         element, etree = xml_tools.etreeParse(self, sys.stdin)
         element = xml_tools.getPayload(self, element)
         payload = etree.tostring(element, encoding="unicode")
+        extra = {}
+        publish_options = NodeCreate.get_config_options(self.args)
+        if publish_options:
+            extra['publish_options'] = publish_options
 
         try:
             published_id = await self.host.bridge.psItemSend(
@@ -748,7 +762,7 @@
                 self.args.node,
                 payload,
                 self.args.item,
-                {},
+                data_format.serialise(extra),
                 self.profile,
             )
         except Exception as e:
@@ -869,7 +883,7 @@
             self.pubsub_node,
             content,
             self.pubsub_item or "",
-            {},
+            "",
             self.profile,
         )
         if published_id: