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
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
: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`
"""
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):
"""
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.
--- /dev/null
+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