[Checkins] SVN: grok/trunk/src/grok/ The REST and XMLRPC views would not register methods with names starting with '__'. Factored out

Jan-Wijbrand Kolman janwijbrand at gmail.com
Fri Jan 4 16:02:54 EST 2008


Log message for revision 82669:
  The REST and XMLRPC views would not register methods with names starting with '__'. Factored out
  this code into simple util function. Improved this function in order to ignore functions that
  start with an '_' alltogether. The JSON view now makes use of the function too.
  
  

Changed:
  U   grok/trunk/src/grok/meta.py
  A   grok/trunk/src/grok/tests/util/public_methods_from_class.py
  U   grok/trunk/src/grok/util.py

-=-
Modified: grok/trunk/src/grok/meta.py
===================================================================
--- grok/trunk/src/grok/meta.py	2008-01-04 13:47:34 UTC (rev 82668)
+++ grok/trunk/src/grok/meta.py	2008-01-04 21:02:54 UTC (rev 82669)
@@ -47,7 +47,7 @@
 import grok
 from grok import components, formlib, templatereg
 from grok.util import check_adapts, get_default_permission, make_checker
-from grok.util import determine_class_directive
+from grok.util import determine_class_directive, public_methods_from_class
 from grok.rest import RestPublisher
 from grok.interfaces import IRESTSkinType
 
@@ -130,16 +130,13 @@
     def grok(self, name, factory, module_info, config, **kw):
         context = module_info.getAnnotation('grok.context', None)
         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)
 
+        methods = public_methods_from_class(factory)
+
         default_permission = get_default_permission(factory)
 
         for method in methods:
             name = method.__name__
-            if name.startswith('__'):
-                continue
 
             # Make sure that the class inherits MethodPublisher, so that the
             # views have a location
@@ -174,10 +171,9 @@
     def grok(self, name, factory, module_info, config, **kw):
         context = module_info.getAnnotation('grok.context', None)
         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)
 
+        methods = public_methods_from_class(factory)
+
         default_permission = get_default_permission(factory)
 
         # grab layer from class or module
@@ -187,8 +183,6 @@
 
         for method in methods:
             name = method.__name__
-            if name.startswith('__'):
-                continue
 
             # Make sure that the class inherits RestPublisher, so that the
             # views have a location
@@ -294,8 +288,9 @@
     def grok(self, name, factory, module_info, config, **kw):
         context = module_info.getAnnotation('grok.context', None)
         view_context = util.determine_class_context(factory, context)
-        methods = util.methods_from_class(factory)
 
+        methods = public_methods_from_class(factory)
+
         default_permission = get_default_permission(factory)
 
         for method in methods:

Added: grok/trunk/src/grok/tests/util/public_methods_from_class.py
===================================================================
--- grok/trunk/src/grok/tests/util/public_methods_from_class.py	                        (rev 0)
+++ grok/trunk/src/grok/tests/util/public_methods_from_class.py	2008-01-04 21:02:54 UTC (rev 82669)
@@ -0,0 +1,28 @@
+"""
+  >>> methods = grok.util.public_methods_from_class(A)
+  >>> sorted([m.__name__ for m in methods])
+  ['should_also_be_public', 'should_be_public']
+
+"""
+import grok
+import grok.util
+
+class A(object):
+
+    def __init__(self):
+        pass # this method is ignored
+
+    def __call__(self):
+        pass # this method is ignored
+
+    def __double_underscored(self):
+        pass # this method is ignored
+
+    def _single_underscored(self):
+        pass # this method is ignored
+
+    def should_be_public(self):
+        pass # this method is found
+
+    def should_also_be_public(self):
+        pass # this method is found

Modified: grok/trunk/src/grok/util.py
===================================================================
--- grok/trunk/src/grok/util.py	2008-01-04 13:47:34 UTC (rev 82668)
+++ grok/trunk/src/grok/util.py	2008-01-04 21:02:54 UTC (rev 82669)
@@ -25,7 +25,7 @@
 from zope.security.interfaces import IPermission
 
 from martian.error import GrokError, GrokImportError
-from martian.util import class_annotation
+from martian.util import class_annotation, methods_from_class
 
 def check_adapts(class_):
     if component.adaptedBy(class_) is None:
@@ -60,7 +60,7 @@
 
 def get_default_permission(factory):
     """Determine the default permission for a view.
-    
+
     There can be only 0 or 1 default permission.
     """
     permissions = class_annotation(factory, 'grok.require', [])
@@ -78,7 +78,7 @@
     """Given a request and an object, give the URL.
 
     Optionally pass a third argument name which gets added to the URL.
-    """    
+    """
     url = component.getMultiAdapter((obj, request), IAbsoluteURL)()
     if name is None:
         return url
@@ -105,3 +105,7 @@
     if directive is not None:
         return directive
     return default
+
+def public_methods_from_class(factory):
+    return [m for m in methods_from_class(factory) if \
+            not m.__name__.startswith('_')]



More information about the Checkins mailing list