diff src/plugins/plugin_import.py @ 2370:2c2b826b0bb3

plugin import: node can now be specified + added a "session" dict to keep import session data: import session data are data that can be used by importer to store anything which can be useful to keep between import methods.
author Goffi <goffi@goffi.org>
date Fri, 06 Oct 2017 08:52:51 +0200
parents cdaa58e14553
children f57a8eaec8ed
line wrap: on
line diff
--- a/src/plugins/plugin_import.py	Sun Oct 01 12:21:23 2017 +0200
+++ b/src/plugins/plugin_import.py	Fri Oct 06 08:52:51 2017 +0200
@@ -65,14 +65,14 @@
         import_handler.register = partial(self.register, import_handler)
         import_handler.unregister = partial(self.unregister, import_handler)
         import_handler.importers = {}
-        def _import(name, location, options, pubsub_service, profile):
-            return self._doImport(import_handler, name, location, options, pubsub_service, profile)
+        def _import(name, location, options, pubsub_service, pubsub_node, profile):
+            return self._doImport(import_handler, name, location, options, pubsub_service, pubsub_node, profile)
         def _importList():
             return self.listImporters(import_handler)
         def _importDesc(name):
             return self.getDescription(import_handler, name)
 
-        self.host.bridge.addMethod(name + "Import", ".plugin", in_sign='ssa{ss}ss', out_sign='s', method=_import, async=True)
+        self.host.bridge.addMethod(name + "Import", ".plugin", in_sign='ssa{ss}sss', out_sign='s', method=_import, async=True)
         self.host.bridge.addMethod(name + "ImportList", ".plugin", in_sign='', out_sign='a(ss)', method=_importList)
         self.host.bridge.addMethod(name + "ImportDesc", ".plugin", in_sign='s', out_sign='(ss)', method=_importDesc)
 
@@ -100,7 +100,7 @@
         else:
             return importer.short_desc, importer.long_desc
 
-    def _doImport(self, import_handler, name, location, options, pubsub_service='', profile=C.PROF_KEY_NONE):
+    def _doImport(self, import_handler, name, location, options, pubsub_service='', pubsub_node='', profile=C.PROF_KEY_NONE):
         client = self.host.getClient(profile)
         options = {key: unicode(value) for key, value in options.iteritems()}
         for option in import_handler.BOOL_OPTIONS:
@@ -108,10 +108,10 @@
                 options[option] = C.bool(options[option])
             except KeyError:
                 pass
-        return self.doImport(client, import_handler, unicode(name), unicode(location), options)
+        return self.doImport(client, import_handler, unicode(name), unicode(location), options, pubsub_service or None, pubsub_node or None)
 
     @defer.inlineCallbacks
-    def doImport(self, client, import_handler, name, location, options=None, pubsub_service=None):
+    def doImport(self, client, import_handler, name, location, options=None, pubsub_service=None, pubsub_node=None):
         """Import data
 
         @param import_handler(object): instance of the import handler
@@ -122,6 +122,8 @@
         @param options(dict, None): extra options.
         @param pubsub_service(jid.JID, None): jid of the PubSub service where data must be imported
             None to use profile's server
+        @param pubsub_node(unicode, None): PubSub node to use
+            None to use importer's default node
         @return (unicode): progress id
         """
         if options is None:
@@ -157,17 +159,19 @@
                    }
         self.host.registerProgressCb(progress_id, partial(self.getProgress, import_handler), metadata, profile=client.profile)
         self.host.bridge.progressStarted(progress_id, metadata, client.profile)
-        url_redirect = {}
-        self.recursiveImport(client, import_handler, items_import_data, progress_id, options, url_redirect)
+        session = {}  # session data, can be use by importers
+        self.recursiveImport(client, import_handler, items_import_data, progress_id, session, options, None, pubsub_service, pubsub_node)
         defer.returnValue(progress_id)
 
     @defer.inlineCallbacks
-    def recursiveImport(self, client, import_handler, items_import_data, progress_id, options, return_data=None, service=None, node=None, depth=0):
+    def recursiveImport(self, client, import_handler, items_import_data, progress_id, session, options, return_data=None, service=None, node=None, depth=0):
         """Do the import recursively
 
         @param import_handler(object): instance of the import handler
         @param items_import_data(iterable): iterable of data as specified in [register]
         @param progress_id(unicode): id of progression
+        @param session(dict): data for this import session
+            can be used by importer so store any useful data
         @param options(dict): import options
         @param return_data(dict): data to return on progressFinished
         @param service(jid.JID, None): PubSub service to use
@@ -177,15 +181,16 @@
         if return_data is None:
             return_data = {}
         for idx, item_import_data in enumerate(items_import_data):
-            item_data = yield import_handler.importItem(client, item_import_data, options, return_data, service, node)
-            yield import_handler.itemFilters(client, item_data, options)
-            recurse_kwargs = yield import_handler.importSubItems(client, item_import_data, item_data, options)
-            yield import_handler.publishItem(client, item_data, service, node)
+            item_data = yield import_handler.importItem(client, item_import_data, session, options, return_data, service, node)
+            yield import_handler.itemFilters(client, item_data, session, options)
+            recurse_kwargs = yield import_handler.importSubItems(client, item_import_data, item_data, session, options)
+            yield import_handler.publishItem(client, item_data, service, node, session)
 
             if recurse_kwargs is not None:
                 recurse_kwargs['client'] = client
                 recurse_kwargs['import_handler'] = import_handler
                 recurse_kwargs['progress_id'] = progress_id
+                recurse_kwargs['session'] = session
                 recurse_kwargs.setdefault('options', options)
                 recurse_kwargs['return_data'] = return_data
                 recurse_kwargs['depth'] = depth + 1