[Zope-Checkins] CVS: Zope3/lib/python/Zope/Configuration - __init__.py:1.1.2.7 xmlconfig.py:1.1.2.13

Casey Duncan casey@zope.com
Wed, 3 Apr 2002 15:44:11 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Configuration
In directory cvs.zope.org:/tmp/cvs-serv365/lib/python/Zope/Configuration

Modified Files:
      Tag: Zope-3x-branch
	__init__.py xmlconfig.py 
Log Message:
Zope configuration now uses Shane's nifty traceback supplements to sprinkle
helpful debug info into exceptions that occur during configuration processing,
rather than masking them with a single generic configuration exception. 

Exception formatting is now turned on during testing, and when config is run
at startup.


=== Zope3/lib/python/Zope/Configuration/__init__.py 1.1.2.6 => 1.1.2.7 ===
     return 'http://namespaces.zope.org/'+suffix
 
-import os
+import sys, os
 from Zope.Configuration.xmlconfig import XMLConfig
 
 def config(dir):
-    XMLConfig(os.path.join(dir, 'site.zcml'))()
+    try:
+        XMLConfig(os.path.join(dir, 'site.zcml'))()
+    except:
+        # Use the ExceptionFormatter to provide XMLconfig debug info
+        from Zope.Exceptions.ExceptionFormatter import format_exception
+        exc_info = ['='*80, '\nZope Configuration Error\n', '='*80, '\n'] \
+                   + apply(format_exception, sys.exc_info())
+        sys.stderr.write(''.join(exc_info))
+        sys.exit(0) # Fatal config error
 
 __all__ = ["namespace", "config"]


=== Zope3/lib/python/Zope/Configuration/xmlconfig.py 1.1.2.12 => 1.1.2.13 ===
                 if iskeyword(aname): aname += '_'
                 kw[aname]=value
-
+                
         if len(stack) == 1:
-            try:                
-                stack.append(begin(self.__directives, name, self.__context,
-                                   **kw))
+            try:
+                stack.append(begin(self.__directives, name, self.__context, **kw))
             except Exception, v:
-                raise ZopeXMLConfigurationError, (
-                    self.__locator, v), sys.exc_info()[2] 
-                
+                __traceback_supplement__ = (
+                    ConfigurationTracebackSupplement, self.__locator, v
+                    )
+                raise
         else:
             subs = self.__stack[-1]
             if subs is None:
@@ -102,9 +102,11 @@
             try:
                 stack.append(sub(subs, name, self.__context, **kw))
             except Exception, v:
-                raise ZopeXMLConfigurationError, (
-                    self.__locator, v), sys.exc_info()[2] 
-
+                __traceback_supplement__ = (
+                    ConfigurationTracebackSupplement, self.__locator, v
+                    )
+                raise
+                
     def endElementNS(self, name, qname):
         subs = self.__stack.pop()
         # to fool compiler that thinks actions is used before assignment:
@@ -114,8 +116,10 @@
             try:
                 actions = end(subs)
             except Exception, v:
-                raise ZopeXMLConfigurationError, (
-                    self.__locator, str(v)), sys.exc_info()[2] 
+                __traceback_supplement__ = (
+                    ConfigurationTracebackSupplement, self.__locator, v
+                    )
+                raise
 
         append = self.__actions.append
 
@@ -175,7 +179,7 @@
     parser.setContentHandler(ConfigurationHandler(actions, context,
                                                   directives))
     parser.setFeature(feature_namespaces, 1)
-    parser.parse(src)
+    parser.parse(src) 
 
     if call:
         descriptors = {}
@@ -285,3 +289,22 @@
                 callable(*args, **kw)
             except Exception, v:
                 raise ConfigurationExecutionError(loc, v)
+                
+                
+class ConfigurationTracebackSupplement:
+    """Implementation of Zope.Exceptions.ITracebackSupplement"""
+    def __init__(self, locator, message):
+        self.message = message
+        self.line = locator.getLineNumber()
+        self.column = locator.getColumnNumber()
+        self.source_url  = locator.getSystemId()
+        
+    def getInfo(self, as_html=0):
+        
+        if not as_html:
+            return '   - Message: %s' % self.message
+        else:
+            from cgi import escape
+            return '<b>Message:</b><pre>%s</pre> in %s' % escape(self.message)
+        return None
+