[pypy-svn] r29876 - in pypy/branch/objspace-config-cleanup/pypy/config: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Sun Jul 9 10:29:53 CEST 2006


Author: cfbolz
Date: Sun Jul  9 10:29:51 2006
New Revision: 29876

Modified:
   pypy/branch/objspace-config-cleanup/pypy/config/config.py
   pypy/branch/objspace-config-cleanup/pypy/config/pypyoption.py
   pypy/branch/objspace-config-cleanup/pypy/config/test/test_config.py
Log:
let bool groups be exposed as options that take a list of args. Options can now
be choosen to be exposed using *.


Modified: pypy/branch/objspace-config-cleanup/pypy/config/config.py
==============================================================================
--- pypy/branch/objspace-config-cleanup/pypy/config/config.py	(original)
+++ pypy/branch/objspace-config-cleanup/pypy/config/config.py	Sun Jul  9 10:29:51 2006
@@ -83,7 +83,7 @@
                     child._name, getattr(self, child._name))
             else:
                 substr = str(getattr(self, child._name))
-                substr = "    " + substr.replace("\n", "\n    ")
+                substr = "    " + substr[:-1].replace("\n", "\n    ") + "\n"
                 result += substr
         return result
 
@@ -147,7 +147,10 @@
 
     def add_optparse_option(self, argnames, parser, config):
         def _callback(option, opt_str, value, parser, *args, **kwargs):
-            config.setoption(self._name, True, who='cmdline')
+            try:
+                config.setoption(self._name, True, who='cmdline')
+            except ValueError, e:
+                raise optparse.OptionValueError(e.args[0])
         parser.add_option(help=self.doc,
                             action='callback', 
                             callback=_callback, *argnames)
@@ -203,10 +206,11 @@
                             callback=_callback, *argnames)
 
 class OptionDescription(object):
-    def __init__(self, name, children):
+    def __init__(self, name, children, cmdline=None):
         self._name = name
         self._children = children
         self._build()
+        self.cmdline = cmdline
 
     def _build(self):
         for child in self._children:
@@ -216,15 +220,46 @@
         return tuple([child.getkey(getattr(config, child._name))
                       for child in self._children])
 
+    def add_optparse_option(self, argnames, parser, config):
+        for child in self._children:
+            if not isinstance(child, BoolOption):
+                raise ValueError(
+                    "cannot make OptionDescription %s a cmdline option" % (
+                        self._name, ))
+        def _callback(option, opt_str, value, parser, *args, **kwargs):
+            try:
+                values = value.split(",")
+                for value in values:
+                    value = value.strip()
+                    option = getattr(self, value, None)
+                    if option is None:
+                        raise ValueError("did not find option %s" % (value, ))
+                    getattr(config, self._name).setoption(
+                        value, True, who='cmdline')
+            except ValueError, e:
+                raise optparse.OptionValueError(e.args[0])
+        parser.add_option(help=self._name, action='callback', type='string',
+                          callback=_callback, *argnames)
+
+
 def to_optparse(config, useoptions, parser=None):
     if parser is None:
         parser = optparse.OptionParser()
     for path in useoptions:
-        subconf, name = config._get_by_path(path)
-        option = getattr(subconf._descr, name)
-        if option.cmdline is None:
-            chunks = ('--%s' % (path.replace('.', '-'),),)
+        if path.endswith("*"):
+            assert path.endswith("*")
+            path = path[:-2]
+            subconf, name = config._get_by_path(path)
+            children = [
+                path + "." + child._name
+                for child in getattr(subconf, name)._descr._children]
+            useoptions.extend(children)
         else:
-            chunks = option.cmdline.split(' ')
-        option.add_optparse_option(chunks, parser, subconf)
+            subconf, name = config._get_by_path(path)
+            option = getattr(subconf._descr, name)
+            if option.cmdline is None:
+                chunks = ('--%s' % (path.replace('.', '-'),),)
+            else:
+                chunks = option.cmdline.split(' ')
+            option.add_optparse_option(chunks, parser, subconf)
     return parser

Modified: pypy/branch/objspace-config-cleanup/pypy/config/pypyoption.py
==============================================================================
--- pypy/branch/objspace-config-cleanup/pypy/config/pypyoption.py	(original)
+++ pypy/branch/objspace-config-cleanup/pypy/config/pypyoption.py	Sun Jul  9 10:29:51 2006
@@ -49,10 +49,10 @@
                        default=False, requires=[("withsmallint", False)]),
 
             IntOption("prebuiltintfrom", "lowest integer which is prebuilt",
-                      default=-5),
+                      default=-5, cmdline="--prebuiltinfrom"),
 
             IntOption("prebuiltintto", "highest integer which is prebuilt",
-                      default=100),
+                      default=100, cmdline="--prebuiltintto"),
 
             BoolOption("withstrjoin", "use strings optimized for addition",
                        default=False),
@@ -79,7 +79,10 @@
 if __name__ == '__main__':
     config = Config(pypy_optiondescription)
     parser = to_optparse(config, ["objspace.name",
-                                  "objspace.std.oldstyle",
-                                  "objspace.std.withstrdict"])
+                                  "objspace.std.*",
+                                  "objspace.std.withsmallint",
+                                  "objspace.std.withprebuiltint",
+                                  "objspace.usemodules",
+                                  "objspace.std.prebuiltintfrom",])
     option, args = parser.parse_args()
     print config

Modified: pypy/branch/objspace-config-cleanup/pypy/config/test/test_config.py
==============================================================================
--- pypy/branch/objspace-config-cleanup/pypy/config/test/test_config.py	(original)
+++ pypy/branch/objspace-config-cleanup/pypy/config/test/test_config.py	Sun Jul  9 10:29:51 2006
@@ -153,6 +153,46 @@
     py.test.raises(SystemExit,
             "(options, args) = parser.parse_args(args=['-bfoo'])")
 
+def test_optparse_boolgroup():
+    group = OptionDescription("test", [
+        BoolOption("smallint", "use tagged integers",
+                   default=False),
+        BoolOption("strjoin", "use strings optimized for addition",
+                   default=False),
+        BoolOption("strslice", "use strings optimized for slicing",
+                   default=False),
+        BoolOption("strdict", "use dictionaries optimized for string keys",
+                   default=False),
+    ], cmdline="--test")
+    descr = OptionDescription("all", [group])
+    config = Config(descr)
+    parser = to_optparse(config, ['test'])
+    (options, args) = parser.parse_args(
+        args=['--test=smallint,strjoin,strdict'])
+    
+    assert config.test.smallint
+    assert config.test.strjoin
+    assert config.test.strdict
+
+    config = Config(descr)
+    parser = to_optparse(config, ['test'])
+    (options, args) = parser.parse_args(
+        args=['--test=smallint'])
+    
+    assert config.test.smallint
+    assert not config.test.strjoin
+    assert not config.test.strdict
+
+def test_config_start():
+    descr = make_description()
+    config = Config(descr)
+    parser = to_optparse(config, ["gc.*"])
+
+    options, args = parser.parse_args(args=["--gc-name=framework", "--gc-dummy"])
+    assert config.gc.name == "framework"
+    assert config.gc.dummy
+
+
 def test_optparse_path_options():
     gcoption = ChoiceOption('name', 'GC name', ['ref', 'framework'], 'ref')
     gcgroup = OptionDescription('gc', [gcoption])



More information about the Pypy-commit mailing list