changeset 2224:87fcd4a7c7e4

jp (pubsub): added uri command to build pubsub URI
author Goffi <goffi@goffi.org>
date Sun, 16 Apr 2017 17:59:54 +0200
parents c6c9a97ffebf
children 301bb52c8715
files frontends/src/jp/cmd_pubsub.py
diffstat 1 files changed, 47 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pubsub.py	Sun Apr 16 17:57:40 2017 +0200
+++ b/frontends/src/jp/cmd_pubsub.py	Sun Apr 16 17:59:54 2017 +0200
@@ -22,6 +22,8 @@
 from sat.core.i18n import _
 from sat_frontends.jp.constants import Const as C
 from functools import partial
+from sat.tools.common import uri
+from sat_frontends.tools import jid
 
 __commands__ = ["Pubsub"]
 
@@ -87,7 +89,7 @@
 
     def add_parser_options(self):
         self.parser.add_argument("-f", "--field", type=base.unicode_decoder, action='append', nargs=2, dest='fields',
-                                 required=True, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)"))
+                                 default={}, metavar=(u"KEY", u"VALUE"), help=_(u"configuration field to set (required)"))
         self.parser.add_argument("-F", "--full-prefix", action="store_true", help=_(u"don't prepend \"pubsub#\" prefix to field names"))
         NodeCommon.add_parser_options(self)
 
@@ -275,7 +277,7 @@
         super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))
 
 
-class Get(base.CommandBase):
+class Get(base.CommandBase, NodeCommon):
 
     def __init__(self, host):
         base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, help=_(u'get pubsub item(s)'))
@@ -288,9 +290,6 @@
                                  help=_(u"subscription id"))
         self.parser.add_argument("-m", "--max", type=int, default=10, help=_(u"maximum number of items to get ({} to get all items)".format(C.NO_LIMIT)))
         # TODO: a key(s) argument to select keys to display
-        self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
-                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
-        self.parser.add_argument("node", type=base.unicode_decoder, help=_(u"node to request"))
         # TODO: add MAM filters
 
 
@@ -316,16 +315,15 @@
             errback=self.psItemGetEb)
 
 
-class Affiliations(base.CommandBase):
+class Affiliations(base.CommandBase, NodeCommon):
 
     def __init__(self, host):
+        NodeCommon.__init__(self, node_required=False)
         base.CommandBase.__init__(self, host, 'affiliations', use_output=C.OUTPUT_DICT, help=_(u'retrieve all affiliations on a service'))
         self.need_loop=True
 
     def add_parser_options(self):
-        self.parser.add_argument("-n", "--node", type=base.unicode_decoder, default=u'', help=_(u"node to request"))
-        self.parser.add_argument("-s", "--service", type=base.unicode_decoder, default=u'',
-                                 help=_(u"JID of the PubSub service (default: request profile own pubsub)"))
+        NodeCommon.add_parser_options(self)
 
     def psAffiliationsGetCb(self, affiliations):
         self.output(affiliations)
@@ -345,8 +343,47 @@
             errback=self.psAffiliationsGetEb)
 
 
+class Uri(base.CommandBase, NodeCommon):
+
+    def __init__(self, host):
+        base.CommandBase.__init__(self, host, 'uri', use_profile=False, help=_(u'build URI'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        NodeCommon.add_parser_options(self)
+        self.parser.add_argument("-i", "--item", type=base.unicode_decoder, help=_(u"item to link"))
+        self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default=C.PROF_KEY_DEFAULT, help=_(u"profile (used when no server is specified)"))
+
+    def display_uri(self, jid_):
+        uri_args = {u'type': 'pubsub'}
+        if not self.args.service:
+            self.args.service = jid.JID(jid_).bare
+
+        for key in ('node', 'service', 'item'):
+            value = getattr(self.args, key)
+            if key == 'service':
+                key = 'path'
+            if value:
+                uri_args[key] = value
+        self.disp(uri.buildXMPPUri(**uri_args))
+        self.host.quit()
+
+    def start(self):
+        if not self.args.service:
+            self.host.bridge.asyncGetParamA(
+                u'JabberID',
+                u'Connection',
+                profile_key=self.args.profile,
+                callback=self.display_uri,
+                errback=partial(self.errback,
+                                msg=_(u"can't retrieve jid: {}"),
+                                exit_code=C.EXIT_BRIDGE_ERRBACK))
+        else:
+            self.display_uri(None)
+
+
 class Pubsub(base.CommandBase):
-    subcommands = (Get, Node, Affiliations)
+    subcommands = (Get, Node, Affiliations, Uri)
 
     def __init__(self, host):
         super(Pubsub, self).__init__(host, 'pubsub', use_profile=False, help=_('PubSub nodes/items management'))