[Zope3-checkins] CVS: Zope3/src/zope/component - factory.py:1.9
Stephan Richter
srichter at cosmos.phy.tufts.edu
Tue Mar 9 07:39:54 EST 2004
Update of /cvs-repository/Zope3/src/zope/component
In directory cvs.zope.org:/tmp/cvs-serv10483/src/zope/component
Modified Files:
factory.py
Log Message:
Added a simple factory implementation that can handle all sorts of
objects. This way any callable can be rendered into a factory easily.
=== Zope3/src/zope/component/factory.py 1.8 => 1.9 ===
--- Zope3/src/zope/component/factory.py:1.8 Fri Mar 5 17:09:25 2004
+++ Zope3/src/zope/component/factory.py Tue Mar 9 07:39:54 2004
@@ -11,100 +11,31 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
-"""Global Factory Service
+"""Factory object
$Id$
"""
-from zope.interface.verify import verifyObject
-from zope.interface import implements
+from zope.interface import implements, implementedBy
from zope.component.interfaces import IFactory
-from zope.component.interfaces import IFactoryInfo
-from zope.component.interfaces import IFactoryService
-from zope.component.exceptions import ComponentLookupError
-class IGlobalFactoryService(IFactoryService):
+class Factory(object):
+ """Generic factory implementation.
- def provideFactory(name, factory, info=None):
- """Provide a factory for the given name.
+ The purpose of this implementation is to provide a quick way of creating
+ factories for classes, functions and other objects.
+ """
+ implements(IFactory)
- If specified, info must implement IFactoryInfo.
- """
-
-class FactoryInfo:
-
- implements(IFactoryInfo)
-
- def __init__(self, title, description):
+ def __init__(self, callable, title='', description=''):
+ self._callable = callable
self.title = title
self.description = description
-class GlobalFactoryService:
-
- implements(IGlobalFactoryService)
-
- def __init__(self):
- self.__factories = {}
- self.__info = {}
-
- def provideFactory(self, name, factory, info=None):
- """See IGlobalFactoryService interface"""
- # XXX At this point the verify object code does not support variable
- # arguments well. For example, I was not able to register any factory
- # that requires constructor arguments! (SR)
- # verifyObject(IFactory, factory)
- assert IFactory.providedBy(factory)
- self.__factories[name] = factory
- if info is not None:
- self.__info[name] = info
-
- def createObject(self, name, *args, **kwargs):
- """See IFactoryService interface"""
- try:
- return self.__factories[name](*args, **kwargs)
- except KeyError:
- raise ComponentLookupError(name)
+ def __call__(self, *args, **kw):
+ return self._callable(*args, **kw)
- def getFactory(self, name):
- """See IFactoryService interface"""
+ def getInterfaces(self):
try:
- return self.__factories[name]
- except KeyError:
- raise ComponentLookupError(name)
-
- def queryFactory(self, name, default=None):
- """See IFactoryService interface"""
- return self.__factories.get(name, default)
-
- def getInterfaces(self, name):
- """See IFactoryService interface"""
- try: return self.__factories[name].getInterfaces()
- except KeyError:
- raise ComponentLookupError(name)
-
- def getFactoriesFor(self, iface):
- """See IFactoryService interface"""
- factories = self.queryFactoriesFor(iface, None)
- if factories is None:
- raise ComponentLookupError(iface)
- return factories
-
- def queryFactoriesFor(self, iface, default=None):
- """See IFactoryService interface"""
- return [(n, f) for n, f in self.__factories.items() \
- if iface in tuple(f.getInterfaces())] or default
-
- def getFactoryInfo(self, name):
- return self.__info.get(name)
-
- _clear = __init__
-
-# the global factory service instance (see component.zcml )
-factoryService = GlobalFactoryService()
-provideFactory = factoryService.provideFactory
-
-_clear = factoryService._clear
-
-# Register our cleanup with Testing.CleanUp to make writing unit tests simpler.
-from zope.testing.cleanup import addCleanUp
-addCleanUp(_clear)
-del addCleanUp
+ return list(implementedBy(self._callable))
+ except AttributeError:
+ return []
More information about the Zope3-Checkins
mailing list