[Checkins] SVN: megrok.traject/trunk/ Can now also use @classmethod for factory and arguments.

Martijn Faassen faassen at startifact.com
Sun Aug 8 10:47:43 EDT 2010


Log message for revision 115546:
  Can now also use @classmethod for factory and arguments.
  

Changed:
  U   megrok.traject/trunk/CHANGES.txt
  A   megrok.traject/trunk/src/megrok/traject/ftests/traject/classmethod.py
  A   megrok.traject/trunk/src/megrok/traject/ftests/traject/modelclassmethod.py
  U   megrok.traject/trunk/src/megrok/traject/meta.py

-=-
Modified: megrok.traject/trunk/CHANGES.txt
===================================================================
--- megrok.traject/trunk/CHANGES.txt	2010-08-08 12:13:43 UTC (rev 115545)
+++ megrok.traject/trunk/CHANGES.txt	2010-08-08 14:47:42 UTC (rev 115546)
@@ -1,12 +1,13 @@
 CHANGES.txt
 ***********
 
-0.10.2 (unreleased)
--------------------
+1.0 (unreleased)
+----------------
 
-- Nothing changed yet.
+- Can now also use @classmethod for ``factory`` and
+  ``arguments``. This is useful if these functions depend on
+  information on the class they are defined on.
 
-
 0.10.1 (2010-01-25)
 -------------------
 

Added: megrok.traject/trunk/src/megrok/traject/ftests/traject/classmethod.py
===================================================================
--- megrok.traject/trunk/src/megrok/traject/ftests/traject/classmethod.py	                        (rev 0)
+++ megrok.traject/trunk/src/megrok/traject/ftests/traject/classmethod.py	2010-08-08 14:47:42 UTC (rev 115546)
@@ -0,0 +1,79 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation 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.
+#
+##############################################################################
+"""
+
+We test traject, but with classmethods instead of functions.
+
+We create an app:
+
+  >>> getRootFolder()["app"] = App()
+
+We set up a test browser:
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
+  >>> browser.handleErrors = False
+
+We we traverse from the app URL, we expect things to happen::
+
+  >>> browser.open("http://localhost/app/mammoths/Knuth")
+  >>> print browser.contents
+  The name of this mammoth is Knuth.
+
+We can also go to another view::
+
+  >>> browser.open("http://localhost/app/mammoths/Knuth/other")
+  >>> print browser.contents
+  This is indeed Knuth
+  
+"""
+
+import grok
+
+from megrok import traject
+
+class Mammoth(grok.Model):
+    def __init__(self, name):
+        self.name = name
+
+class MammothIndex(grok.View):
+    grok.context(Mammoth)
+    grok.name('index')
+    def render(self):
+        return 'The name of this mammoth is %s.' % self.context.name
+
+class MammothOther(grok.View):
+    grok.context(Mammoth)
+    grok.name('other')
+    def render(self):
+        return "This is indeed %s" % self.context.name
+
+class App(grok.Application, grok.Model):
+    pass
+
+class MammothTraject(traject.Traject):
+    grok.context(App)
+
+    pattern = 'mammoths/:name'
+    model = Mammoth
+
+    @classmethod
+    def factory(cls, name):
+        return cls.model(name)
+
+    @classmethod
+    def arguments(cls, mammoth):
+        return dict(name=mammoth.name)
+

Added: megrok.traject/trunk/src/megrok/traject/ftests/traject/modelclassmethod.py
===================================================================
--- megrok.traject/trunk/src/megrok/traject/ftests/traject/modelclassmethod.py	                        (rev 0)
+++ megrok.traject/trunk/src/megrok/traject/ftests/traject/modelclassmethod.py	2010-08-08 14:47:42 UTC (rev 115546)
@@ -0,0 +1,85 @@
+##############################################################################
+#
+# Copyright (c) 2009 Zope Foundation 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.
+#
+##############################################################################
+"""
+
+We create an app:
+
+  >>> getRootFolder()["app"] = app = App()
+
+We set up a test browser:
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')
+  >>> browser.handleErrors = False
+
+We we traverse from the app URL, we expect things to happen::
+
+  >>> browser.open("http://localhost/app/mammoths/Knuth")
+  >>> print browser.contents
+  The name of this mammoth is Knuth.
+
+We can also go to another view::
+
+  >>> browser.open("http://localhost/app/mammoths/Knuth/other")
+  >>> print browser.contents
+  This is indeed Knuth
+
+We can also locate a mammoth::
+
+  >>> mammoth = Mammoth('Dijkstra')
+  >>> class Default(object):
+  ...    def __init__(self, *args, **kw):
+  ...        pass
+  >>> traject.locate(app, mammoth, Default)
+  >>> mammoth.__name__
+  u'Dijkstra'
+  >>> mammoth.__parent__
+  <megrok.traject.ftests.traject.modelclassmethod.Default object at ...>
+
+"""
+
+import grok
+
+from megrok import traject
+
+class App(grok.Application, grok.Model):
+    pass
+
+class Mammoth(traject.Model):
+    traject.context(App)
+    traject.pattern('mammoths/:name')
+    
+    def __init__(self, name):
+        self.name = name
+
+    @classmethod
+    def factory(cls, name):
+        return Mammoth(name)
+
+    @classmethod
+    def arguments(cls, self):
+        return dict(name=self.name)
+
+class MammothIndex(grok.View):
+    grok.context(Mammoth)
+    grok.name('index')
+    def render(self):
+        return 'The name of this mammoth is %s.' % self.context.name
+
+class MammothOther(grok.View):
+    grok.context(Mammoth)
+    grok.name('other')
+    def render(self):
+        return "This is indeed %s" % self.context.name

Modified: megrok.traject/trunk/src/megrok/traject/meta.py
===================================================================
--- megrok.traject/trunk/src/megrok/traject/meta.py	2010-08-08 12:13:43 UTC (rev 115545)
+++ megrok.traject/trunk/src/megrok/traject/meta.py	2010-08-08 14:47:42 UTC (rev 115546)
@@ -14,13 +14,14 @@
     def execute(self, factory, config, context, **kw):
         pattern_str = factory.pattern
         model = factory.model
-        factory_func = factory.factory.im_func
-        arguments_func = factory.arguments.im_func
 
+        factory_func = _get_func(factory, 'factory')
+        arguments_func = _get_func(factory, 'arguments')
+
         _register_traject(config, context, pattern_str, model,
                           factory_func, arguments_func)
         return True
-    
+
 class ModelGrokker(martian.ClassGrokker):
     martian.component(components.Model)
 
@@ -30,13 +31,22 @@
     def execute(self, factory, config, context, pattern, **kw):
         pattern_str = pattern
         model = factory
-        factory_func = factory.factory.im_func
-        arguments_func = factory.arguments.im_func
 
+        factory_func = _get_func(factory, 'factory')
+        arguments_func = _get_func(factory, 'arguments')
+
         _register_traject(config, context, pattern_str, model,
                           factory_func, arguments_func)
         return True
 
+def _get_func(cls, name):
+    f = getattr(cls, name)
+    # detect a @classmethod (XXX is this correct?)
+    if f.im_self is not None:
+        return f
+    # return function underlying method implementation
+    return f.im_func
+
 def _register_traject(config, context,
                       pattern_str, model, factory_func, arguments_func):
     # register



More information about the checkins mailing list