comparison 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
comparison
equal deleted inserted replaced
96:778c3bb1fb41 97:1b4d95f60650
67 raise NotImplementedError("use `readfp()' instead") 67 raise NotImplementedError("use `readfp()' instead")
68 68
69 def readfp(self, fp, filename): 69 def readfp(self, fp, filename):
70 """Read from a file-like object `fp`. 70 """Read from a file-like object `fp`.
71 71
72 The `fp` argument must have a `readline()` method. 72 The `fp` argument must have a `readline()` method.
73 73
74 """ 74 """
75 if hasattr(self, "filename"): 75 if hasattr(self, "filename"):
76 raise RuntimeError("already initialized") 76 raise RuntimeError("already initialized")
77 filename = os.path.normpath(os.path.abspath(filename)) 77 filename = os.path.normpath(os.path.abspath(filename))
147 """Load a single INI file and read/interpolate the sections given in 147 """Load a single INI file and read/interpolate the sections given in
148 `extract`. 148 `extract`.
149 149
150 Flattens the given sections into the resulting dictionary. 150 Flattens the given sections into the resulting dictionary.
151 151
152 Then build a tree out of sections which start with any of the `extract`
153 content value and a point ``.``.
154
152 """ 155 """
153 conf = DictImpl() 156 conf = DictImpl()
154 ini = INIConfigParser(filename) 157 ini = INIConfigParser(filename)
155 for sect in extract: 158 for sect in extract:
156 try: 159 try:
159 pass 162 pass
160 else: 163 else:
161 for option in cfg: 164 for option in cfg:
162 value = ini.getx(sect, option) 165 value = ini.getx(sect, option)
163 conf[option] = value 166 conf[option] = value
167 # try to read "<extract>.xxx" sections as tree
168 for treemarker in [ e + '.' for e in extract ]:
169 sections = list(ini.sections())
170 sections.sort()
171 for section in sections:
172 cur_cfg = conf
173 if section.startswith(treemarker):
174 treestr = section[len(treemarker):]
175 for treepart in treestr.split('.'):
176 cur_cfg = cur_cfg.setdefault(treepart, DictImpl())
177 for option in ini.options(section):
178 value = ini.getx(section, option)
179 cur_cfg[option] = value
164 return conf 180 return conf