diff options
author | Rob Austein <sra@hactrn.net> | 2010-02-19 03:17:46 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2010-02-19 03:17:46 +0000 |
commit | d970a6e2499afb4c7ca0168a255946e47d49179e (patch) | |
tree | 106585357e1b746c32bde43ab73881e1e186d0d7 /rpkid | |
parent | 8575ae095ccf893760b1d27d3ad45f0ac03cdaee (diff) |
Clean up ${section::option} parsing, make it work for subscripted
options too.
svn path=/rpkid/rpki/config.py; revision=2979
Diffstat (limited to 'rpkid')
-rw-r--r-- | rpkid/rpki/config.py | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/rpkid/rpki/config.py b/rpkid/rpki/config.py index b09d1300..6d6ad225 100644 --- a/rpkid/rpki/config.py +++ b/rpkid/rpki/config.py @@ -33,12 +33,7 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ -import ConfigParser, os - -debug = False - -if debug: - import rpki.log +import ConfigParser, os, re class parser(object): """ @@ -97,40 +92,38 @@ class parser(object): for key, value in self.cfg.items(section): s = key.rsplit(".", 1) if len(s) == 2 and s[0] == option and s[1].isdigit(): - matches.append((int(s[1]), value)) + matches.append((int(s[1]), self.get(option, section = section))) matches.sort() return [match[1] for match in matches] + _regexp = re.compile("\\${(.*?)::(.*?)}") + + def _repl(self, m): + """ + Replacement function for indirect variable substitution. + This is intended for use with re.subn(). + """ + section, option = m.group(1, 2) + if section == "ENV": + return os.getenv(option, "") + else: + return self.cfg.get(section, option) + def get(self, option, default = None, section = None): """ Get an option, perhaps with a default value. """ if section is None: section = self.default_section + if not self.cfg.has_option(section, option): + option = option.replace("-", "_") if default is not None and not self.cfg.has_option(section, option): return default val = self.cfg.get(section, option) while True: - if debug: - rpki.log.debug("++ [%s]%s = %s" % (section, option, val)) - head, sep, tail = val.partition("${") - if not sep and not tail: + val, modified = self._regexp.subn(self._repl, val, 1) + if not modified: return val - name, sep, tail = tail.partition("}") - if not name or not sep: - raise ValueError, "Couldn't parse indirect reference: %s" % val - section, sep, option = name.partition("::") - if not option or not section or not sep: - raise ValueError, "Couldn't parse indirect reference: %s" % val - if section == "ENV": - newval = head + os.getenv(option, "") + tail - else: - newval = head + self.cfg.get(section, option) + tail - if val == newval: - raise ValueError, "Looping indirect reference: %s" % val - val = newval - if debug: - rpki.log.debug("++ => %s" % val) def getboolean(self, option, default = None, section = None): """ |