[Checkins] SVN: grok/trunk/src/grok/ explicit grok.context on module level for disambiguation

Philipp von Weitershausen philikon at philikon.de
Sat Oct 14 14:26:35 EDT 2006


Log message for revision 70623:
  explicit grok.context on module level for disambiguation
  

Changed:
  U   grok/trunk/src/grok/__init__.py
  U   grok/trunk/src/grok/_grok.py
  U   grok/trunk/src/grok/adapter.txt
  A   grok/trunk/src/grok/tests/modulecontext.py

-=-
Modified: grok/trunk/src/grok/__init__.py
===================================================================
--- grok/trunk/src/grok/__init__.py	2006-10-14 18:18:05 UTC (rev 70622)
+++ grok/trunk/src/grok/__init__.py	2006-10-14 18:26:35 UTC (rev 70623)
@@ -15,4 +15,4 @@
 """
 
 from zope.interface import implements
-from _grok import Model, Adapter, grok
+from _grok import Model, Adapter, grok, context

Modified: grok/trunk/src/grok/_grok.py
===================================================================
--- grok/trunk/src/grok/_grok.py	2006-10-14 18:18:05 UTC (rev 70622)
+++ grok/trunk/src/grok/_grok.py	2006-10-14 18:26:35 UTC (rev 70623)
@@ -14,6 +14,7 @@
 """Grok
 """
 import types
+import sys
 from zope.dottedname.resolve import resolve
 from zope import component
 
@@ -54,6 +55,9 @@
         elif check_subclass(obj, Adapter):
             adapters.append(obj)
 
+    if getattr(module, '__grok_context__', None):
+        context = module.__grok_context__
+
     if adapters:
         if context is None:
             raise GrokError("Adapter without context")
@@ -62,3 +66,11 @@
 
     for factory in adapters:
         component.provideAdapter(factory, adapts=(context,))
+
+def context(obj):    
+    locals = sys._getframe(1).f_locals
+
+    if '__grok_context__' in locals:
+        raise GrokError("grok.context can only be called once per class or module.")
+
+    locals['__grok_context__'] = obj

Modified: grok/trunk/src/grok/adapter.txt
===================================================================
--- grok/trunk/src/grok/adapter.txt	2006-10-14 18:18:05 UTC (rev 70622)
+++ grok/trunk/src/grok/adapter.txt	2006-10-14 18:26:35 UTC (rev 70623)
@@ -89,3 +89,20 @@
   Traceback (most recent call last):
     ...
   GrokError: Ambiguous contexts, please use grok.context.
+
+Clean up:
+
+  >>> cleanUp()
+
+Explicit module-level context:
+
+  >>> grok.grok('grok.tests.modulecontext')
+
+  >>> from grok.tests.modulecontext import IHome, Home, Cave
+  >>> cave = Cave()
+  >>> home = IHome(cave)
+
+  >>> IHome.providedBy(home)
+  True
+  >>> isinstance(home, Home)
+  True

Added: grok/trunk/src/grok/tests/modulecontext.py
===================================================================
--- grok/trunk/src/grok/tests/modulecontext.py	2006-10-14 18:18:05 UTC (rev 70622)
+++ grok/trunk/src/grok/tests/modulecontext.py	2006-10-14 18:26:35 UTC (rev 70623)
@@ -0,0 +1,16 @@
+import grok
+from zope import interface
+
+class Cave(grok.Model):
+    pass
+
+class Club(grok.Model):
+    pass
+
+grok.context(Cave)
+
+class IHome(interface.Interface):
+    pass
+
+class Home(grok.Adapter):
+    grok.implements(IHome)


Property changes on: grok/trunk/src/grok/tests/modulecontext.py
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Checkins mailing list