diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-01-20 16:23:20 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-01-20 16:23:20 -0500 |
commit | 38368e8f9a306ba9dfa668dcc8cfaef5d2f031b1 (patch) | |
tree | 7bb9cc3b4d2912e1c821076dbfe14025787d48c7 | |
parent | a1feddafbd9806988ecc5c266d8a8fdb9f0d985d (diff) | |
download | alembic-38368e8f9a306ba9dfa668dcc8cfaef5d2f031b1.tar.gz |
- [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]
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | alembic/config.py | 40 | ||||
-rw-r--r-- | tests/test_config.py | 18 |
3 files changed, 57 insertions, 6 deletions
@@ -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 diff --git a/alembic/config.py b/alembic/config.py index d48df0f..22a4874 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -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 index 0000000..fb54e8a --- /dev/null +++ b/tests/test_config.py @@ -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 |