[Checkins] SVN: grok/branches/jw-philipp-using-ndir-directives/src/grok/ Switch over all directives to the new-style implementation. Adjust many places in grokkers.

Philipp von Weitershausen philikon at philikon.de
Sat May 3 09:14:42 EDT 2008


Log message for revision 86205:
  Switch over all directives to the new-style implementation.  Adjust many places in grokkers.

Changed:
  U   grok/branches/jw-philipp-using-ndir-directives/src/grok/directive.py
  U   grok/branches/jw-philipp-using-ndir-directives/src/grok/meta.py
  U   grok/branches/jw-philipp-using-ndir-directives/src/grok/templatereg.py

-=-
Modified: grok/branches/jw-philipp-using-ndir-directives/src/grok/directive.py
===================================================================
--- grok/branches/jw-philipp-using-ndir-directives/src/grok/directive.py	2008-05-03 12:50:28 UTC (rev 86204)
+++ grok/branches/jw-philipp-using-ndir-directives/src/grok/directive.py	2008-05-03 13:14:42 UTC (rev 86205)
@@ -14,45 +14,39 @@
 """Grok directives.
 """
 
+import sys
 import grok
 from zope.interface.interfaces import IInterface
 from zope.publisher.interfaces.browser import IBrowserView
 
 import martian
-from martian.error import GrokImportError
-from martian.directive import (Directive, OnceDirective,
-                               MultipleTimesDirective, BaseTextDirective,
-                               SingleValue, SingleTextDirective,
-                               MultipleTextDirective,
-                               MarkerDirective,
-                               InterfaceDirective,
-                               InterfaceOrClassDirective,
-                               ModuleDirectiveContext,
-                               OptionalValueDirective,
-                               ClassDirectiveContext,
-                               ClassOrModuleDirectiveContext)
 from martian import util
+from martian.error import GrokImportError
+from martian.directive import StoreMultipleTimes
 from grok import components
 
-class MultiValueOnceDirective(OnceDirective):
+# Define grok directives
+class template(martian.Directive):
+    scope = martian.CLASS
+    store = martian.ONCE
+    validate = martian.validateText
 
-    def check_arguments(self, *values):
-        pass
+class templatedir(martian.Directive):
+    scope = martian.MODULE
+    store = martian.ONCE
+    validate = martian.validateText
 
-    def value_factory(self, *args):
-        return args
+class local_utility(martian.MultipleTimesDirective):
+    scope = martian.CLASS
 
-class LocalUtilityDirective(MultipleTimesDirective):
-    def check_arguments(self, factory, provides=None, name=u'',
-                        setup=None, public=False, name_in_container=None):
+    def factory(self, factory, provides=None, name=u'',
+                setup=None, public=False, name_in_container=None):
         if provides is not None and not IInterface.providedBy(provides):
             raise GrokImportError("You can only pass an interface to the "
                                   "provides argument of %s." % self.name)
+        return LocalUtilityInfo(factory, provides, name, setup,
+                                public, name_in_container)
 
-    def value_factory(self, *args, **kw):
-        return LocalUtilityInfo(*args, **kw)
-
-
 class LocalUtilityInfo(object):
     def __init__(self, factory, provides=None, name=u'',
                  setup=None, public=False, name_in_container=None):
@@ -65,16 +59,16 @@
         self.public = public
         self.name_in_container = name_in_container
 
+class RequireDirectiveStore(StoreMultipleTimes):
 
-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
+    def pop(self, locals_, directive):
+        return locals_[directive.dotted_name()].pop()
 
+class require(martian.Directive):
+    scope = martian.CLASS
+    store = RequireDirectiveStore()
 
-class RequireDirective(SingleValue, MultipleTimesDirective):
-    def check_arguments(self, value):
+    def validate(self, value):
         if util.check_subclass(value, components.Permission):
             return
         if util.not_unicode_or_ascii(value):
@@ -82,41 +76,26 @@
                 "You can only pass unicode, ASCII, or a subclass "
                 "of grok.Permission %s." % self.name)
 
-    def store(self, frame, value):
+    def factory(self, value):
         if util.check_subclass(value, components.Permission):
-            value = grok.name.get(value)
+            return grok.name.get(value)
+        return value
 
-        super(RequireDirective, self).store(frame, value)
-        values = frame.f_locals.get(self.local_name, [])
-
+    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
+        frame = sys._getframe(1)
+        permission = self.store.pop(frame.f_locals, self)
+        self.set(func, permission)
+        return func
 
-
-# Define grok directives
-class template(martian.Directive):
+class site(martian.Directive):
     scope = martian.CLASS
     store = martian.ONCE
-    validate = martian.validateText
+    validate = martian.validateInterfaceOrClass
 
-class templatedir(martian.Directive):
-    scope = martian.MODULE
-    store = martian.ONCE
-    validate = martian.validateText
-
-local_utility = LocalUtilityDirective('grok.local_utility',
-                                      ClassDirectiveContext())
-require = RequireDirective('grok.require', ClassDirectiveContext())
-site = InterfaceOrClassDirective('grok.site',
-                                 ClassDirectiveContext())
-
 class permissions(martian.Directive):
     scope = martian.CLASS
     store = martian.ONCE
@@ -126,6 +105,7 @@
         return args
 
 class OneInterfaceOrClassOnClassOrModule(martian.Directive):
+    """Convenience base class.  Not for public use."""
     scope = martian.CLASS_OR_MODULE
     store = martian.ONCE
     validate = martian.validateInterfaceOrClass

Modified: grok/branches/jw-philipp-using-ndir-directives/src/grok/meta.py
===================================================================
--- grok/branches/jw-philipp-using-ndir-directives/src/grok/meta.py	2008-05-03 12:50:28 UTC (rev 86204)
+++ grok/branches/jw-philipp-using-ndir-directives/src/grok/meta.py	2008-05-03 13:14:42 UTC (rev 86205)
@@ -116,8 +116,9 @@
             # 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)
+            permission = grok.require.get(method)
+            if permission is None:
+                permission = default_permission
 
             config.action(
                 discriminator=('protectName', method_view, '__call__'),
@@ -169,8 +170,10 @@
             # 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)
+            permission = grok.require.get(method)
+            if permission is None:
+                permission = default_permission
+
             config.action(
                 discriminator=('protectName', method_view, '__call__'),
                 callable=make_checker,
@@ -212,7 +215,7 @@
         # @grok.require on any of the view's methods.
         methods = util.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.'
@@ -298,8 +301,9 @@
             # set on the method, the default permission from the class
             # level or zope.Public.
 
-            permission = getattr(method, '__grok_require__',
-                                 default_permission)
+            permission = grok.require.get(method)
+            if permission is None:
+                permission = default_permission
 
             config.action(
                 discriminator=('protectName', method_view, '__call__'),
@@ -433,8 +437,8 @@
     priority = 500
 
     def grok(self, name, factory, module_info, config, **kw):
-        infos = util.class_annotation_list(factory, 'grok.local_utility', None)
-        if infos is None:
+        infos = grok.local_utility.get(factory)
+        if not infos:
             return False
 
         for info in infos:
@@ -468,8 +472,7 @@
         # raise an error in case of any duplicate registrations
         # on the class level (subclassing overrides, see below)
         used = set()
-        class_infos = util.class_annotation(factory, 'grok.local_utility',
-                                            [])
+        class_infos = grok.local_utility.get(factory)
         for info in class_infos:
             key = (info.provides, info.name)
             if key in used:
@@ -683,7 +686,7 @@
     component_class = components.IndexesClass
 
     def grok(self, name, factory, module_info, config, **kw):
-        site = util.class_annotation(factory, 'grok.site', None)
+        site = grok.site.get(factory)
         if site is None:
             raise GrokError("No site specified for grok.Indexes "
                             "subclass in module %r. "

Modified: grok/branches/jw-philipp-using-ndir-directives/src/grok/templatereg.py
===================================================================
--- grok/branches/jw-philipp-using-ndir-directives/src/grok/templatereg.py	2008-05-03 12:50:28 UTC (rev 86204)
+++ grok/branches/jw-philipp-using-ndir-directives/src/grok/templatereg.py	2008-05-03 13:14:42 UTC (rev 86205)
@@ -24,8 +24,9 @@
         return entry['template']
 
     def findFilesystem(self, module_info):
-        template_dir_name = module_info.getAnnotation(
-            'grok.templatedir', module_info.name + '_templates')
+        template_dir_name = grok.templatedir.get(module_info.getModule())
+        if template_dir_name is None:
+            template_dir_name = module_info.name + '_templates'
 
         template_dir = module_info.getResourcePath(template_dir_name)
 
@@ -87,8 +88,9 @@
     def checkTemplates(self, module_info, factory, component_name,
                        has_render, has_no_render):
         factory_name = factory.__name__.lower()
-        template_name = util.class_annotation(factory, 'grok.template',
-                                              factory_name)
+        template_name = grok.template.get(factory)
+        if template_name is None:
+            template_name = factory_name
 
         if factory_name != template_name:
             # grok.template is being used



More information about the Checkins mailing list