[Checkins] SVN: grok/branches/grok-martian/src/grok/ Move directive implementation into martian. Also only define GrokError

Martijn Faassen faassen at infrae.com
Wed Jun 20 08:49:51 EDT 2007


Log message for revision 76838:
  Move directive implementation into martian. Also only define GrokError
  and friends in martian.
  

Changed:
  U   grok/branches/grok-martian/src/grok/__init__.py
  U   grok/branches/grok-martian/src/grok/_grok.py
  U   grok/branches/grok-martian/src/grok/directive.py
  D   grok/branches/grok-martian/src/grok/error.py
  U   grok/branches/grok-martian/src/grok/index.py
  U   grok/branches/grok-martian/src/grok/meta.py
  U   grok/branches/grok-martian/src/grok/templatereg.py
  U   grok/branches/grok-martian/src/grok/util.py

-=-
Modified: grok/branches/grok-martian/src/grok/__init__.py
===================================================================
--- grok/branches/grok-martian/src/grok/__init__.py	2007-06-20 12:47:08 UTC (rev 76837)
+++ grok/branches/grok-martian/src/grok/__init__.py	2007-06-20 12:49:51 UTC (rev 76838)
@@ -40,7 +40,7 @@
                             define_permission, require, site)
 from grok._grok import do_grok as grok  # Avoid name clash within _grok
 from grok._grok import SubscribeDecorator as subscribe
-from grok.error import GrokError, GrokImportError
+from martian.error import GrokError, GrokImportError
 from grok.formlib import action, AutoFields, Fields
 from grok.util import url
 

Modified: grok/branches/grok-martian/src/grok/_grok.py
===================================================================
--- grok/branches/grok-martian/src/grok/_grok.py	2007-06-20 12:47:08 UTC (rev 76837)
+++ grok/branches/grok-martian/src/grok/_grok.py	2007-06-20 12:49:51 UTC (rev 76838)
@@ -23,16 +23,16 @@
 from zope.publisher.interfaces.browser import IBrowserRequest
 from zope.app.component.site import LocalSiteManager
 
+import martian
+from martian import scan
+from martian.error import GrokError, GrokImportError
+from martian.util import frame_is_module
+
 import grok
 
 from grok import util, components, meta
-from grok.error import GrokError, GrokImportError
-from grok.directive import frame_is_module
 from grok import templatereg
 
-import martian
-from martian import scan
-
 _bootstrapped = False
 def bootstrap():
     component.provideAdapter(components.ModelTraverser)

Modified: grok/branches/grok-martian/src/grok/directive.py
===================================================================
--- grok/branches/grok-martian/src/grok/directive.py	2007-06-20 12:47:08 UTC (rev 76837)
+++ grok/branches/grok-martian/src/grok/directive.py	2007-06-20 12:49:51 UTC (rev 76838)
@@ -14,191 +14,21 @@
 """Grok directives.
 """
 
-import sys
-import inspect
-from zope import interface, component
 from zope.interface.interfaces import IInterface
 
-import grok
+from martian.error import GrokImportError
+from martian.directive import (MultipleTimesDirective, BaseTextDirective,
+                               SingleValue, SingleTextDirective,
+                               MultipleTextDirective,
+                               MarkerDirective,
+                               InterfaceDirective,
+                               InterfaceOrClassDirective,
+                               ModuleDirectiveContext,
+                               ClassDirectiveContext,
+                               ClassOrModuleDirectiveContext)
+
 from grok import util
-from grok.error import GrokImportError
 
-
-def frame_is_module(frame):
-    return frame.f_locals is frame.f_globals
-
-def frame_is_class(frame):
-    return '__module__' in frame.f_locals    
-
-
-class IDirectiveContext(interface.Interface):
-    description = interface.Attribute("The correct place in which the "
-                                      "directive can be used.")
-
-    def matches(frame):
-        """returns whether the given frame is the correct place in
-        which the directive can be used.
-        """
-
-class ClassDirectiveContext(object):
-    interface.implements(IDirectiveContext)
-    
-    description = "class"
-
-    def matches(self, frame):
-        return frame_is_class(frame)
-
-    
-class ModuleDirectiveContext(object):
-    interface.implements(IDirectiveContext)
-    
-    description = "module"
-
-    def matches(self, frame):
-        return frame_is_module(frame)
-    
-
-class ClassOrModuleDirectiveContext(object):
-    interface.implements(IDirectiveContext)
-    
-    description = "class or module"
-
-    def matches(self, frame):
-        return frame_is_module(frame) or frame_is_class(frame)
-
-
-class Directive(object):
-    """
-    Directive sets a value into the context's locals as __<name>__
-    ('.' in the name are replaced with '_').
-    """
-
-    def __init__(self, name, directive_context):
-        self.name = name
-        self.local_name = '__%s__' % name.replace('.', '_')
-        self.directive_context = directive_context
-
-    def __call__(self, *args, **kw):
-        self.check_argument_signature(*args, **kw)
-        self.check_arguments(*args, **kw)
-
-        frame = sys._getframe(1)
-        self.check_directive_context(frame)
-
-        value = self.value_factory(*args, **kw)
-        return self.store(frame, value)
-
-    def check_arguments(self, *args, **kw):
-        raise NotImplementedError
-
-    # to get a correct error message, we construct a function that has
-    # the same signature as check_arguments(), but without "self".
-    def check_argument_signature(self, *arguments, **kw):
-        args, varargs, varkw, defaults = inspect.getargspec(
-            self.check_arguments)
-        argspec = inspect.formatargspec(args[1:], varargs, varkw, defaults)
-        exec("def signature_checker" + argspec + ": pass")
-        try:
-            signature_checker(*arguments, **kw)
-        except TypeError, e:
-            message = e.args[0]
-            message = message.replace("signature_checker()", self.name)
-            raise TypeError(message)
-
-    def check_directive_context(self, frame):
-        if not self.directive_context.matches(frame):
-            raise GrokImportError("%s can only be used on %s level."
-                                  % (self.name,
-                                     self.directive_context.description))
-
-    def value_factory(self, *args, **kw):
-        raise NotImplementedError
-
-    def store(self, frame, value):
-        raise NotImplementedError
-
-
-class OnceDirective(Directive):
-    def store(self, frame, value):
-        if self.local_name in frame.f_locals:
-            raise GrokImportError("%s can only be called once per %s."
-                                  % (self.name,
-                                     self.directive_context.description))
-        frame.f_locals[self.local_name] = value
-
-
-class MarkerDirective(OnceDirective):
-    """A directive without argument that places a marker.
-    """
-    def value_factory(self):
-        return True
-
-    def check_arguments(self):
-        pass
-
-class MultipleTimesDirective(Directive):
-    def store(self, frame, value):
-        values = frame.f_locals.get(self.local_name, [])
-        values.append(value)
-        frame.f_locals[self.local_name] = values
-
-
-class SingleValue(object):
-
-    # Even though the value_factory is called with (*args, **kw),
-    # we're safe since check_arguments would have bailed out with a
-    # TypeError if the number arguments we were called with was not
-    # what we expect here.
-    def value_factory(self, value):
-        return value
-
-
-class BaseTextDirective(object):
-    """
-    Base directive that only accepts unicode/ASCII values.
-    """
-
-    def check_arguments(self, value):
-        if util.not_unicode_or_ascii(value):
-            raise GrokImportError("You can only pass unicode or ASCII to "
-                                  "%s." % self.name)
-
-
-class SingleTextDirective(BaseTextDirective, SingleValue, OnceDirective):
-    """
-    Directive that accepts a single unicode/ASCII value, only once.
-    """
-
-
-class MultipleTextDirective(BaseTextDirective, SingleValue,
-                            MultipleTimesDirective):
-    """
-    Directive that accepts a single unicode/ASCII value, multiple times.
-    """
-
-
-class InterfaceOrClassDirective(SingleValue, OnceDirective):
-    """
-    Directive that only accepts classes or interface values.
-    """
-
-    def check_arguments(self, value):
-        if not (IInterface.providedBy(value) or util.isclass(value)):
-            raise GrokImportError("You can only pass classes or interfaces to "
-                                  "%s." % self.name)
-
-
-class InterfaceDirective(SingleValue, OnceDirective):
-    """
-    Directive that only accepts interface values.
-    """
-
-    def check_arguments(self, value):
-        if not (IInterface.providedBy(value)):
-            raise GrokImportError("You can only pass interfaces to "
-                                  "%s." % self.name)
-
-
 class GlobalUtilityDirective(MultipleTimesDirective):
     def check_arguments(self, factory, provides=None, name=u'',
                         direct=False):

Deleted: grok/branches/grok-martian/src/grok/error.py
===================================================================
--- grok/branches/grok-martian/src/grok/error.py	2007-06-20 12:47:08 UTC (rev 76837)
+++ grok/branches/grok-martian/src/grok/error.py	2007-06-20 12:49:51 UTC (rev 76838)
@@ -1,24 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2006 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Grok errors.
-"""
-
-class GrokError(Exception):
-
-    def __init__(self, message, component):
-        Exception.__init__(self, message)
-        self.component = component
-
-class GrokImportError(ImportError):
-    pass

Modified: grok/branches/grok-martian/src/grok/index.py
===================================================================
--- grok/branches/grok-martian/src/grok/index.py	2007-06-20 12:47:08 UTC (rev 76837)
+++ grok/branches/grok-martian/src/grok/index.py	2007-06-20 12:49:51 UTC (rev 76838)
@@ -6,8 +6,9 @@
 from zope.app.catalog.field import FieldIndex
 from zope.app.catalog.text import TextIndex
 
-from grok.error import GrokError, GrokImportError
-from grok.directive import frame_is_class
+from martian.error import GrokError, GrokImportError
+from martian.util import frame_is_class
+
 from grok.interfaces import IIndexDefinition
 
 class IndexDefinition(object):

Modified: grok/branches/grok-martian/src/grok/meta.py
===================================================================
--- grok/branches/grok-martian/src/grok/meta.py	2007-06-20 12:47:08 UTC (rev 76837)
+++ grok/branches/grok-martian/src/grok/meta.py	2007-06-20 12:49:51 UTC (rev 76838)
@@ -22,12 +22,12 @@
 
 from zope.exceptions.interfaces import DuplicationError
 
+import martian
+from martian.error import GrokError
+
 import grok
 from grok import util, components, formlib
-from grok.error import GrokError
 
-import martian
-
 class ModelGrokker(martian.ClassGrokker):
     component_class = grok.Model
 

Modified: grok/branches/grok-martian/src/grok/templatereg.py
===================================================================
--- grok/branches/grok-martian/src/grok/templatereg.py	2007-06-20 12:47:08 UTC (rev 76837)
+++ grok/branches/grok-martian/src/grok/templatereg.py	2007-06-20 12:49:51 UTC (rev 76838)
@@ -1,8 +1,8 @@
+from martian.error import GrokError
+
 import os
 import grok
-from grok.error import GrokError
 
-
 class TemplateRegistry(object):
 
     def __init__(self):

Modified: grok/branches/grok-martian/src/grok/util.py
===================================================================
--- grok/branches/grok-martian/src/grok/util.py	2007-06-20 12:47:08 UTC (rev 76837)
+++ grok/branches/grok-martian/src/grok/util.py	2007-06-20 12:49:51 UTC (rev 76838)
@@ -28,7 +28,7 @@
 from zope.security.checker import NamesChecker, defineChecker
 from zope.security.interfaces import IPermission
 
-from grok.error import GrokError, GrokImportError
+from martian.error import GrokError, GrokImportError
 
 from martian.util import (not_unicode_or_ascii, is_not_ascii,
                           isclass, check_subclass)



More information about the Checkins mailing list