]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- document that the value we pass to set_section_option and set_main_option
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Dec 2015 20:01:15 +0000 (15:01 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Dec 2015 20:01:15 +0000 (15:01 -0500)
is in fact subject to variable interpolation, so raw percents must be doubled.
- add a test demonstrating the use of interpolation.

alembic/config.py
tests/test_config.py

index 1d4169d68c42e2055b341a99300ea02bfd6e0b9b..9534fcc98c8fa007366d85bb9e903c5518616cf5 100644 (file)
@@ -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)
index da0b4131855d959a3ceaee11eb2c052e861f9a23..7b39087197e68d7b46b2fd0d1fe5fcf0d33f76f5 100644 (file)
@@ -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()