Author: ianb
Date: 2005-03-30 17:03:13 -0700 (Wed, 30 Mar 2005)
New Revision: 2263
Added:
WSGIKit/trunk/wsgikit/tests/pyconfig_data/deriv.conf
Modified:
WSGIKit/trunk/wsgikit/pyconfig.py
WSGIKit/trunk/wsgikit/tests/test_pyconfig.py
Log:
Allow configuration files to use and update previously-defined
configuration values.
Modified: WSGIKit/trunk/wsgikit/pyconfig.py
===================================================================
--- WSGIKit/trunk/wsgikit/pyconfig.py 2005-03-30 23:25:41 UTC (rev 2262)
+++ WSGIKit/trunk/wsgikit/pyconfig.py 2005-03-31 00:03:13 UTC (rev 2263)
@@ -60,14 +60,19 @@
content = f.read()
f.close()
namespace = {}
+ for key in self:
+ namespace[key] = self[key]
+ orig = namespace.copy()
exec content in namespace
for name in namespace.keys():
if (hasattr(__builtins__, name)
or name.startswith('_')):
del namespace[name]
+ if orig.has_key(name) and namespace[name] is orig[name]:
+ del namespace[name]
self.load_dict(namespace, default)
- def load_dict(self, d, default):
+ def load_dict(self, d, default=False):
if default:
self.namespaces.append(d)
else:
Added: WSGIKit/trunk/wsgikit/tests/pyconfig_data/deriv.conf
===================================================================
--- WSGIKit/trunk/wsgikit/tests/pyconfig_data/deriv.conf 2005-03-30 23:25:41 UTC (rev 2262)
+++ WSGIKit/trunk/wsgikit/tests/pyconfig_data/deriv.conf 2005-03-31 00:03:13 UTC (rev 2263)
@@ -0,0 +1 @@
+test1 = test1 + '+another'
Modified: WSGIKit/trunk/wsgikit/tests/test_pyconfig.py
===================================================================
--- WSGIKit/trunk/wsgikit/tests/test_pyconfig.py 2005-03-30 23:25:41 UTC (rev 2262)
+++ WSGIKit/trunk/wsgikit/tests/test_pyconfig.py 2005-03-31 00:03:13 UTC (rev 2263)
@@ -10,7 +10,7 @@
conf = pyconfig.load(path('one.py'))
assert conf['name1'] == 'n1'
assert conf['name2'] == "n2"
- raises(AttributeError, "conf.name3")
+ raises(KeyError, "conf['name3']")
def test_nest():
conf = pyconfig.load(path('nest1.conf'))
@@ -18,7 +18,19 @@
assert conf['a'] == 1
assert conf['b'] == 2
assert conf['c'] == 3
+
+def test_derivative():
+ conf = pyconfig.Config()
+ conf.load_dict({'test1': 'a'})
+ assert conf['test1'] == 'a'
+ conf.load(path('deriv.conf'))
+ assert conf['test1'] == 'a+another'
+ conf = pyconfig.Config()
+ conf.load_dict({'test1': 'b'})
+ conf.load(path('deriv.conf'))
+ assert conf['test1'] == 'b+another'
+
def test_command():
conf = pyconfig.load(path('one.py'))
extra = conf.load_commandline(
@@ -30,5 +42,5 @@
assert conf['host'] == 'localhost'
assert conf['port'] == 8080
assert conf['config_file'] == 'arg2'
- raises(AttributeError, "conf['h']")
- raises(AttributeError, "conf['f']")
+ raises(KeyError, "conf['h']")
+ raises(KeyError, "conf['f']")
|