Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
Allow exporting for .ini config files
Browse files Browse the repository at this point in the history
  • Loading branch information
Onur Güzel authored and tony committed May 5, 2019
1 parent e9fdb75 commit 9579394
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions kaptan/handlers/ini_handler.py
Expand Up @@ -22,6 +22,8 @@


class KaptanIniParser(configparser.RawConfigParser):
def from_dict(self, dictionary):
self._sections = dictionary

def as_dict(self):
d = dict(self._sections)
Expand All @@ -39,5 +41,10 @@ def load(self, value):
config.read_file(StringIO(value))
return config.as_dict()

def dump(self, file_):
raise NotImplementedError("Exporting .ini files is not supported.")
def dump(self, data, file_=None):
if file_ is None:
raise NotImplementedError("Exporting .ini as string is not supported.")
config = KaptanIniParser()
config.from_dict(data)
with open(file_, 'w') as fp:
config.write(fp)
24 changes: 24 additions & 0 deletions tests/test_kaptan.py
Expand Up @@ -220,6 +220,30 @@ def test_ini_file_handler(tmpdir, testconfig):
) == 'mysql://poor_user:poor_password@localhost/poor_posts'


def test_ini_file_dump(tmpdir):
testdict = {
"development": {
"DATABASE_URI": "mysql://root:123456@localhost/posts"
},
"production": {
"DATABASE_URI": "mysql://poor_user:poor_password@localhost/poor_posts" # NOQA
}
}

config = kaptan.Kaptan()
config.import_config(testdict)

ini_file = tmpdir.join('config.ini')
config.export('ini', file_=str(ini_file))

ini_config = kaptan.Kaptan(handler='ini')
ini_config.import_config(str(ini_file))

assert ini_config.get(
'production.database_uri'
) == 'mysql://poor_user:poor_password@localhost/poor_posts'


def test_py_file_handler(testconfig, tmpdir, monkeypatch):
py_file = tmpdir.join('config.py')
py_file.write("""DATABASE = 'mysql://root:123456@localhost/girlz'
Expand Down

0 comments on commit 9579394

Please sign in to comment.