kynikos / lib.py.configfile

Python library to dynamically parse and edit configuration files with support for subsections.
https://kynikos.github.io/lib.py.configfile/
MIT License
2 stars 1 forks source link

Interpolation of options from parent sections #27

Closed lahwaacz closed 9 years ago

lahwaacz commented 9 years ago

I was thinking of this config:

site = ArchWiki
cache-dir = ~/.cache/wiki-scripts/

[ArchWiki]
api-url = https://wiki.archlinux.org/api.php
index-url = https://wiki.archlinux.org/index.php

[ArchWiki.statistics]
cookie-file = ${cache-dir$}/ArchWiki.bot.cookie

Unfortunately parsing this with interpolation=True (and inherit_options=True) fails with KeyError: 'cache-dir'...

(Btw. I had to add - to the Section._OPTION regex to support the above keys, which should be solved by #5, but is there a reason why it's not included by default?)

kynikos commented 9 years ago

The bug should fixed, please test and close. About _OPTION, now you can monkey-patch it with:

from configfile import ConfigFile, Section
Section._OPTION = r'^[a-zA-Z_]+[a-zA-Z0-9_-]*$'
conf = ConfigFile("./test.conf")

5 will give a better way to do it. IIRC the idea behind the default regex (designed years ago) was to possibly allow accessing options as object attributes, although I haven't even opened a report for that :P

lahwaacz commented 9 years ago

Fixed indeed, thanks!

The attribute thing is also the reason why argparse converts - in arg names to _ in dest names. Interestingly the prefix character (- by default) is configurable, but the conversion is hardcoded to account only for -. I guess nobody really uses different prefix...