diff configmix/ini.py @ 97:1b4d95f60650

Build a tree-ish configuration from an INI style configuration file
author Franz Glasner <hg@dom66.de>
date Sun, 18 Mar 2018 19:15:01 +0100
parents 218807d7d883
children ba5970a2dcef
line wrap: on
line diff
--- a/configmix/ini.py	Sun Mar 18 19:13:35 2018 +0100
+++ b/configmix/ini.py	Sun Mar 18 19:15:01 2018 +0100
@@ -69,7 +69,7 @@
     def readfp(self, fp, filename):
         """Read from a file-like object `fp`.
 
-        The `fp` argument must have a `readline()` method. 
+        The `fp` argument must have a `readline()` method.
 
         """
         if hasattr(self, "filename"):
@@ -149,6 +149,9 @@
 
     Flattens the given sections into the resulting dictionary.
 
+    Then build a tree out of sections which start with any of the `extract`
+    content value and a point ``.``.
+
     """
     conf = DictImpl()
     ini = INIConfigParser(filename)
@@ -161,4 +164,17 @@
             for option in cfg:
                 value = ini.getx(sect, option)
                 conf[option] = value
+    # try to read "<extract>.xxx" sections as tree
+    for treemarker in [ e + '.' for e in extract ]:
+        sections = list(ini.sections())
+        sections.sort()
+        for section in sections:
+            cur_cfg = conf
+            if section.startswith(treemarker):
+                treestr = section[len(treemarker):]
+                for treepart in treestr.split('.'):
+                    cur_cfg = cur_cfg.setdefault(treepart, DictImpl())
+                for option in ini.options(section):
+                    value = ini.getx(section, option)
+                    cur_cfg[option] = value
     return conf