[Zope3-checkins] CVS: Packages/ZConfig - ApacheStyle.py:1.5 Context.py:1.12

Fred L. Drake, Jr. fred@zope.com
Tue, 3 Dec 2002 10:36:54 -0500


Update of /cvs-repository/Packages/ZConfig
In directory cvs.zope.org:/tmp/cvs-serv9411

Modified Files:
	ApacheStyle.py Context.py 
Log Message:
Pure refactoring:  Move information about a configuration source to a
separate object.


=== Packages/ZConfig/ApacheStyle.py 1.4 => 1.5 ===
--- Packages/ZConfig/ApacheStyle.py:1.4	Sun Nov 24 01:04:35 2002
+++ Packages/ZConfig/ApacheStyle.py	Tue Dec  3 10:36:53 2002
@@ -5,11 +5,11 @@
 from Common import *
 
 
-def Parse(file, context, section, url):
+def Parse(resource, context, section):
     lineno = 0
     stack = []
     while 1:
-        line = file.readline()
+        line = resource.file.readline()
         if not line:
             break
         lineno += 1
@@ -24,25 +24,25 @@
             # section end
             if line[-1] != ">":
                 raise ConfigurationSyntaxError(
-                    "malformed section end", url, lineno)
+                    "malformed section end", resource.url, lineno)
             if not stack:
                 raise ConfigurationSyntaxError(
-                    "unexpected section end", url, lineno)
+                    "unexpected section end", resource.url, lineno)
             type = line[2:-1].rstrip()
             if type.lower() != section.type:
                 raise ConfigurationSyntaxError(
-                    "unbalanced section end", url, lineno)
+                    "unbalanced section end", resource.url, lineno)
             try:
                 section.finish()
             except ConfigurationError, e:
-                raise ConfigurationSyntaxError(e[0], url, lineno)
+                raise ConfigurationSyntaxError(e[0], resource.url, lineno)
             section = stack.pop()
             continue
         if line[0] == "<":
             # section start
             if line[-1] != ">":
                 raise ConfigurationSyntaxError(
-                    "malformed section start", url, lineno)
+                    "malformed section start", resource.url, lineno)
             isempty = line[-2] == "/"
             if isempty:
                 text = line[1:-2].rstrip()
@@ -52,13 +52,13 @@
             m = _section_start_rx.match(text)
             if not m:
                 raise ConfigurationSyntaxError(
-                    "malformed section header", url, lineno)
+                    "malformed section header", resource.url, lineno)
             type, name, delegatename = m.group('type', 'name', 'delegatename')
             try:
                 newsect = context.nestSection(section, type, name,
                                               delegatename)
             except ConfigurationError, e:
-                raise ConfigurationSyntaxError(e[0], url, lineno)
+                raise ConfigurationSyntaxError(e[0], resource.url, lineno)
             if not isempty:
                 stack.append(section)
                 section = newsect
@@ -67,17 +67,17 @@
         m = _keyvalue_rx.match(line)
         if not m:
             raise ConfigurationSyntaxError(
-                "malformed configuration data", url, lineno)
+                "malformed configuration data", resource.url, lineno)
         key, value = m.group('key', 'value')
         if key == "import":
             if stack:
                 raise ConfigurationSyntaxError(
                     "import only allowed at the outermost level of a resource",
-                    url, lineno)
-            newurl = urlparse.urljoin(url, value)
+                    resource.url, lineno)
+            newurl = urlparse.urljoin(resource.url, value)
             context.importConfiguration(section, newurl)
         elif key == "include":
-            newurl = urlparse.urljoin(section.url, value)
+            newurl = urlparse.urljoin(resource.url, value)
             context.includeConfiguration(section, newurl)
         else:
             if not value:
@@ -85,10 +85,10 @@
             try:
                 section.addValue(key, value)
             except ConfigurationError, e:
-                raise ConfigurationSyntaxError(e[0], url, lineno)
+                raise ConfigurationSyntaxError(e[0], resource.url, lineno)
     if stack:
         raise ConfigurationSyntaxError(
-            "unclosed sections no allowed", url, lineno + 1)
+            "unclosed sections no allowed", resource.url, lineno + 1)
 
 
 import re


=== Packages/ZConfig/Context.py 1.11 => 1.12 ===
--- Packages/ZConfig/Context.py:1.11	Sat Nov 23 01:37:38 2002
+++ Packages/ZConfig/Context.py	Tue Dec  3 10:36:53 2002
@@ -12,7 +12,6 @@
 class Context:
 
     def __init__(self):
-        #Configuration.__init__(self, None, None, url)
         self._imports = []         # URL  -> Configuration
         self._named_sections = {}  # name -> Configuration
         self._needed_names = {}    # name -> [needy Configuration, ...]
@@ -32,14 +31,17 @@
     def createToplevelSection(self, url):
         return ImportingConfiguration(url)
 
+    def createResource(self, file, url):
+        return Resource(file, url)
+
     def getDelegateType(self, type):
         # Applications must provide delegation typing information by
         # overriding the Context.getDelegateType() method.
         return type.lower()
 
-    def parse(self, file, section, url):
+    def parse(self, resource, section):
         from ApacheStyle import Parse
-        Parse(file, self, section, url)
+        Parse(resource, self, section)
 
     def _normalize_url(self, url):
         if os.path.exists(url):
@@ -72,8 +74,9 @@
         self._all_sections.append(top)
         self._imports = [top]
         self._current_imports.append(top)
+        r = Resource(file, url)
         try:
-            self.parse(file, top, url)
+            self.parse(r, top)
         finally:
             del self._current_imports[-1]
         self._finish()
@@ -95,8 +98,9 @@
     def includeConfiguration(self, section, url):
         # XXX we always re-parse, unlike import
         file = urllib2.urlopen(url)
+        r = Resource(file, url)
         try:
-            self.parse(file, section, url)
+            self.parse(r, section)
         finally:
             file.close()
 
@@ -147,8 +151,9 @@
                 "fragment identifiers are not currently supported")
         file = urllib2.urlopen(url)
         self._current_imports.append(section)
+        r = Resource(file, url)
         try:
-            self.parse(file, section, url)
+            self.parse(r, section)
         finally:
             del self._current_imports[-1]
             file.close()
@@ -188,3 +193,9 @@
             if sect.delegate is not None:
                 sect.finish()
         self._all_sections = None
+
+
+class Resource:
+    def __init__(self, file, url):
+        self.file = file
+        self.url = url