From: Mike Bayer Date: Tue, 8 Dec 2015 20:01:15 +0000 (-0500) Subject: - document that the value we pass to set_section_option and set_main_option X-Git-Tag: rel_0_8_4~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=831b1b9222d37d0e9b393d83b26811b98d9da0e9;p=thirdparty%2Fsqlalchemy%2Falembic.git - document that the value we pass to set_section_option and set_main_option is in fact subject to variable interpolation, so raw percents must be doubled. - add a test demonstrating the use of interpolation. --- diff --git a/alembic/config.py b/alembic/config.py index 1d4169d6..9534fcc9 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -197,8 +197,17 @@ class Config(object): This overrides whatever was in the .ini file. + :param name: name of the value + + :param value: the value. Note that this value is passed to + ``ConfigParser.set``, which supports variable interpolation using + pyformat (e.g. ``%(some_value)s``). A raw percent sign not part of + an interpolation symbol must therefore be escaped, e.g. ``%%``. + The given value may refer to another value already in the file + using the interpolation format. + """ - self.file_config.set(self.config_ini_section, name, value) + self.set_section_option(self.config_ini_section, name, value) def remove_main_option(self, name): self.file_config.remove_option(self.config_ini_section, name) @@ -210,7 +219,19 @@ class Config(object): The value here will override whatever was in the .ini file. + :param section: name of the section + + :param name: name of the value + + :param value: the value. Note that this value is passed to + ``ConfigParser.set``, which supports variable interpolation using + pyformat (e.g. ``%(some_value)s``). A raw percent sign not part of + an interpolation symbol must therefore be escaped, e.g. ``%%``. + The given value may refer to another value already in the file + using the interpolation format. + """ + if not self.file_config.has_section(section): self.file_config.add_section(section) self.file_config.set(section, name, value) diff --git a/tests/test_config.py b/tests/test_config.py index da0b4131..7b390871 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -49,6 +49,29 @@ class ConfigTest(TestBase): cfg.set_section_option("foo", "echo", "True") eq_(cfg.get_section_option("foo", "echo"), "True") + def test_config_set_main_option_percent(self): + cfg = config.Config() + cfg.set_main_option("foob", "a %% percent") + + eq_(cfg.get_main_option("foob"), "a % percent") + + def test_config_set_section_option_percent(self): + cfg = config.Config() + cfg.set_section_option("some_section", "foob", "a %% percent") + + eq_(cfg.get_section_option("some_section", "foob"), "a % percent") + + def test_config_set_section_option_interpolation(self): + cfg = config.Config() + cfg.set_section_option("some_section", "foob", "foob_value") + + cfg.set_section_option( + "some_section", "bar", "bar with %(foob)s") + + eq_( + cfg.get_section_option("some_section", "bar"), + "bar with foob_value") + def test_standalone_op(self): eng, buf = capture_db()