[Checkins] SVN: five.grok/trunk/src/five/grok/ refatoring to use the new style directives

eric casteleijn eric at infrae.com
Sun May 4 15:23:30 EDT 2008


Log message for revision 86414:
  refatoring to use the new style directives

Changed:
  A   five.grok/trunk/src/five/grok/HISTORY.txt
  U   five.grok/trunk/src/five/grok/directive.py
  U   five.grok/trunk/src/five/grok/meta.py
  U   five.grok/trunk/src/five/grok/tests/subscribers.py
  U   five.grok/trunk/src/five/grok/util.py

-=-
Added: five.grok/trunk/src/five/grok/HISTORY.txt
===================================================================
--- five.grok/trunk/src/five/grok/HISTORY.txt	                        (rev 0)
+++ five.grok/trunk/src/five/grok/HISTORY.txt	2008-05-04 19:23:30 UTC (rev 86414)
@@ -0,0 +1,8 @@
+0.1
+---
+
+  Features added:
+
+    - Switched to refactored martian/grokcore.component directives
+    - syntax Inital version
+   
\ No newline at end of file

Modified: five.grok/trunk/src/five/grok/directive.py
===================================================================
--- five.grok/trunk/src/five/grok/directive.py	2008-05-04 19:22:06 UTC (rev 86413)
+++ five.grok/trunk/src/five/grok/directive.py	2008-05-04 19:23:30 UTC (rev 86414)
@@ -1,22 +1,40 @@
-from martian.directive import MultipleTimesDirective
-from martian.directive import BaseTextDirective
-from martian.directive import SingleValue
-from martian.directive import ClassDirectiveContext
+import martian
+import martian.directive
 
-class RequireDirective(BaseTextDirective, SingleValue, MultipleTimesDirective):
+class RequireDirectiveStore(martian.directive.StoreMultipleTimes):
 
-    def store(self, frame, value):
-        super(RequireDirective, self).store(frame, value)
-        values = frame.f_locals.get(self.local_name, [])
+    def get(self, directive, component, default):
+        permissions = getattr(component, directive.dotted_name(), default)
+        if (permissions is default) or not permissions:
+            return default
+        if len(permissions) > 1:
+            raise GrokError('grok.require was called multiple times in '
+                            '%r. It may only be set once for a class.'
+                            % component, component)
+        return permissions[0]
 
+    def pop(self, locals_, directive):
+        return locals_[directive.dotted_name()].pop()
+
+
+class require(martian.Directive):
+    scope = martian.CLASS
+    store = RequireDirectiveStore()
+    validate = martian.validateText
+
+    def __call__(self, func):
         # grok.require can be used both as a class-level directive and
         # as a decorator for methods.  Therefore we return a decorator
         # here, which may be used for methods, or simply ignored when
         # used as a directive.
-        def decorator(func):
-            permission = values.pop()
-            func.__grok_require__ = permission
-            return func
-        return decorator
-    
-require = RequireDirective('grok.require', ClassDirectiveContext()) 
+        frame = sys._getframe(1)
+        permission = self.store.pop(frame.f_locals, self)
+        self.set(func, [permission])
+        return func
+
+
+class layer(martian.Directive):
+    scope = martian.CLASS_OR_MODULE
+    store = martian.ONCE
+    validate = martian.validateInterfaceOrClass
+

Modified: five.grok/trunk/src/five/grok/meta.py
===================================================================
--- five.grok/trunk/src/five/grok/meta.py	2008-05-04 19:22:06 UTC (rev 86413)
+++ five.grok/trunk/src/five/grok/meta.py	2008-05-04 19:23:30 UTC (rev 86414)
@@ -1,13 +1,12 @@
 import martian
-from martian import util
+from martian.util import methods_from_class
 from martian.error import GrokError
 from zope import interface, component
 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 
 from five import grok
-from five.grok.util import get_default_permission
-from grokcore.component.meta import get_context, get_name_classname
-from grokcore.component.util import determine_class_directive
+from five.grok import util
+
 from Products.Five.security import protectClass
 from Globals import InitializeClass as initializeClass
 
@@ -16,22 +15,10 @@
     component_class = grok.View
 
     def grok(self, name, factory, module_info, config, **kw):
-        view_context = get_context(module_info, factory)
+        view_context = grok.context.get(factory, module_info.getModule())
 
         factory.module_info = module_info
 
-        #if util.check_subclass(factory, components.GrokForm):
-            ## setup form_fields from context class if we've encountered a form
-            #if getattr(factory, 'form_fields', None) is None:
-                #factory.form_fields = formlib.get_auto_fields(view_context)
-
-            #if not getattr(factory.render, 'base_method', False):
-                #raise GrokError(
-                    #"It is not allowed to specify a custom 'render' "
-                    #"method for form %r. Forms either use the default "
-                    #"template or a custom-supplied one." % factory,
-                    #factory)
-
         # find templates
         templates = module_info.getAnnotation('grok.templates', None)
         if templates is not None:
@@ -43,20 +30,18 @@
 
         # safety belt: make sure that the programmer didn't use
         # @grok.require on any of the view's methods.
-        methods = util.methods_from_class(factory)
+        methods = methods_from_class(factory)
         for method in methods:
-            if getattr(method, '__grok_require__', None) is not None:
+            if grok.require.get(method) is not None:
                 raise GrokError('The @grok.require decorator is used for '
                                 'method %r in view %r. It may only be used '
                                 'for XML-RPC methods.'
                                 % (method.__name__, factory), factory)
 
         # grab layer from class or module
-        view_layer = determine_class_directive('grok.layer',
-                                               factory, module_info,
-                                               default=IDefaultBrowserLayer)
+        view_layer = grok.directive.layer.get(factory, module_info.getModule())
 
-        view_name = get_name_classname(factory)
+        view_name = util.get_name_classname(factory)
         # __view_name__ is needed to support IAbsoluteURL on views
         factory.__view_name__ = view_name
         adapts = (view_context, view_layer)
@@ -67,7 +52,7 @@
             args=(factory, adapts, interface.Interface, view_name),
             )
 
-        permission = get_default_permission(factory)
+        permission = grok.require.get(factory)
         config.action(
             discriminator = ('five:protectClass', factory),
             callable = protectClass,

Modified: five.grok/trunk/src/five/grok/tests/subscribers.py
===================================================================
--- five.grok/trunk/src/five/grok/tests/subscribers.py	2008-05-04 19:22:06 UTC (rev 86413)
+++ five.grok/trunk/src/five/grok/tests/subscribers.py	2008-05-04 19:23:30 UTC (rev 86414)
@@ -21,7 +21,7 @@
 
 """
 import zope.event
-import grokcore.component as grok
+from five import grok
 from zope.component.interfaces import IObjectEvent, ObjectEvent
 
 from OFS.interfaces import ISimpleItem
@@ -35,4 +35,4 @@
 
 @grok.subscribe(ISimpleItem, IObjectEvent)
 def itemAddedInstance(item, event):
-    items2.append(item.getId().upper())
\ No newline at end of file
+    items2.append(item.getId().upper())

Modified: five.grok/trunk/src/five/grok/util.py
===================================================================
--- five.grok/trunk/src/five/grok/util.py	2008-05-04 19:22:06 UTC (rev 86413)
+++ five.grok/trunk/src/five/grok/util.py	2008-05-04 19:23:30 UTC (rev 86414)
@@ -1,19 +1,8 @@
 from martian.error import GrokError
+from five import grok
 
-from martian.util import class_annotation
-
-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', [])
-    if not permissions:
-        return 'zope.Public'
-    if len(permissions) > 1:
-        raise GrokError('grok.require was called multiple times in '
-                        '%r. It may only be set once for a class.'
-                        % factory, factory)
-
-    result = permissions[0]
-    return result
+def get_name_classname(factory):
+    name = grok.name.get(factory)
+    if not name:
+        name = factory.__name__.lower()
+    return name



More information about the Checkins mailing list