confini

Parse and merge multiple ini files in python3
git clone git://git.defalsify.org/python-confini.git
Log | Files | Refs | README | LICENSE

commit 3a0e02712bedd6ee2ec49f0c615494c6239a9d12
parent 5bb505d5d3ef6184fe3ce60ad6c7aae971404834
Author: nolash <dev@holbrook.no>
Date:   Tue,  2 Nov 2021 06:11:38 +0100

Add exporter

Diffstat:
Aconfini/export.py | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msetup.py | 2+-
Atests/test_export.py | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 187 insertions(+), 1 deletion(-)

diff --git a/confini/export.py b/confini/export.py @@ -0,0 +1,83 @@ +# import +import sys +import configparser +import os +import stat +import enum +import io +import logging + +logg = logging.getLogger(__name__) + + +class ConfigExporterTarget(enum.Enum): + HANDLE = 1 + FILE = 2 + DIR = 3 + + +class ConfigExporter: + + def __init__(self, config, target=None, split=False): + self.config = config + self.sections = {} + self.target_split = split + self.target_typ = ConfigExporterTarget.HANDLE + self.target = None + if isinstance(target, io.IOBase): + self.target = target + else: + st = os.stat(target) + if stat.S_ISDIR(st.st_mode): + self.target_typ = ConfigExporterTarget.DIR + self.target = os.path.realpath(target) + else: + self.target_typ = ConfigExporterTarget.FILE + d = os.getcwd() + self.target = os.path.join(d, target) + + + def scan(self): + for k in self.config.all(): + (s, v) = k.split('_', maxsplit=1) + s = s.lower() + v = v.lower() + if self.sections.get(s) == None: + self.sections[s] = {} + self.sections[s][v] = self.config.get(k) + + + def export_section(self, k, w): + s = {} + s[k] = self.sections[k] + p = configparser.ConfigParser() + p.read_dict(s) + p.write(w) + + + def export(self): + self.scan() + + w = None + if self.target_typ == ConfigExporterTarget.HANDLE: + w = self.target + + for k in self.sections: + if w != None: + self.export_section(k, w) + continue + + if self.target_typ == ConfigExporterTarget.FILE: + w = open(self.target, 'a') + elif self.target_typ == ConfigExporterTarget.DIR: + if self.target_split: + fn = k + '.ini' + else: + fn = 'config.ini' + fp = os.path.join(self.target, fn) + w = open(fp, 'a') + + self.export_section(k, w) + + w.close() + w = None diff --git a/setup.py b/setup.py @@ -6,7 +6,7 @@ f.close() setup( name='confini', - version='0.5.0rc1', + version='0.5.1', description='Parse, verify and merge all ini files in a single directory', author='Louis Holbrook', author_email='dev@holbrook.no', diff --git a/tests/test_export.py b/tests/test_export.py @@ -0,0 +1,103 @@ +# standard imports +import os +import io +import unittest +import logging +import configparser +import tempfile + +# local imports +from confini import Config +from confini.export import ConfigExporter + +logging.basicConfig(level=logging.DEBUG) + +logg = logging.getLogger() + + +class TestExport(unittest.TestCase): + + wd = os.path.dirname(__file__) + + + def setUp(self): + self.inidir = os.path.join(self.wd, 'files') + c = Config(self.inidir) + c.process() + self.config = c + + + def test_handle(self): + w = io.StringIO() + e = ConfigExporter(self.config, target=w) + e.export() + + w.seek(0) + a = configparser.ConfigParser() + a.read_string(w.read()) + + b = configparser.ConfigParser() + b.read(os.path.join(self.inidir, 'foo.ini')) + b.read(os.path.join(self.inidir, 'bar.ini')) + + for s in b.sections(): + for o in b.options(s): + self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower())) + + + def test_file(self): + (fd, fn) = tempfile.mkstemp() + e = ConfigExporter(self.config, target=fn) + e.export() + + w = os.fdopen(fd) + a = configparser.ConfigParser() + a.read_string(w.read()) + + b = configparser.ConfigParser() + b.read(os.path.join(self.inidir, 'foo.ini')) + b.read(os.path.join(self.inidir, 'bar.ini')) + + for s in b.sections(): + for o in b.options(s): + self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower())) + + + def test_dir_file(self): + d = tempfile.mkdtemp() + e = ConfigExporter(self.config, target=d) + e.export() + + a = configparser.ConfigParser() + a.read(os.path.join(d, 'config.ini')) + + b = configparser.ConfigParser() + b.read(os.path.join(self.inidir, 'foo.ini')) + b.read(os.path.join(self.inidir, 'bar.ini')) + + for s in b.sections(): + for o in b.options(s): + self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower())) + + + def test_dir_split(self): + d = tempfile.mkdtemp() + e = ConfigExporter(self.config, target=d, split=True) + e.export() + + a = configparser.ConfigParser() + a.read(os.path.join(d, 'foo.ini')) + a.read(os.path.join(d, 'bar.ini')) + a.read(os.path.join(d, 'xyzzy.ini')) + + b = configparser.ConfigParser() + b.read(os.path.join(self.inidir, 'foo.ini')) + b.read(os.path.join(self.inidir, 'bar.ini')) + + for s in b.sections(): + for o in b.options(s): + self.assertEqual(b.get(s, o), a.get(s.lower(), o.lower())) + + +if __name__ == '__main__': + unittest.main()