Updated "factory_config" decorator to be able to register factories by name
authorThierry Florac <thierry.florac@onf.fr>
Fri, 25 May 2018 08:39:32 +0200
changeset 182 71a6c885b336
parent 181 17f98772891e
child 183 5e239a213d1d
Updated "factory_config" decorator to be able to register factories by name
src/pyams_utils/factory.py
--- a/src/pyams_utils/factory.py	Fri May 25 08:08:14 2018 +0200
+++ b/src/pyams_utils/factory.py	Fri May 25 08:39:32 2018 +0200
@@ -47,16 +47,18 @@
     return iface.__module__ + '.' + iface.__name__
 
 
-def register_factory(interface, klass, registry=None):
+def register_factory(interface, klass, registry=None, name=''):
     """Register factory for a given interface"""
 
     class Temp(ObjectFactoryAdapter):
         factory = klass
 
-    name = get_interface_name(interface)
+    if_name = get_interface_name(interface)
+    if name:
+        if_name = '{0}::{1}'.format(if_name, name)
     if registry is None:
         registry = get_global_registry()
-    registry.registerAdapter(Temp, name=name)
+    registry.registerAdapter(Temp, name=if_name)
 
 
 class factory_config(object):
@@ -72,16 +74,22 @@
         depth = settings.pop('_depth', 0)
 
         def callback(context, name, ob):
-            provides = settings.get('provided')
-            if not provides:
+            name = settings.get('name', '')
+            provided = settings.get('provided')
+            if not provided:
                 raise TypeError("No provided interface(s) was given for registered factory %r" % ob)
-            if not isinstance(provides, tuple):
-                provides = (provides,)
+            if not isinstance(provided, tuple):
+                provided = (provided,)
 
             config = context.config.with_package(info.module)
-            for provided in provides:
-                logger.debug("Registering factory {0} for interface {1}".format(str(ob), str(provided)))
-                register_factory(provided, ob, config.registry)
+            for interface in provided:
+                if name:
+                    logger.debug("Registering factory {0} for interface {1} with name {2}".format(str(ob),
+                                                                                                  str(interface),
+                                                                                                  name))
+                else:
+                    logger.debug("Registering default factory {0} for interface {1}".format(str(ob), str(interface)))
+                register_factory(interface, ob, config.registry, name)
 
         info = self.venusian.attach(wrapped, callback, category='pyams_factory', depth=depth + 1)
         if info.scope == 'class':
@@ -95,9 +103,11 @@
         return wrapped
 
 
-def get_object_factory(interface, registry=None):
+def get_object_factory(interface, registry=None, name=''):
     """Get registered factory for given interface"""
-    name = get_interface_name(interface)
+    if_name = get_interface_name(interface)
+    if name:
+        if_name = '{0}::{1}'.format(if_name, name)
     if registry is None:
         registry = get_global_registry()
-    return registry.queryAdapter(interface, IObjectFactory, name=name)
+    return registry.queryAdapter(interface, IObjectFactory, name=if_name)