[Zope-Checkins] CVS: Zope/lib/python/Controller - MetaDirective.py:1.1.2.4

Chris McDonough chrism@zope.com
Wed, 9 Oct 2002 01:05:29 -0400


Update of /cvs-repository/Zope/lib/python/Controller
In directory cvs.zope.org:/tmp/cvs-serv12210

Modified Files:
      Tag: chrism-install-branch
	MetaDirective.py 
Log Message:
Add interpolation to config value parsing.

Add optional context argument to Directive.set to support interpolation.


=== Zope/lib/python/Controller/MetaDirective.py 1.1.2.3 => 1.1.2.4 ===
--- Zope/lib/python/Controller/MetaDirective.py:1.1.2.3	Tue Sep 10 00:15:59 2002
+++ Zope/lib/python/Controller/MetaDirective.py	Wed Oct  9 01:05:29 2002
@@ -16,7 +16,20 @@
 __version__='$Revision$'[11:-2]
 
 _marker = []
-import os
+import os, re
+
+interpolate_split = re.compile(r'.*?\$\{(.*?)\}.*?').split
+
+def interpolate(val, context=None):
+    match = interpolate_split(val)
+    if len(match) == 1:
+        return val
+    replace = context.get(match[1], _marker)
+    if replace is _marker:
+        raise ValueError("Cannot interpolate %s, can't find a value for"
+                         "%s in the configuration" % (val, match[1]))
+    interpolated = '%s%s%s' % (match[0], replace, match[2])
+    return interpolated
 
 def getParsedValue(type, s):
     if s is None:
@@ -36,7 +49,9 @@
     def getInfluences(self):
         return self.influences
 
-    def set(self, value):
+    def set(self, value, context=None):
+        if context and value.find('${') != -1:
+            value = interpolate(value, context)
         self.value = getParsedValue(self.valtype, value)
 
 class EnvironmentDirective(Directive):