[Zope3-checkins] CVS: Packages/ZConfig - ApacheStyle.py:1.7 Context.py:1.14

Fred L. Drake, Jr. fred@zope.com
Wed, 4 Dec 2002 16:21:14 -0500


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

Modified Files:
	ApacheStyle.py Context.py 
Log Message:
Implement the %define directive and $-substitution within values.
Further documentation is needed.


=== Packages/ZConfig/ApacheStyle.py 1.6 => 1.7 ===
--- Packages/ZConfig/ApacheStyle.py:1.6	Tue Dec  3 12:58:00 2002
+++ Packages/ZConfig/ApacheStyle.py	Wed Dec  4 16:20:43 2002
@@ -20,6 +20,7 @@
         if line[0] == "#":
             # comment
             continue
+
         if line[:2] == "</":
             # section end
             if line[-1] != ">":
@@ -38,6 +39,7 @@
                 raise ConfigurationSyntaxError(e[0], resource.url, lineno)
             section = stack.pop()
             continue
+
         if line[0] == "<":
             # section start
             if line[-1] != ">":
@@ -63,6 +65,7 @@
                 stack.append(section)
                 section = newsect
             continue
+
         if line[0] == "%":
             # directive: import, include
             m = _keyvalue_rx.match(line, 1)
@@ -70,7 +73,7 @@
                 raise ConfigurationSyntaxError(
                     "missing or unrecognized directive", resource.url, lineno)
             name, arg = m.group('key', 'value')
-            if name not in ("import", "include"):
+            if name not in ("define", "import", "include"):
                 raise ConfigurationSyntaxError(
                     "unknown directive: " + `name`, resource.url, lineno)
             if not arg:
@@ -87,9 +90,20 @@
             elif name == "include":
                 newurl = urlparse.urljoin(resource.url, arg)
                 context.includeConfiguration(section, newurl)
+            elif name == "define":
+                parts = arg.split(None, 1)
+                defname = parts[0]
+                defvalue = ''
+                if len(parts) == 2:
+                    defvalue = parts[1]
+                try:
+                    resource.define(defname, defvalue)
+                except ConfigurationError, e:
+                    raise ConfigurationSyntaxError(e[0], resource.url, lineno)
             else:
                 assert 0, "unexpected directive for " + `line`
             continue
+
         # key-value
         m = _keyvalue_rx.match(line)
         if not m:
@@ -98,10 +112,13 @@
         key, value = m.group('key', 'value')
         if not value:
             value = ''
+        else:
+            value = resource.substitute(value)
         try:
             section.addValue(key, value)
         except ConfigurationError, e:
             raise ConfigurationSyntaxError(e[0], resource.url, lineno)
+
     if stack:
         raise ConfigurationSyntaxError(
             "unclosed sections no allowed", resource.url, lineno + 1)


=== Packages/ZConfig/Context.py 1.13 => 1.14 ===
--- Packages/ZConfig/Context.py:1.13	Wed Dec  4 12:28:59 2002
+++ Packages/ZConfig/Context.py	Wed Dec  4 16:20:43 2002
@@ -7,6 +7,7 @@
 
 from Common import *
 from Config import Configuration, ImportingConfiguration
+from Substitution import isname, substitute
 
 
 class Context:
@@ -199,3 +200,18 @@
     def __init__(self, file, url):
         self.file = file
         self.url = url
+        self._definitions = {}
+
+    def define(self, name, value):
+        key = name.lower()
+        if self._definitions.has_key(key):
+            raise ConfigurationError("cannot redefine " + `name`)
+        if not isname(name):
+            raise ConfigurationError(
+                "not a substitution legal name: " + `name`)
+        self._definitions[key] = value
+
+    def substitute(self, s):
+        # XXX  I don't really like calling this substitute(),
+        # XXX  but it will do for now.
+        return substitute(s, self._definitions)