[Zope3-checkins] CVS: Zope3/src/zope/configuration - xmlconfig.py:1.9

Fred L. Drake, Jr. fred@zope.com
Mon, 23 Jun 2003 10:44:44 -0400


Update of /cvs-repository/Zope3/src/zope/configuration
In directory cvs.zope.org:/tmp/cvs-serv23423

Modified Files:
	xmlconfig.py 
Log Message:
When loading a file, if it does not exist but a file with the same name and
a .in suffix does, load the .in version instead of raising an error.
Added tests.


=== Zope3/src/zope/configuration/xmlconfig.py 1.8 => 1.9 ===
--- Zope3/src/zope/configuration/xmlconfig.py:1.8	Sun Jun 22 15:00:58 2003
+++ Zope3/src/zope/configuration/xmlconfig.py	Mon Jun 23 10:44:13 2003
@@ -15,6 +15,7 @@
 $Id$
 """
 
+import errno
 import os
 import sys
 import logging
@@ -72,7 +73,7 @@
             return 'File "%s", line %s.%s, %s' % s
         else:
             return str(v)
-        
+
 class ConfigurationExecutionError(ZopeXMLConfigurationError):
     """An error occurred during execution of a configuration action
     """
@@ -297,6 +298,26 @@
         return "\n".join(r)
 
 
+def inopen(filename):
+    # XXX I don't really like the name of this function
+    """Open a file, falling back to filename.in.
+
+    If the requested file does not exist and filename.in does, fall
+    back to filename.in.  If opening the original filename fails for
+    any other reason, allow the failure to propogate.
+    """
+    try:
+        fp = open(filename)
+    except IOError, (code, msg):
+        if code == errno.ENOENT:
+            fn = filename + ".in"
+            if os.path.exists(fn):
+                fp = open(fn)
+            else:
+                raise
+    return fp
+
+
 class XMLConfig:
 
     def __init__(self, file_name, module=_NO_MODULE_GIVEN):
@@ -308,7 +329,7 @@
         self._directives = {('*', 'include'):
                             (self.include, {})}
 
-        f = open(file_name)
+        f = inopen(file_name)
         self._stack = [file_name]
         xmlconfig(f, self._actions,
                   Context(self._stack, module=module),
@@ -359,7 +380,7 @@
 
     def _include(self, file_name, package):
         logger.debug("include %s" % file_name)
-        f = open(file_name)
+        f = inopen(file_name)
         self._stack.append(file_name)
         xmlconfig(f, self._actions, Context(self._stack, package),
                   self._directives)