[Checkins] SVN: grok/branches/timte-json/ A first shot at json support.

Tim Terlegård tim.terlegard at lovelysystems.com
Mon Mar 19 16:20:28 EDT 2007


Log message for revision 73356:
  A first shot at json support.
  

Changed:
  A   grok/branches/timte-json/
  U   grok/branches/timte-json/src/grok/__init__.py
  U   grok/branches/timte-json/src/grok/components.py
  U   grok/branches/timte-json/src/grok/meta.py

-=-
Copied: grok/branches/timte-json (from rev 73355, grok/trunk)

Modified: grok/branches/timte-json/src/grok/__init__.py
===================================================================
--- grok/trunk/src/grok/__init__.py	2007-03-19 20:05:54 UTC (rev 73355)
+++ grok/branches/timte-json/src/grok/__init__.py	2007-03-19 20:20:27 UTC (rev 73356)
@@ -30,7 +30,7 @@
     IContainerModifiedEvent, ContainerModifiedEvent)
 
 from grok.components import ClassGrokker, InstanceGrokker, ModuleGrokker
-from grok.components import Model, Adapter, MultiAdapter, View, XMLRPC
+from grok.components import Model, Adapter, MultiAdapter, View, XMLRPC, JSON
 from grok.components import PageTemplate, PageTemplateFile, Container, Traverser
 from grok.components import Site, GlobalUtility, LocalUtility, Annotation
 from grok.components import Application, Form, AddForm, EditForm, DisplayForm

Modified: grok/branches/timte-json/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2007-03-19 20:05:54 UTC (rev 73355)
+++ grok/branches/timte-json/src/grok/components.py	2007-03-19 20:20:27 UTC (rev 73356)
@@ -220,6 +220,8 @@
 class XMLRPC(object):
     pass
 
+class JSON(BrowserPage):
+    pass
 
 class GrokPageTemplate(object):
 

Modified: grok/branches/timte-json/src/grok/meta.py
===================================================================
--- grok/trunk/src/grok/meta.py	2007-03-19 20:05:54 UTC (rev 73355)
+++ grok/branches/timte-json/src/grok/meta.py	2007-03-19 20:20:27 UTC (rev 73356)
@@ -211,6 +211,48 @@
                                 % (method.__name__, factory), factory)
 
 
+class JSONGrokker(grok.ClassGrokker):
+    component_class = grok.JSON
+
+    def register(self, context, name, factory, module_info, templates):
+        view_context = util.determine_class_context(factory, context)
+        # XXX We should really not make __FOO__ methods available to
+        # the outside -- need to discuss how to restrict such things.
+        methods = util.methods_from_class(factory)
+
+        # Determine the default permission for the XMLRPC methods.
+        # There can only be 0 or 1 of those.
+        permissions = util.class_annotation(factory, 'grok.require', [])
+        if not permissions:
+            default_permission = None
+        elif len(permissions) == 1:
+            default_permission = permissions[0]
+        else:
+            raise GrokError('grok.require was called multiple times in '
+                            '%r. It may only be called once on class level.'
+                            % factory, factory)
+
+        for method in methods:
+            method_view = type(
+                factory.__name__, (factory,),
+                {'__call__': method}
+                )
+            component.provideAdapter(
+                method_view, (view_context, IDefaultBrowserLayer),
+                interface.Interface,
+                name=method.__name__)
+
+            # Protect method_view with either the permission that was
+            # set on the method, the default permission from the class
+            # level or zope.Public.
+            permission = getattr(method, '__grok_require__', default_permission)
+            if permission is None or permission == 'zope.Public':
+                checker = NamesChecker(['__call__'])
+            else:
+                checker = NamesChecker(['__call__'], permission)
+            defineChecker(method_view, checker)
+
+
 class TraverserGrokker(grok.ClassGrokker):
     component_class = grok.Traverser
 



More information about the Checkins mailing list