diff frontends/src/jp/cmd_pubsub.py @ 2351:3c0a3fae1862

jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
author Goffi <goffi@goffi.org>
date Wed, 06 Sep 2017 07:39:10 +0200
parents d94e932be8b3
children ebc0dfe9c0ca
line wrap: on
line diff
--- a/frontends/src/jp/cmd_pubsub.py	Wed Sep 06 07:38:39 2017 +0200
+++ b/frontends/src/jp/cmd_pubsub.py	Wed Sep 06 07:39:10 2017 +0200
@@ -37,6 +37,7 @@
 __commands__ = ["Pubsub"]
 
 PUBSUB_TMP_DIR = u"pubsub"
+PUBSUB_SCHEMA_TMP_DIR = PUBSUB_TMP_DIR + "_schema"
 ALLOWED_SUBSCRIPTIONS_OWNER = ('subscribed', 'pending', 'none')
 
 # TODO: need to split this class in several modules, plugin should handle subcommands
@@ -362,8 +363,119 @@
         super(NodeSubscriptions, self).__init__(host, 'subscriptions', use_profile=False, help=_(u'get or modify node subscriptions'))
 
 
+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'))
+        self.need_loop = True
+
+    def add_parser_options(self):
+        self.parser.add_argument('schema', help=_(u"schema to set (must be XML)"))
+
+    def psSchemaSetCb(self):
+        self.disp(_(u'schema has been set'), 1)
+        self.host.quit()
+
+    def start(self):
+        self.host.bridge.psSchemaSet(
+            self.args.service,
+            self.args.node,
+            self.args.schema,
+            self.profile,
+            callback=self.psSchemaSetCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't set schema: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
+class NodeSchemaEdit(base.CommandBase, common.BaseEdit):
+    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'))
+        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)
+
+    def psSchemaSetCb(self):
+        self.disp(_(u'schema has been set'), 1)
+        self.host.quit()
+
+    def publish(self, schema):
+        self.host.bridge.psSchemaSet(
+            self.args.service,
+            self.args.node,
+            schema,
+            self.profile,
+            callback=self.psSchemaSetCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't set schema: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+    def psSchemaGetCb(self, schema):
+        try:
+            from lxml import etree
+        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)
+        parser = etree.XMLParser(remove_blank_text=True)
+        schema_elt = etree.fromstring(schema, parser)
+        content_file_obj, content_file_path = self.getTmpFile()
+        content_file_obj.write(etree.tostring(schema_elt, encoding="utf-8", pretty_print=True))
+        content_file_obj.seek(0)
+        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,
+            self.profile,
+            callback=self.psSchemaGetCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't edit schema: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
+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'))
+        self.need_loop=True
+
+    def add_parser_options(self):
+        pass
+
+    def psSchemaGetCb(self, schema):
+        if not schema:
+            self.disp(_(u'no schema found'), 1)
+            self.host.quit(1)
+        self.output(schema)
+        self.host.quit()
+
+    def start(self):
+        common.checkURI(self.args)
+        self.host.bridge.psSchemaGet(
+            self.args.service,
+            self.args.node,
+            self.profile,
+            callback=self.psSchemaGetCb,
+            errback=partial(self.errback,
+                            msg=_(u"can't get schema: {}"),
+                            exit_code=C.EXIT_BRIDGE_ERRBACK))
+
+
+class NodeSchema(base.CommandBase):
+    subcommands = (NodeSchemaSet, NodeSchemaEdit, NodeSchemaGet)
+
+    def __init__(self, host):
+        super(NodeSchema, self).__init__(host, 'schema', use_profile=False, help=_(u"data schema manipulation"))
+
+
 class Node(base.CommandBase):
-    subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations, NodeSubscriptions)
+    subcommands = (NodeInfo, NodeCreate, NodeDelete, NodeSet, NodeAffiliations, NodeSubscriptions, NodeSchema)
 
     def __init__(self, host):
         super(Node, self).__init__(host, 'node', use_profile=False, help=_('node handling'))
@@ -447,7 +559,7 @@
 class Edit(base.CommandBase, common.BaseEdit):
 
     def __init__(self, host):
-        base.CommandBase.__init__(self, host, 'edit', use_verbose=True, use_pubsub=True, help=_(u'edit an existing or new pubsub item'))
+        base.CommandBase.__init__(self, host, 'edit', use_verbose=True, use_pubsub_node_req=True, help=_(u'edit an existing or new pubsub item'))
         common.BaseEdit.__init__(self, self.host, PUBSUB_TMP_DIR)
 
     def add_parser_options(self):
@@ -980,7 +1092,7 @@
     subcommands = (HookCreate, HookDelete, HookList)
 
     def __init__(self, host):
-        super(Hook, self).__init__(host, 'hook', use_profile=False, help=_('trigger action on Pubsub notifications'))
+        super(Hook, self).__init__(host, 'hook', use_profile=False, use_verbose=True, help=_('trigger action on Pubsub notifications'))
 
 
 class Pubsub(base.CommandBase):