[Checkins] SVN: grok/trunk/src/grok/ - made GrokError remember the component which caused the error while grokking

Christian Theune ct at gocept.com
Mon Oct 16 13:44:23 EDT 2006


Log message for revision 70723:
   - made GrokError remember the component which caused the error while grokking 
   - made templates that come from the filesystem report their correct filename with repr()
  

Changed:
  U   grok/trunk/src/grok/_grok.py
  U   grok/trunk/src/grok/error.py
  U   grok/trunk/src/grok/tests/error/error.py
  A   grok/trunk/src/grok/tests/error/filesystemtemplate/
  A   grok/trunk/src/grok/tests/error/filesystemtemplate/nocontext.pt
  A   grok/trunk/src/grok/tests/error/filesystemtemplate.py

-=-
Modified: grok/trunk/src/grok/_grok.py
===================================================================
--- grok/trunk/src/grok/_grok.py	2006-10-16 17:20:28 UTC (rev 70722)
+++ grok/trunk/src/grok/_grok.py	2006-10-16 17:44:22 UTC (rev 70723)
@@ -76,6 +76,10 @@
         # PageTemplate cannot be subclassed
         self.__grok_module__ = caller_module()
 
+    def __repr__(self):
+        return '<%s template in %s>' % (self.__grok_name__, self.__grok_location__)
+
+
 AMBIGUOUS_CONTEXT = object()
 def grok(dotted_name):
     # TODO for now we only grok modules
@@ -103,6 +107,8 @@
             views.append(obj)
         elif isinstance(obj, PageTemplate):
             templates.register(name, obj)
+            obj.__grok_name__ = name
+            obj.__grok_location__ = dotted_name
 
     # find filesystem resources
     module_name = dotted_name.split('.')[-1]
@@ -112,16 +118,23 @@
         for resource in resources:
             if not resource.endswith(".pt"):
                 continue
+            template_name = resource[:-3]
+            resource_path = os.path.join(directory_name, resource)
 
-            contents = resource_string(dotted_name,
-                                       os.path.join(directory_name, resource))
+            contents = resource_string(dotted_name, resource_path)
             template = PageTemplate(contents)
-            template_name = resource[:-3]
-            if templates.get(template_name):
+            template.__grok_name__ = template_name
+            # XXX is this zip-safe?
+            template.__grok_location__ = os.path.join(
+                os.path.dirname(module.__file__), resource_path)
+
+            inline_template = templates.get(template_name)
+            if inline_template:
                 raise GrokError("Conflicting templates found for name '%s' "
                                 "in module %r, both inline and in resource "
                                 "directory '%s'."
-                                % (template_name, module, directory_name))
+                                % (template_name, module, directory_name),
+                                inline_template)
             templates.register(template_name, template)
 
     if len(models) == 0:
@@ -164,20 +177,23 @@
                 raise GrokError("Multiple possible templates for view %r. It "
                                 "uses grok.template('%s'), but there is also "
                                 "a template called '%s'."
-                                % (factory, template_name, factory_name))
+                                % (factory, template_name, factory_name),
+                                factory)
 
         if template:
             if getattr(factory, 'render', None):
                 raise GrokError("Multiple possible ways to render view %r. "
                                 "It has both a 'render' method as well as "
-                                "an associated template." % factory)
+                                "an associated template." % factory,
+                                factory)
 
             templates.markAssociated(template_name)
             factory.template = template
         else:
             if not getattr(factory, 'render', None):
                 raise GrokError("View %r has no associated template or "
-                                "'render' method." % factory)
+                                "'render' method." % factory,
+                                factory)
 
         view_name = directive_annotation(factory, 'grok.name', factory_name)
         component.provideAdapter(factory,
@@ -189,8 +205,7 @@
         defineChecker(factory, NoProxy)
 
     for name, unassociated in templates.listUnassociatedTemplates():
-        source = '<%s template in %s>' % (name, dotted_name)
-        check_context(source, context)
+        check_context(unassociated, context)
 
         class TemplateView(View):
             template = unassociated
@@ -233,17 +248,17 @@
         obj_module = getattr(obj, '__module__', None)
     return obj_module == dotted_name
 
-def check_context(source, context):
+def check_context(component, context):
     if context is None:
-        raise GrokError("No module-level context for %s, please use "
-                        "grok.context." % source)
+        raise GrokError("No module-level context for %r, please use "
+                        "grok.context." % component, component)
     elif context is AMBIGUOUS_CONTEXT:
-        raise GrokError("Multiple possible contexts for %s, please use "
-                        "grok.context." % source)
+        raise GrokError("Multiple possible contexts for %r, please use "
+                        "grok.context." % component, component)
 
 def determine_context(factory, module_context):
     context = directive_annotation(factory, 'grok.context', module_context)
-    check_context(repr(factory), context)
+    check_context(factory, context)
     return context
 
 def directive_annotation(obj, name, default):

Modified: grok/trunk/src/grok/error.py
===================================================================
--- grok/trunk/src/grok/error.py	2006-10-16 17:20:28 UTC (rev 70722)
+++ grok/trunk/src/grok/error.py	2006-10-16 17:44:22 UTC (rev 70723)
@@ -15,7 +15,10 @@
 """
 
 class GrokError(Exception):
-    pass
 
+    def __init__(self, message, component):
+        Exception.__init__(self, message)
+        self.component = component
+
 class GrokImportError(ImportError):
     pass

Modified: grok/trunk/src/grok/tests/error/error.py
===================================================================
--- grok/trunk/src/grok/tests/error/error.py	2006-10-16 17:20:28 UTC (rev 70722)
+++ grok/trunk/src/grok/tests/error/error.py	2006-10-16 17:44:22 UTC (rev 70723)
@@ -4,8 +4,10 @@
 
   >>> try:
   ...     grok.grok(__name__)
-  ... except grok.GrokError:
+  ... except grok.GrokError, error:
   ...     pass
+  >>> error.component
+  <class 'grok.tests.error.error.CavePainting'>
 
 """
 

Added: grok/trunk/src/grok/tests/error/filesystemtemplate/nocontext.pt
===================================================================
--- grok/trunk/src/grok/tests/error/filesystemtemplate/nocontext.pt	2006-10-16 17:20:28 UTC (rev 70722)
+++ grok/trunk/src/grok/tests/error/filesystemtemplate/nocontext.pt	2006-10-16 17:44:22 UTC (rev 70723)
@@ -0,0 +1 @@
+This template has no context.


Property changes on: grok/trunk/src/grok/tests/error/filesystemtemplate/nocontext.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: grok/trunk/src/grok/tests/error/filesystemtemplate.py
===================================================================
--- grok/trunk/src/grok/tests/error/filesystemtemplate.py	2006-10-16 17:20:28 UTC (rev 70722)
+++ grok/trunk/src/grok/tests/error/filesystemtemplate.py	2006-10-16 17:44:22 UTC (rev 70723)
@@ -0,0 +1,11 @@
+"""
+
+This test checks for the right name in the error message if a filesystem-based
+template can not find a context to be associated with:
+
+  >>> grok.grok(__name__)
+  Traceback (most recent call last):
+  GrokError: No module-level context for <nocontext template in ...grok/tests/error/filesystemtemplate/nocontext.pt>, please use grok.context.
+
+"""
+import grok


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



More information about the Checkins mailing list