diff frontends/src/jp/cmd_pubsub.py @ 2532:772447ec070f

jp: pubsub options refactoring: There is now only "use_pubsub", and specification are set using "pubsub_flags" argument when instantiating CommandBase. Options are more Python Zen compliant by using explicit arguments for item, draft, url instead of trying to guess with magic keyword and type detection. Pubsub node and item are now always using respecively "-n" and "-i" even when required, this way shell history can be used to change command more easily, and it's globally less confusing for user. if --pubsub-url is used, elements can be overwritten with individual option (e.g. change item id with --item). New "use_draft" argument in CommandBase, to re-use current draft or open a file path as draft. Item can now be specified when using a draft. If it already exists, its content will be added to current draft (with a separator), to avoid loosing data. common.BaseEdit.getItemPath could be simplified thanks to those changes. Pubsub URI handling has been moved to base.py.
author Goffi <goffi@goffi.org>
date Wed, 21 Mar 2018 19:13:22 +0100
parents 0046283a285d
children dcc77f23e370
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pubsub.py	Wed Mar 21 19:07:06 2018 +0100
+++ b/frontends/src/jp/cmd_pubsub.py	Wed Mar 21 19:13:22 2018 +0100
@@ -46,7 +46,7 @@
 class NodeInfo(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'info', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_(u'retrieve node configuration'))
+        base.CommandBase.__init__(self, host, 'info', use_output=C.OUTPUT_DICT, use_pubsub=True, pubsub_flags={C.NODE}, help=_(u'retrieve node configuration'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -71,7 +71,6 @@
         self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
     def start(self):
-        common.checkURI(self.args)
         self.host.bridge.psNodeConfigurationGet(
             self.args.service,
             self.args.node,
@@ -83,7 +82,7 @@
 class NodeCreate(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'create', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, use_verbose=True, help=_(u'create a node'))
+        base.CommandBase.__init__(self, host, 'create', use_output=C.OUTPUT_DICT, use_pubsub=True, pubsub_flags={C.NODE}, use_verbose=True, help=_(u'create a node'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -123,7 +122,7 @@
 class NodeDelete(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'delete', use_pubsub_node_req=True, help=_(u'delete a node'))
+        base.CommandBase.__init__(self, host, 'delete', use_pubsub=True, pubsub_flags={C.NODE}, help=_(u'delete a node'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -134,7 +133,6 @@
         self.host.quit()
 
     def start(self):
-        common.checkURI(self.args)
         if not self.args.force:
             if not self.args.service:
                 message = _(u"Are you sure to delete pep node [{node_id}] ?").format(
@@ -161,7 +159,7 @@
 class NodeSet(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'set', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, use_verbose=True, help=_(u'set node configuration'))
+        base.CommandBase.__init__(self, host, 'set', use_output=C.OUTPUT_DICT, use_pubsub=True, pubsub_flags={C.NODE}, use_verbose=True, help=_(u'set node configuration'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -184,7 +182,6 @@
             return k
 
     def start(self):
-        common.checkURI(self.args)
         self.host.bridge.psNodeConfigurationSet(
             self.args.service,
             self.args.node,
@@ -197,7 +194,7 @@
 class NodeAffiliationsGet(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_(u'retrieve node affiliations (for node owner)'))
+        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, use_pubsub=True, pubsub_flags={C.NODE}, help=_(u'retrieve node affiliations (for node owner)'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -213,7 +210,6 @@
         self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
     def start(self):
-        common.checkURI(self.args)
         self.host.bridge.psNodeAffiliationsGet(
             self.args.service,
             self.args.node,
@@ -225,7 +221,7 @@
 class NodeAffiliationsSet(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, use_verbose=True, help=_(u'set affiliations (for node owner)'))
+        base.CommandBase.__init__(self, host, 'set', use_pubsub=True, pubsub_flags={C.NODE}, use_verbose=True, help=_(u'set affiliations (for node owner)'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -251,7 +247,6 @@
         self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
     def start(self):
-        common.checkURI(self.args)
         affiliations = dict(self.args.affiliations)
         self.host.bridge.psNodeAffiliationsSet(
             self.args.service,
@@ -272,7 +267,7 @@
 class NodeSubscriptionsGet(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, use_pubsub_node_req=True, help=_(u'retrieve node subscriptions (for node owner)'))
+        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_DICT, use_pubsub=True, pubsub_flags={C.NODE}, help=_(u'retrieve node subscriptions (for node owner)'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -288,7 +283,6 @@
         self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
     def start(self):
-        common.checkURI(self.args)
         self.host.bridge.psNodeSubscriptionsGet(
             self.args.service,
             self.args.node,
@@ -320,7 +314,7 @@
 class NodeSubscriptionsSet(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, use_verbose=True, help=_(u'set/modify subscriptions (for node owner)'))
+        base.CommandBase.__init__(self, host, 'set', use_pubsub=True, pubsub_flags={C.NODE}, use_verbose=True, help=_(u'set/modify subscriptions (for node owner)'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -347,7 +341,6 @@
         self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
     def start(self):
-        common.checkURI(self.args)
         self.host.bridge.psNodeSubscriptionsSet(
             self.args.service,
             self.args.node,
@@ -367,7 +360,7 @@
 class NodeSchemaSet(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, use_verbose=True, help=_(u'set/replace a schema'))
+        base.CommandBase.__init__(self, host, 'set', use_pubsub=True, pubsub_flags={C.NODE}, use_verbose=True, help=_(u'set/replace a schema'))
         self.need_loop = True
 
     def add_parser_options(self):
@@ -393,12 +386,12 @@
     use_items=False
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'edit', use_pubsub_node_req=True, use_verbose=True, help=_(u'edit a schema'))
+        base.CommandBase.__init__(self, host, 'edit', use_pubsub=True, pubsub_flags={C.NODE}, use_draft=True, use_verbose=True, help=_(u'edit a schema'))
         common.BaseEdit.__init__(self, self.host, PUBSUB_SCHEMA_TMP_DIR)
         self.need_loop=True
 
     def add_parser_options(self):
-        common.BaseEdit.add_parser_options(self)
+        pass
 
     def psSchemaSetCb(self):
         self.disp(_(u'schema has been set'), 1)
@@ -431,7 +424,6 @@
         self.runEditor("pubsub_schema_editor_args", content_file_path, content_file_obj)
 
     def start(self):
-        common.checkURI(self.args)
         self.host.bridge.psSchemaGet(
             self.args.service,
             self.args.node,
@@ -445,7 +437,7 @@
 class NodeSchemaGet(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_XML, use_pubsub_node_req=True, use_verbose=True, help=_(u'get schema'))
+        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_XML, use_pubsub=True, pubsub_flags={C.NODE}, use_verbose=True, help=_(u'get schema'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -459,7 +451,6 @@
         self.host.quit()
 
     def start(self):
-        common.checkURI(self.args)
         self.host.bridge.psSchemaGet(
             self.args.service,
             self.args.node,
@@ -487,7 +478,7 @@
 class Set(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'set', use_pubsub_node_req=True, help=_(u'publish a new item or update an existing one'))
+        base.CommandBase.__init__(self, host, 'set', use_pubsub=True, pubsub_flags={C.NODE}, help=_(u'publish a new item or update an existing one'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -506,7 +497,6 @@
         except ImportError:
             self.disp(u"lxml module must be installed to use edit, please install it with \"pip install lxml\"", error=True)
             self.host.quit(1)
-        common.checkURI(self.args)
         try:
             element = etree.parse(sys.stdin).getroot()
         except Exception as e:
@@ -532,12 +522,10 @@
 class Get(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, use_pubsub_node_req=True, help=_(u'get pubsub item(s)'))
+        base.CommandBase.__init__(self, host, 'get', use_output=C.OUTPUT_LIST_XML, use_pubsub=True, pubsub_flags={C.NODE, C.MULTI_ITEMS}, help=_(u'get pubsub item(s)'))
         self.need_loop=True
 
     def add_parser_options(self):
-        self.parser.add_argument("-i", "--item", type=base.unicode_decoder, action='append', default=[], dest='items',
-                                 help=_(u"item(s) id(s) to get (default: request all items)"))
         self.parser.add_argument("-S", "--sub-id", type=base.unicode_decoder, default=u'',
                                  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)))
@@ -555,7 +543,6 @@
         self.host.quit(C.EXIT_BRIDGE_ERRBACK)
 
     def start(self):
-        common.checkURI(self.args)
         self.host.bridge.psItemsGet(
             self.args.service,
             self.args.node,
@@ -570,20 +557,18 @@
 class Delete(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'delete', use_pubsub_node_req=True, help=_(u'delete an item'))
+        base.CommandBase.__init__(self, host, 'delete', use_pubsub=True, pubsub_flags={C.NODE, C.SINGLE_ITEM}, help=_(u'delete an item'))
         self.need_loop=True
 
     def add_parser_options(self):
-        self.parser.add_argument("item", nargs='?', type=base.unicode_decoder, help=_(u"item to delete"))
         self.parser.add_argument("-f", "--force", action='store_true', help=_(u"delete without confirmation"))
-        self.parser.add_argument("-n", "--notify", action='store_true', help=_(u"notify deletion"))
+        self.parser.add_argument("-N", "--notify", action='store_true', help=_(u"notify deletion"))
 
     def psItemsDeleteCb(self):
         self.disp(_(u'item {item_id} has been deleted').format(item_id=self.args.item))
         self.host.quit(C.EXIT_OK)
 
     def start(self):
-        common.checkURI(self.args)
         if not self.args.item:
             self.parser.error(_(u"You need to specify an item to delete"))
         if not self.args.force:
@@ -607,12 +592,12 @@
 class Edit(base.CommandBase, common.BaseEdit):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'edit', use_verbose=True, use_pubsub_node_req=True, help=_(u'edit an existing or new pubsub item'))
+        base.CommandBase.__init__(self, host, 'edit', use_verbose=True, use_pubsub=True,
+                                  pubsub_flags={C.NODE, C.SINGLE_ITEM}, use_draft=True, help=_(u'edit an existing or new pubsub item'))
         common.BaseEdit.__init__(self, self.host, PUBSUB_TMP_DIR)
 
     def add_parser_options(self):
-        self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword"))
-        common.BaseEdit.add_parser_options(self)
+        pass
 
     def edit(self, content_file_path, content_file_obj):
         # we launch editor
@@ -644,15 +629,14 @@
         return etree.tostring(payload, encoding="unicode", pretty_print=True), item_id
 
     def start(self):
-        self.pubsub_service, self.pubsub_node, self.pubsub_item, content_file_path, content_file_obj = self.getItemPath(self.args.item)
-
+        self.pubsub_service, self.pubsub_node, self.pubsub_item, content_file_path, content_file_obj = self.getItemPath()
         self.edit(content_file_path, content_file_obj)
 
 
 class Subscribe(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'subscribe', use_pubsub_node_req=True, use_verbose=True, help=_(u'subscribe to a node'))
+        base.CommandBase.__init__(self, host, 'subscribe', use_pubsub=True, pubsub_flags={C.NODE}, use_verbose=True, help=_(u'subscribe to a node'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -680,7 +664,7 @@
     # TODO: voir pourquoi NodeNotFound sur subscribe juste après unsubscribe
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'unsubscribe', use_pubsub_node_req=True, use_verbose=True, help=_(u'unsubscribe from a node'))
+        base.CommandBase.__init__(self, host, 'unsubscribe', use_pubsub=True, pubsub_flags={C.NODE}, use_verbose=True, help=_(u'unsubscribe from a node'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -758,7 +742,8 @@
     EXEC_ACTIONS = (u'exec', u'external')
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'search', use_output=C.OUTPUT_XML, use_pubsub=True, use_verbose=True, help=_(u'search items corresponding to filters'))
+        base.CommandBase.__init__(self, host, 'search', use_output=C.OUTPUT_XML, use_pubsub=True, pubsub_flags={C.MULTI_ITEMS},
+                                  use_verbose=True, help=_(u'search items corresponding to filters'))
         self.need_loop=True
 
     @property
@@ -778,7 +763,6 @@
         return (type_, value)
 
     def add_parser_options(self):
-        self.parser.add_argument("-i", "--item", action="append", default=[], dest='items', type=base.unicode_decoder, help=_(u"item id(s)"))
         self.parser.add_argument("-D", "--max-depth", type=int, default=0, help=_(u"maximum depth of recursion (will search linked nodes if > 0, default: 0)"))
         self.parser.add_argument("-m", "--max", type=int, default=30, help=_(u"maximum number of items to get per node ({} to get all items, default: 30)".format(C.NO_LIMIT)))
         self.parser.add_argument("-N", "--namespace", action='append', nargs=2, default=[],
@@ -1082,18 +1066,16 @@
         if self.args.filters is None:
             self.args.filters = []
         self.args.namespace = dict(self.args.namespace + [('pubsub', "http://jabber.org/protocol/pubsub")])
-        common.checkURI(self.args)
         self.getItems(0, self.args.service, self.args.node, self.args.items)
 
 
 class Uri(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'uri', use_profile=False, use_pubsub_node_req=True, help=_(u'build URI'))
+        base.CommandBase.__init__(self, host, 'uri', use_profile=False, use_pubsub=True, pubsub_flags={C.NODE, C.SINGLE_ITEM}, help=_(u'build URI'))
         self.need_loop=True
 
     def 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_):
@@ -1127,7 +1109,7 @@
 class HookCreate(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'create', use_pubsub_node_req=True, help=_(u'create a Pubsub hook'))
+        base.CommandBase.__init__(self, host, 'create', use_pubsub=True, pubsub_flags={C.NODE}, help=_(u'create a Pubsub hook'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -1143,7 +1125,6 @@
                 self.parser.error(_(u"{path} is not a file").format(path=self.args.hook_arg))
 
     def start(self):
-        common.checkURI(self.args)
         self.checkArgs(self)
         self.host.bridge.psHookAdd(
             self.args.service,
@@ -1161,7 +1142,7 @@
 class HookDelete(base.CommandBase):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'delete', use_pubsub_node_req=True, help=_(u'delete a Pubsub hook'))
+        base.CommandBase.__init__(self, host, 'delete', use_pubsub=True, pubsub_flags={C.NODE}, help=_(u'delete a Pubsub hook'))
         self.need_loop=True
 
     def add_parser_options(self):
@@ -1174,7 +1155,6 @@
         self.host.quit()
 
     def start(self):
-        common.checkURI(self.args)
         HookCreate.checkArgs(self)
         self.host.bridge.psHookRemove(
             self.args.service,