[Checkins] SVN: grok/trunk/src/grok/ prototype of adapter registration

Philipp von Weitershausen philikon at philikon.de
Sat Oct 14 13:18:13 EDT 2006


Log message for revision 70615:
  prototype of adapter registration
  

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

-=-
Modified: grok/trunk/src/grok/__init__.py
===================================================================
--- grok/trunk/src/grok/__init__.py	2006-10-14 16:42:11 UTC (rev 70614)
+++ grok/trunk/src/grok/__init__.py	2006-10-14 17:18:12 UTC (rev 70615)
@@ -13,3 +13,6 @@
 ##############################################################################
 """Grok
 """
+
+from zope.interface import implements
+from _grok import Model, Adapter, grok

Added: grok/trunk/src/grok/_grok.py
===================================================================
--- grok/trunk/src/grok/_grok.py	2006-10-14 16:42:11 UTC (rev 70614)
+++ grok/trunk/src/grok/_grok.py	2006-10-14 17:18:12 UTC (rev 70615)
@@ -0,0 +1,48 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Grok
+"""
+import types
+from zope.dottedname.resolve import resolve
+from zope import component
+
+class Model(object):
+    pass
+
+class Adapter(object):
+
+    def __init__(self, context):
+        self.context = context
+
+class GrokError(Exception):
+    pass
+
+def check_subclass(obj, class_):
+    if type(obj) not in (types.ClassType, type):
+        return False
+    return issubclass(obj, class_)
+
+def grok(dotted_name):
+    # TODO for now we only grok modules
+    module = resolve(dotted_name)
+    context = None
+    for name in dir(module):
+        obj = getattr(module, name)
+
+        if check_subclass(obj, Model):
+            context = obj
+        elif check_subclass(obj, Adapter):
+            if context is None:
+                raise GrokError("Adapter without context")
+            component.provideAdapter(obj, adapts=(context,))


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

Added: grok/trunk/src/grok/adapter.txt
===================================================================
--- grok/trunk/src/grok/adapter.txt	2006-10-14 16:42:11 UTC (rev 70614)
+++ grok/trunk/src/grok/adapter.txt	2006-10-14 17:18:12 UTC (rev 70615)
@@ -0,0 +1,14 @@
+Adapter
+=======
+
+  >>> import grok
+  >>> grok.grok('grok.tests.adapter')
+
+  >>> from grok.tests.adapter import IHome, Home, Cave
+  >>> cave = Cave()
+  >>> home = IHome(cave)
+
+  >>> IHome.providedBy(home)
+  True
+  >>> isinstance(home, Home)
+  True


Property changes on: grok/trunk/src/grok/adapter.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: grok/trunk/src/grok/tests/__init__.py
===================================================================
--- grok/trunk/src/grok/tests/__init__.py	2006-10-14 16:42:11 UTC (rev 70614)
+++ grok/trunk/src/grok/tests/__init__.py	2006-10-14 17:18:12 UTC (rev 70615)
@@ -0,0 +1 @@
+# this is a package


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

Added: grok/trunk/src/grok/tests/adapter.py
===================================================================
--- grok/trunk/src/grok/tests/adapter.py	2006-10-14 16:42:11 UTC (rev 70614)
+++ grok/trunk/src/grok/tests/adapter.py	2006-10-14 17:18:12 UTC (rev 70615)
@@ -0,0 +1,11 @@
+import grok
+from zope import interface
+
+class Cave(grok.Model):
+    pass
+
+class IHome(interface.Interface):
+    pass
+
+class Home(grok.Adapter):
+    grok.implements(IHome)


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

Added: grok/trunk/src/grok/tests/test_adapter.py
===================================================================
--- grok/trunk/src/grok/tests/test_adapter.py	2006-10-14 16:42:11 UTC (rev 70614)
+++ grok/trunk/src/grok/tests/test_adapter.py	2006-10-14 17:18:12 UTC (rev 70615)
@@ -0,0 +1,11 @@
+import unittest
+from zope.testing.doctest import DocFileSuite
+
+def test_suite():
+    return unittest.TestSuite((
+        DocFileSuite('adapter.txt',
+                     package='grok'),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


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



More information about the Checkins mailing list