[Checkins] SVN: grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/ use always the right site manager

Christian Klinger cklinger at novareto.de
Wed Nov 30 10:37:31 UTC 2011


Log message for revision 123552:
  use always the right site manager

Changed:
  U   grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/meta.py
  U   grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/util.py

-=-
Modified: grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/meta.py
===================================================================
--- grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/meta.py	2011-11-30 10:20:21 UTC (rev 123551)
+++ grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/meta.py	2011-11-30 10:37:31 UTC (rev 123552)
@@ -22,6 +22,7 @@
 from zope import component, interface
 from martian.error import GrokError
 from zope.interface import implementedBy
+from grokcore.component import util
 
 def _provides(component, module=None, **data):
     martian.util.check_implements_one(component)
@@ -43,7 +44,7 @@
     def execute(self, factory, config, context, provides, name, **kw):
         config.action(
             discriminator=('adapter', context, provides, name),
-            callable=component.provideAdapter,
+            callable=util.provideAdapter,
             args=(factory, (context,), provides, name),
             )
         return True
@@ -63,7 +64,7 @@
 
         config.action(
             discriminator=('adapter', for_, provides, name),
-            callable=component.provideAdapter,
+            callable=util.provideAdapter,
             args=(factory, None, provides, name),
             )
         return True
@@ -78,7 +79,7 @@
     def execute(self, factory, config, context, provides, name, **kw):
         config.action(
             discriminator=None,
-            callable=component.provideSubscriptionAdapter,
+            callable=util.provideSubscriptionAdapter,
             args=(factory, (context,), provides),
             )
         return True
@@ -98,7 +99,7 @@
 
         config.action(
             discriminator=None,
-            callable=component.provideSubscriptionAdapter,
+            callable=util.provideSubscriptionAdapter,
             args=(factory, adapts, provides),
             )
         return True
@@ -122,7 +123,7 @@
 
         config.action(
             discriminator=('utility', provides, name),
-            callable=component.provideUtility,
+            callable=util.provideUtility,
             args=(factory, provides, name),
             )
         return True
@@ -149,7 +150,7 @@
             name = getattr(function, '__component_name__', u"")
             config.action(
                 discriminator=('adapter', interfaces, function.__implemented__, name),
-                callable=component.provideAdapter,
+                callable=util.provideAdapter,
                 args=(function, interfaces, function.__implemented__, name),
                 )
         return True
@@ -180,7 +181,7 @@
 
             config.action(
                 discriminator=('utility', provides, name),
-                callable=component.provideUtility,
+                callable=util.provideUtility,
                 args=(obj, provides, name),
                 )
 
@@ -204,7 +205,7 @@
 
             config.action(
                 discriminator=('adapter', adapts, provides, name),
-                callable=component.provideAdapter,
+                callable=util.provideAdapter,
                 args=(factory, adapts, provides, name),
                 )
 
@@ -229,17 +230,17 @@
             if provides is None:
                 config.action(
                     discriminator=None,
-                    callable=component.provideHandler,
+                    callable=util.provideHandler,
                     args=(factory, subscribed))
             else:
                 config.action(
                     discriminator=None,
-                    callable=component.provideSubscriptionAdapter,
+                    callable=util.provideSubscriptionAdapter,
                     args=(factory, subscribed, provides))
 
             for iface in subscribed:
                 config.action(
                     discriminator=None,
-                    callable=zope.component.interface.provideInterface,
+                    callable=util.provideInterface,
                     args=('', iface))
         return True

Modified: grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/util.py
===================================================================
--- grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/util.py	2011-11-30 10:20:21 UTC (rev 123551)
+++ grokcore.component/branches/goschtl-advanced-sitemanager/src/grokcore/component/util.py	2011-11-30 10:37:31 UTC (rev 123552)
@@ -13,7 +13,11 @@
 ##############################################################################
 """Grok utility functions.
 """
+import zope.component
+import zope.component.hooks
+from types import ClassType
 from grokcore.component import directive
+from zope.interface.interfaces import IInterface
 
 def _sort_key(component):
     # If components have a grok.order directive, sort by that.
@@ -29,3 +33,109 @@
     `grok.order`.
     """
     return sorted(components, key=_sort_key)
+
+
+def getSite():
+    site = zope.component.hooks.getSite()
+    if site is None:
+        sm = zope.component.getSiteManager()
+    else:
+        sm = site.getSiteManager()
+    return sm
+
+
+def provideUtility(component, provides=None, name=u''):
+    sm = getSite()
+    sm.registerUtility(component, provides, name, event=False) 
+
+
+def provideAdapter(factory, adapts=None, provides=None, name=''):
+    sm = getSite()
+    sm.registerAdapter(factory, adapts, provides, name, event=False)
+
+
+def provideSubscriptionAdapter(factory, adapts=None, provides=None):
+    sm = getSite()
+    sm.registerSubscriptionAdapter(factory, adapts, provides, event=False)
+
+
+def provideHandler(factory, adapts=None):
+    sm = getSite()
+    sm.registerHandler(factory, adapts, event=False)
+
+def provideInterface(id, interface, iface_type=None, info=''):
+    """register Interface with global site manager as utility
+
+    >>> gsm = zope.component.getGlobalSiteManager()
+
+    >>> from zope.interface import Interface
+    >>> from zope.interface.interfaces import IInterface
+    >>> from zope.component.tests import ITestType
+
+    >>> class I(Interface):
+    ...     pass
+    >>> IInterface.providedBy(I)
+    True
+    >>> ITestType.providedBy(I)
+    False
+    >>> interfaces = gsm.getUtilitiesFor(ITestType)
+    >>> list(interfaces)
+    []
+
+    # provide first interface type
+    >>> provideInterface('', I, ITestType)
+    >>> ITestType.providedBy(I)
+    True
+    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
+    >>> [name for (name, iface) in interfaces]
+    [u'zope.component.interface.I']
+    >>> [iface.__name__ for (name, iface) in interfaces]
+    ['I']
+
+    # provide second interface type
+    >>> class IOtherType(IInterface):
+    ...     pass
+    >>> provideInterface('', I, IOtherType)
+
+    >>> ITestType.providedBy(I)
+    True
+    >>> IOtherType.providedBy(I)
+    True
+    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
+    >>> [name for (name, iface) in interfaces]
+    [u'zope.component.interface.I']
+    >>> interfaces = list(gsm.getUtilitiesFor(IOtherType))
+    >>> [name for (name, iface) in interfaces]
+    [u'zope.component.interface.I']
+
+    >>> class I1(Interface):
+    ...     pass
+    >>> provideInterface('', I1)
+    >>> IInterface.providedBy(I1)
+    True
+    >>> ITestType.providedBy(I1)
+    False
+    >>> interfaces = list(gsm.getUtilitiesFor(ITestType))
+    >>> [name for (name, iface) in interfaces]
+    [u'zope.component.interface.I']
+    >>> [iface.__name__ for (name, iface) in interfaces]
+    ['I']
+    """
+    if not id:
+        id = "%s.%s" % (interface.__module__, interface.__name__)
+
+    if not IInterface.providedBy(interface):
+        if not isinstance(interface, (type, ClassType)):
+            raise TypeError(id, "is not an interface or class")
+        return
+
+    if iface_type is not None:
+        if not iface_type.extends(IInterface):
+            raise TypeError(iface_type, "is not an interface type")
+        alsoProvides(interface, iface_type)
+    else:
+        iface_type = IInterface
+
+    sm = getSite() 
+    sm.registerUtility(interface, iface_type, id, info)
+



More information about the checkins mailing list