]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- [feature] Can create alembic.config.Config
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 Jan 2012 21:23:20 +0000 (16:23 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 20 Jan 2012 21:23:20 +0000 (16:23 -0500)
  with no filename, use set_main_option()
  to add values.  Also added set_section_option()
  which will add sections.  [#23]

CHANGES
alembic/config.py
tests/test_config.py [new file with mode: 0644]

diff --git a/CHANGES b/CHANGES
index 52f48444faf351d848ac14f25259d2088dca1da8..05a8d8696e8ea380cdcf4eddde059a38e1b69f14 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -8,6 +8,11 @@
 - [bug] Fixed alteration of column type on 
   MSSQL to not include the keyword "TYPE".
 
+- [feature] Can create alembic.config.Config
+  with no filename, use set_main_option()
+  to add values.  Also added set_section_option()
+  which will add sections.  [#23]
+
 0.1.1
 =====
 - [bug] Clean up file write operations so that
index d48df0fb475305a1545e97759f2f99206a564f1c..22a48749fed0c83b3992f141537f98d5c5269f47 100644 (file)
@@ -15,7 +15,7 @@ class Config(object):
         some_param = context.config.get_main_option("my option")
     
     When invoking Alembic programatically, a new
-    :class:`.Config` can be created simply by passing
+    :class:`.Config` can be created by passing
     the name of an .ini file to the constructor::
     
         from alembic.config import Config
@@ -24,6 +24,15 @@ class Config(object):
     With a :class:`.Config` object, you can then
     run Alembic commands programmatically using the directives
     in :mod:`alembic.command`.
+    
+    The :class:`.Config` object can also be constructed without
+    a filename.   Values can be set programmatically, and
+    new sections will be created as needed::
+    
+        from alembic.config import Config
+        alembic_cfg = Config()
+        alembic_cfg.set_main_option("url", "postgresql://foo/bar")
+        alembic_cfg.set_section_option("mysection", "foo", "bar")
 
     :param file_: name of the .ini file to open.
     :param ini_section: name of the main Alembic section within the 
@@ -31,8 +40,9 @@ class Config(object):
     :param output_buffer: optional file-like input buffer which
      will be passed to the :class:`.Context` - used to redirect
      access when using Alembic programmatically.
+
     """
-    def __init__(self, file_, ini_section='alembic', output_buffer=None):
+    def __init__(self, file_=None, ini_section='alembic', output_buffer=None):
         """Construct a new :class:`.Config`
         
         """
@@ -59,12 +69,18 @@ class Config(object):
         though the :meth:`.Config.get_section` and 
         :meth:`.Config.get_main_option`
         methods provide a possibly simpler interface.
+
         """
 
-        file_config = ConfigParser.ConfigParser({
-                                    'here':
-                                    os.path.abspath(os.path.dirname(self.config_file_name))})
-        file_config.read([self.config_file_name])
+        if self.config_file_name:
+            here = os.path.abspath(os.path.dirname(self.config_file_name))
+        else:
+            here = ""
+        file_config = ConfigParser.ConfigParser({'here':here})
+        if self.config_file_name:
+            file_config.read([self.config_file_name])
+        else:
+            file_config.add_section(self.config_ini_section)
         return file_config
 
     def get_template_directory(self):
@@ -91,6 +107,18 @@ class Config(object):
         """
         self.file_config.set(self.config_ini_section, name, value)
 
+    def set_section_option(self, section, name, value):
+        """Set an option programmatically within the given section.
+        
+        The section is created if it doesn't exist already.
+        The value here will override whatever was in the .ini
+        file.
+        
+        """
+        if not self.file_config.has_section(section):
+            self.file_config.add_section(section)
+        self.file_config.set(section, name, value)
+
     def get_section_option(self, section, name, default=None):
         """Return an option from the given section of the .ini file.
 
diff --git a/tests/test_config.py b/tests/test_config.py
new file mode 100644 (file)
index 0000000..fb54e8a
--- /dev/null
@@ -0,0 +1,18 @@
+from alembic import config
+from tests import eq_
+
+def test_config_no_file_main_option():
+    cfg = config.Config()
+    cfg.set_main_option("url", "postgresql://foo/bar")
+
+    eq_(cfg.get_main_option("url"), "postgresql://foo/bar")
+
+
+def test_config_no_file_section_option():
+    cfg = config.Config()
+    cfg.set_section_option("foo", "url", "postgresql://foo/bar")
+
+    eq_(cfg.get_section_option("foo", "url"), "postgresql://foo/bar")
+
+    cfg.set_section_option("foo", "echo", "True")
+    eq_(cfg.get_section_option("foo", "echo"), "True")
\ No newline at end of file