[pypy-svn] r33339 - in pypy/branch/even-more-config3/pypy/config: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Oct 16 16:51:33 CEST 2006


Author: cfbolz
Date: Mon Oct 16 16:51:32 2006
New Revision: 33339

Modified:
   pypy/branch/even-more-config3/pypy/config/config.py
   pypy/branch/even-more-config3/pypy/config/test/test_config.py
Log:
(pedronis, cfbolz): add a do-what-I-meanish set method to config that searches
for options through the tree and looks whether it finds one that matches the
given path.


Modified: pypy/branch/even-more-config3/pypy/config/config.py
==============================================================================
--- pypy/branch/even-more-config3/pypy/config/config.py	(original)
+++ pypy/branch/even-more-config3/pypy/config/config.py	Mon Oct 16 16:51:32 2006
@@ -1,6 +1,12 @@
 
 from py.compat import optparse
 
+class AmbigousOptionError(Exception):
+    pass
+
+class NoMatchingOptionFound(AttributeError):
+    pass
+
 class Config(object):
     _cfgimpl_frozen = False
     
@@ -55,6 +61,22 @@
     def require(self, name, value):
         self.setoption(name, value, "required")
 
+    def set(self, **kwargs):
+        all_paths = [p.split(".") for p in self.getpaths()]
+        for key, value in kwargs.iteritems():
+            key_p = key.split('.')
+            candidates = [p for p in all_paths if p[-len(key_p):] == key_p]
+            if len(candidates) == 1:
+                name = '.'.join(candidates[0])
+                homeconfig, name = self._cfgimpl_get_home_by_path(name)
+                homeconfig.setoption(name, value, "user")
+            elif len(candidates) > 1:
+                raise AmbigousOptionError(
+                    'more than one option that ends with %s' % (key, ))
+            else:
+                raise NoMatchingOptionFound(
+                    'there is no option that matches %s' % (key, ))
+
     def _cfgimpl_get_home_by_path(self, path):
         """returns tuple (config, name)"""
         path = path.split('.')

Modified: pypy/branch/even-more-config3/pypy/config/test/test_config.py
==============================================================================
--- pypy/branch/even-more-config3/pypy/config/test/test_config.py	(original)
+++ pypy/branch/even-more-config3/pypy/config/test/test_config.py	Mon Oct 16 16:51:32 2006
@@ -313,3 +313,25 @@
     descr = make_description()
     c = Config(descr)
     print c # does not crash
+
+def test_dwim_set():
+    descr = OptionDescription("opt", "", [
+        OptionDescription("sub", "", [
+            BoolOption("b1", ""),
+            ChoiceOption("c1", "", ['a', 'b', 'c'], 'a'),
+            BoolOption("d1", ""),
+        ]),
+        BoolOption("b2", ""),
+        BoolOption("d1", ""),
+    ])
+    c = Config(descr)
+    c.set(b1=False, c1='b')
+    assert not c.sub.b1
+    assert c.sub.c1 == 'b'
+    # new config, because you cannot change values once they are set
+    c = Config(descr)
+    c.set(b2=False, **{'sub.c1': 'c'})
+    assert not c.b2
+    assert c.sub.c1 == 'c'
+    py.test.raises(AmbigousOptionError, "c.set(d1=True)")
+    py.test.raises(NoMatchingOptionFound, "c.set(unknown='foo')")



More information about the Pypy-commit mailing list