[Checkins] SVN: grok/branches/jspaans-traversableattrs/src/grok/ Initial try at implementing grok.traversable

Jasper Spaans jspaans at thehealthagency.com
Fri May 2 05:05:58 EDT 2008


Log message for revision 86051:
  Initial try at implementing grok.traversable

Changed:
  U   grok/branches/jspaans-traversableattrs/src/grok/__init__.py
  U   grok/branches/jspaans-traversableattrs/src/grok/admin/docgrok.txt
  U   grok/branches/jspaans-traversableattrs/src/grok/components.py
  U   grok/branches/jspaans-traversableattrs/src/grok/directive.py
  A   grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py
  A   grok/branches/jspaans-traversableattrs/src/grok/tests/directive/multipleasdict.py
  U   grok/branches/jspaans-traversableattrs/src/grok/util.py

-=-
Modified: grok/branches/jspaans-traversableattrs/src/grok/__init__.py
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/__init__.py	2008-05-02 09:01:14 UTC (rev 86050)
+++ grok/branches/jspaans-traversableattrs/src/grok/__init__.py	2008-05-02 09:05:58 UTC (rev 86051)
@@ -48,7 +48,7 @@
     context, name, title, provides, baseclass, global_utility, direct, order)
 from grok.directive import (
     template, templatedir, local_utility, permissions, require, site,
-    layer, viewletmanager, view)
+    layer, viewletmanager, view, traversable)
 from grokcore.component.decorators import subscribe, adapter, implementer
 from martian.error import GrokError, GrokImportError
 

Modified: grok/branches/jspaans-traversableattrs/src/grok/admin/docgrok.txt
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/admin/docgrok.txt	2008-05-02 09:01:14 UTC (rev 86050)
+++ grok/branches/jspaans-traversableattrs/src/grok/admin/docgrok.txt	2008-05-02 09:05:58 UTC (rev 86051)
@@ -11,7 +11,7 @@
 DocGrok is a helper to generate documentation for nearly everything
 living in a running Zope 3 instance. Basically it provides
 'information'-objects for different types of things, like modules,
-classes, functions, textfiles etc. 
+classes, functions, textfiles etc.
 
 A ``DocGrok`` therefore is an object wich normally is bound to a
 certain entity, which is describable by a dotted path. It provides
@@ -230,7 +230,7 @@
     >>> entry['name']
     'url'
     >>> entry['doc'].strip()
-    'Return string for the URL based on the obj and name. The data \n    argument is used to form a CGI query string.'
+    'Return string for the URL based on the obj and name. The data\n    argument is used to form a CGI query string.'
     >>> entry['signature']
     '(obj=None, name=None, data=None)'
 

Modified: grok/branches/jspaans-traversableattrs/src/grok/components.py
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/components.py	2008-05-02 09:01:14 UTC (rev 86050)
+++ grok/branches/jspaans-traversableattrs/src/grok/components.py	2008-05-02 09:05:58 UTC (rev 86051)
@@ -192,7 +192,7 @@
 
 
     def url(self, obj=None, name=None, data=None):
-        """Return string for the URL based on the obj and name. The data 
+        """Return string for the URL based on the obj and name. The data
         argument is used to form a CGI query string.
         """
         if isinstance(obj, basestring):
@@ -209,7 +209,7 @@
         elif name is not None and obj is None:
             # create URL to view on context
             obj = self.context
-            
+
         if data is None:
             data = {}
         else:
@@ -436,6 +436,15 @@
         if subob is not None:
             return util.safely_locate_maybe(subob, self.context, name)
 
+        traversable_dict = getattr(self.context, '__grok_traversable__', None)
+        if traversable_dict:
+            if name in traversable_dict:
+                subob = getattr(self.context, traversable_dict[name])
+                if callable(subob):
+                    subob = subob()
+                print 'XXX', subob, dir(subob), subob.__parent__
+                return util.safely_locate_maybe(subob, self.context, name)
+
         # XXX Special logic here to deal with containers.  It would be
         # good if we wouldn't have to do this here. One solution is to
         # rip this out and make you subclass ContainerTraverser if you

Modified: grok/branches/jspaans-traversableattrs/src/grok/directive.py
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/directive.py	2008-05-02 09:01:14 UTC (rev 86050)
+++ grok/branches/jspaans-traversableattrs/src/grok/directive.py	2008-05-02 09:05:58 UTC (rev 86051)
@@ -17,7 +17,7 @@
 from zope.interface.interfaces import IInterface
 
 from martian.error import GrokImportError
-from martian.directive import (OnceDirective,
+from martian.directive import (Directive, OnceDirective,
                                MultipleTimesDirective, BaseTextDirective,
                                SingleValue, SingleTextDirective,
                                MultipleTextDirective,
@@ -54,7 +54,22 @@
         self.public = public
         self.name_in_container = name_in_container
 
+class MultipleTimesAsDictDirective(Directive):
+    def store(self, frame, value):
+        values = frame.f_locals.get(self.local_name, {})
+        values[value[1]] = value[0]
+        frame.f_locals[self.local_name] = values
 
+class TraversableDirective(MultipleTimesAsDictDirective):
+    def check_argument_signature(self, attr, name=None):
+        pass
+    def check_arguments(self, attr, name=None):
+        pass
+    def value_factory(self, attr, name=None):
+        if name is None:
+            name = attr
+        return (attr, name)
+
 class RequireDirective(BaseTextDirective, SingleValue, MultipleTimesDirective):
 
     def store(self, frame, value):
@@ -87,3 +102,4 @@
                                            ClassOrModuleDirectiveContext())
 view = InterfaceOrClassDirective('grok.view',
                                  ClassOrModuleDirectiveContext())
+traversable = TraversableDirective('grok.traversable', ClassDirectiveContext())

Added: grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py	                        (rev 0)
+++ grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py	2008-05-02 09:05:58 UTC (rev 86051)
@@ -0,0 +1,51 @@
+"""
+Models can determine how they want to be traversed by
+implementing a 'traverse' method:
+
+  >>> getRootFolder()["traversefoo"] = TraFoo('foo')
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+  >>> browser.open("http://localhost/traversefoo/")
+  >>> print browser.contents
+  <html>
+  <body>foo</body>
+  </html>
+  >>> browser.open("http://localhost/traversefoo/foo")
+  >>> print browser.contents
+  foo
+  >>> browser.open("http://localhost/traversefoo/bar")
+  >>> print browser.contents
+  <html>
+  <body>bar</body>
+  </html>
+
+
+"""
+import grok
+
+class TraBar(grok.Model):
+    def __init__(self, name):
+        self.name = name
+
+#class TraBarIndex(grok.View):
+#    grok.context(TraBar)
+#    def render(self):
+#        return self.name
+
+class TraFoo(grok.Model): #, grok.Application):
+    grok.traversable('bar')
+    grok.traversable('foo')
+
+    def __init__(self, name):
+        self.name = name
+
+    foo = TraBar('foo')
+    def bar(self):
+        return TraBar('bar')
+
+#class TraFooIndex(grok.View):
+#    grok.context(TraFoo)
+#    def render(self):
+#        return self.name


Property changes on: grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py
___________________________________________________________________
Name: svn:eol-style
   + native

Copied: grok/branches/jspaans-traversableattrs/src/grok/tests/directive/multipleasdict.py (from rev 86006, grok/branches/jspaans-traversableattrs/src/grok/tests/directive/multipletimes.py)
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/tests/directive/multipleasdict.py	                        (rev 0)
+++ grok/branches/jspaans-traversableattrs/src/grok/tests/directive/multipleasdict.py	2008-05-02 09:05:58 UTC (rev 86051)
@@ -0,0 +1,27 @@
+"""
+The MultipleTimesAsDictDirective is used by grok.traversable so multiple
+attributes can be mentioned.
+
+  >>> from martian import scan
+  >>> from grok.tests.directive import multipleasdict
+  >>> module_info = scan.module_info_from_module(multipleasdict)
+
+  >>> g = Club.__grok_traversable__
+  >>> isinstance(g, dict)
+  True
+  >>> g['demo']
+  'demo'
+  >>> g['attr']
+  'attr'
+  >>> g['asdf']
+  'attr'
+"""
+import grok
+from zope import interface
+
+class Club(grok.Model):
+    grok.traversable('asdf', name='attr')
+    grok.traversable('attr')
+    grok.traversable('attr', name='asdf')
+    grok.traversable('demo')
+    demo = 'something'

Modified: grok/branches/jspaans-traversableattrs/src/grok/util.py
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/util.py	2008-05-02 09:01:14 UTC (rev 86050)
+++ grok/branches/jspaans-traversableattrs/src/grok/util.py	2008-05-02 09:05:58 UTC (rev 86051)
@@ -89,7 +89,7 @@
             if isinstance(v, unicode):
                 data[k] = v.encode('utf-8')
             if isinstance(v, (list, set, tuple)):
-                data[k] = [isinstance(item, unicode) and item.encode('utf-8') 
+                data[k] = [isinstance(item, unicode) and item.encode('utf-8')
                 or item for item in v]
         url += '?' + urllib.urlencode(data, doseq=True)
     return url



More information about the Checkins mailing list