[Checkins] SVN: grok/trunk/src/grok/ - distinguish between GrokError and GrokImportError

Christian Theune ct at gocept.com
Mon Oct 16 12:48:11 EDT 2006


Log message for revision 70721:
   - distinguish between GrokError and GrokImportError
   - preparation for better error reporting during grok()-time
  

Changed:
  U   grok/trunk/src/grok/__init__.py
  U   grok/trunk/src/grok/_grok.py
  U   grok/trunk/src/grok/directive.py
  U   grok/trunk/src/grok/error.py
  U   grok/trunk/src/grok/tests/adapter/classcontextmultiple.py
  U   grok/trunk/src/grok/tests/adapter/classorinterface.py
  U   grok/trunk/src/grok/tests/adapter/functioncontext.py
  U   grok/trunk/src/grok/tests/adapter/modulecontextmultiple.py
  A   grok/trunk/src/grok/tests/error/
  A   grok/trunk/src/grok/tests/error/__init__.py
  A   grok/trunk/src/grok/tests/error/error.py
  U   grok/trunk/src/grok/tests/test_grok.py
  U   grok/trunk/src/grok/tests/view/inlinebogus.py
  U   grok/trunk/src/grok/tests/view/namemultiple.py
  U   grok/trunk/src/grok/tests/view/nameunicode.py
  U   grok/trunk/src/grok/tests/view/nomodulename.py

-=-
Modified: grok/trunk/src/grok/__init__.py
===================================================================
--- grok/trunk/src/grok/__init__.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/__init__.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -16,5 +16,7 @@
 
 from zope.interface import implements
 from zope.component import adapts
-from _grok import (Model, Adapter, MultiAdapter, View, PageTemplate,
-                   grok, context, name, template, resources)
+
+from grok._grok import (Model, Adapter, MultiAdapter, View, PageTemplate,
+                   grok, context, name, template, resources, )
+from grok.error import GrokError, GrokImportError

Modified: grok/trunk/src/grok/_grok.py
===================================================================
--- grok/trunk/src/grok/_grok.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/_grok.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -27,7 +27,7 @@
 from zope.app.pagetemplate.engine import TrustedAppPT
 
 from grok import util
-from grok.error import GrokError
+from grok.error import GrokError, GrokImportError
 from grok.directive import (ClassDirectiveContext, ModuleDirectiveContext,
                             ClassOrModuleDirectiveContext,
                             TextDirective, InterfaceOrClassDirective)
@@ -68,8 +68,8 @@
     def __init__(self, template):
         super(PageTemplate, self).__init__()
         if util.not_unicode_or_ascii(template):
-            raise GrokError("Invalid page template. Page templates must be "
-                            "unicode or ASCII.")
+            raise ValueError("Invalid page template. Page templates must be "
+                             "unicode or ASCII.")
         self.write(template)
 
         # XXX unfortunately using caller_module means that

Modified: grok/trunk/src/grok/directive.py
===================================================================
--- grok/trunk/src/grok/directive.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/directive.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -19,7 +19,7 @@
 from zope.interface.interfaces import IInterface
 
 from grok import util
-from grok.error import GrokError
+from grok.error import GrokImportError
 
 
 def frame_is_module(frame):
@@ -74,16 +74,18 @@
 
     def __call__(self, value):
         self.check(value)
-        
+
         frame = sys._getframe(1)
         if not self.directive_context.matches(frame):
-            raise GrokError("%s can only be used on %s level."
-                            % (self.name, self.directive_context.description))
-        
+            raise GrokImportError("%s can only be used on %s level."
+                                  % (self.name,
+                                     self.directive_context.description))
+
         local_name = '__%s__' % self.name.replace('.', '_')
         if local_name in frame.f_locals:
-            raise GrokError("%s can only be called once per %s."
-                            % (self.name, self.directive_context.description))
+            raise GrokImportError("%s can only be called once per %s."
+                                  % (self.name,
+                                     self.directive_context.description))
         frame.f_locals[local_name] = value
 
     def check(self, value):
@@ -96,16 +98,15 @@
 
     def check(self, value):
         if util.not_unicode_or_ascii(value):
-            raise GrokError("You can only pass unicode or ASCII to "
-                            "%s." % self.name)
+            raise GrokImportError("You can only pass unicode or ASCII to "
+                                  "%s." % self.name)
 
 class InterfaceOrClassDirective(Directive):
     """
     Directive that only accepts classes or interface values.
     """
-    
+
     def check(self, value):
         if not (IInterface.providedBy(value) or util.isclass(value)):
-            raise GrokError("You can only pass classes or interfaces to "
-                            "%s." % self.name)
-    
+            raise GrokImportError("You can only pass classes or interfaces to "
+                                  "%s." % self.name)

Modified: grok/trunk/src/grok/error.py
===================================================================
--- grok/trunk/src/grok/error.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/error.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -17,3 +17,5 @@
 class GrokError(Exception):
     pass
 
+class GrokImportError(ImportError):
+    pass

Modified: grok/trunk/src/grok/tests/adapter/classcontextmultiple.py
===================================================================
--- grok/trunk/src/grok/tests/adapter/classcontextmultiple.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/adapter/classcontextmultiple.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -4,7 +4,7 @@
   >>> import grok.tests.adapter.classcontextmultiple_fixture
   Traceback (most recent call last):
     ...
-  GrokError: grok.context can only be called once per class or module.
+  GrokImportError: grok.context can only be called once per class or module.
 
 """
 

Modified: grok/trunk/src/grok/tests/adapter/classorinterface.py
===================================================================
--- grok/trunk/src/grok/tests/adapter/classorinterface.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/adapter/classorinterface.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -5,22 +5,22 @@
   >>> function_context()
   Traceback (most recent call last):
     ...
-  GrokError: You can only pass classes or interfaces to grok.context.
+  GrokImportError: You can only pass classes or interfaces to grok.context.
 
   >>> string_context()
   Traceback (most recent call last):
     ...
-  GrokError: You can only pass classes or interfaces to grok.context.
+  GrokImportError: You can only pass classes or interfaces to grok.context.
 
   >>> module_context()
   Traceback (most recent call last):
     ...
-  GrokError: You can only pass classes or interfaces to grok.context.
+  GrokImportError: You can only pass classes or interfaces to grok.context.
 
   >>> instance_context()
   Traceback (most recent call last):
     ...
-  GrokError: You can only pass classes or interfaces to grok.context.
+  GrokImportError: You can only pass classes or interfaces to grok.context.
 
 """
 import grok

Modified: grok/trunk/src/grok/tests/adapter/functioncontext.py
===================================================================
--- grok/trunk/src/grok/tests/adapter/functioncontext.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/adapter/functioncontext.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -4,14 +4,14 @@
   >>> func()
   Traceback (most recent call last):
     ...
-  GrokError: grok.context can only be used on class or module level.
+  GrokImportError: grok.context can only be used on class or module level.
 
 You can't call grok.context from a method either:
 
   >>> SomeClass().meth()
   Traceback (most recent call last):
     ...
-  GrokError: grok.context can only be used on class or module level.
+  GrokImportError: grok.context can only be used on class or module level.
 
 """
 import grok

Modified: grok/trunk/src/grok/tests/adapter/modulecontextmultiple.py
===================================================================
--- grok/trunk/src/grok/tests/adapter/modulecontextmultiple.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/adapter/modulecontextmultiple.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -4,6 +4,6 @@
   >>> import grok.tests.adapter.modulecontextmultiple_fixture
   Traceback (most recent call last):
     ...
-  GrokError: grok.context can only be called once per class or module.
+  GrokImportError: grok.context can only be called once per class or module.
 
 """

Added: grok/trunk/src/grok/tests/error/__init__.py
===================================================================
--- grok/trunk/src/grok/tests/error/__init__.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/error/__init__.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -0,0 +1 @@
+# this is a package


Property changes on: grok/trunk/src/grok/tests/error/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id Rev Date
Name: svn:eol-style
   + native

Added: grok/trunk/src/grok/tests/error/error.py
===================================================================
--- grok/trunk/src/grok/tests/error/error.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/error/error.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -0,0 +1,21 @@
+"""
+
+We expect this grok to fail, and give 
+
+  >>> try:
+  ...     grok.grok(__name__)
+  ... except grok.GrokError:
+  ...     pass
+
+"""
+
+import grok
+
+class Mammoth(grok.Model):
+    pass
+
+class CavePainting(grok.View):
+    grok.template("a")
+
+a = grok.PageTemplate("a")
+cavepainting = grok.PageTemplate("b")


Property changes on: grok/trunk/src/grok/tests/error/error.py
___________________________________________________________________
Name: svn:keywords
   + Id Rev Date
Name: svn:eol-style
   + native

Modified: grok/trunk/src/grok/tests/test_grok.py
===================================================================
--- grok/trunk/src/grok/tests/test_grok.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/test_grok.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -26,7 +26,7 @@
 
 def test_suite():
     suite = unittest.TestSuite()
-    for name in ['adapter', 'view', 'security']:
+    for name in ['adapter', 'error', 'view', 'security']:
         suite.addTest(suiteFromPackage(name))
     return suite
 

Modified: grok/trunk/src/grok/tests/view/inlinebogus.py
===================================================================
--- grok/trunk/src/grok/tests/view/inlinebogus.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/view/inlinebogus.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -12,5 +12,5 @@
   ... </html>''')
   Traceback (most recent call last):
     ...
-  GrokError: Invalid page template. Page templates must be unicode or ASCII.
+  ValueError: Invalid page template. Page templates must be unicode or ASCII.
 """

Modified: grok/trunk/src/grok/tests/view/namemultiple.py
===================================================================
--- grok/trunk/src/grok/tests/view/namemultiple.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/view/namemultiple.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -4,6 +4,6 @@
   >>> import grok.tests.view.namemultiple_fixture
   Traceback (most recent call last):
     ...
-  GrokError: grok.name can only be called once per class.
+  GrokImportError: grok.name can only be called once per class.
 
 """

Modified: grok/trunk/src/grok/tests/view/nameunicode.py
===================================================================
--- grok/trunk/src/grok/tests/view/nameunicode.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/view/nameunicode.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -6,11 +6,11 @@
   >>> pass_encodedstring()
   Traceback (most recent call last):
     ...
-  GrokError: You can only pass unicode or ASCII to grok.name.
+  GrokImportError: You can only pass unicode or ASCII to grok.name.
   >>> pass_object()
   Traceback (most recent call last):
     ...
-  GrokError: You can only pass unicode or ASCII to grok.name.
+  GrokImportError: You can only pass unicode or ASCII to grok.name.
 
 """
 import grok

Modified: grok/trunk/src/grok/tests/view/nomodulename.py
===================================================================
--- grok/trunk/src/grok/tests/view/nomodulename.py	2006-10-16 16:47:14 UTC (rev 70720)
+++ grok/trunk/src/grok/tests/view/nomodulename.py	2006-10-16 16:48:10 UTC (rev 70721)
@@ -4,6 +4,6 @@
   >>> import grok.tests.view.nomodulename_fixture
   Traceback (most recent call last):
     ...
-  GrokError: grok.name can only be used on class level.
+  GrokImportError: grok.name can only be used on class level.
 
 """



More information about the Checkins mailing list