diff options
author | grabner <pjg.github@ubergrabner.net> | 2016-12-14 11:42:35 -0500 |
---|---|---|
committer | grabner <pjg.github@ubergrabner.net> | 2016-12-14 11:42:35 -0500 |
commit | 179db88fb541e756283ba349c14c223cc889b162 (patch) | |
tree | b9204e10d1ba04ecc78260f3cd6ae01da9869acc | |
parent | e3dd87f1a8076df83d7eee63a75e20b7f1be9d77 (diff) | |
download | iniherit-179db88fb541e756283ba349c14c223cc889b162.tar.gz |
moved Loader class definition below global variables
-rw-r--r-- | iniherit/__init__.py | 2 | ||||
-rw-r--r-- | iniherit/interpolation.py | 44 | ||||
-rw-r--r-- | iniherit/parser.py | 16 |
3 files changed, 57 insertions, 5 deletions
diff --git a/iniherit/__init__.py b/iniherit/__init__.py index 74e1062..bcfbafc 100644 --- a/iniherit/__init__.py +++ b/iniherit/__init__.py @@ -8,7 +8,9 @@ from .parser import * from . import mixin +from .interpolation import InterpolationMissingEnvError #------------------------------------------------------------------------------ # end of $Id$ +# $ChangeLog$ #------------------------------------------------------------------------------ diff --git a/iniherit/interpolation.py b/iniherit/interpolation.py new file mode 100644 index 0000000..d431b9b --- /dev/null +++ b/iniherit/interpolation.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +#------------------------------------------------------------------------------ +# file: $Id$ +# auth: Philip J Grabner <grabner@cadit.com> +# date: 2016/12/14 +# copy: (C) Copyright 2016-EOT Cadit Inc., All Rights Reserved. +#------------------------------------------------------------------------------ + +import re +import os + +from six.moves import configparser as CP + +#------------------------------------------------------------------------------ +class InterpolationMissingEnvError(CP.InterpolationMissingOptionError): pass + +#------------------------------------------------------------------------------ +_env_cre = re.compile(r'%\(ENV:([^:)]+)(:-([^)]*))?\)s', flags=re.DOTALL) +def interpolate(parser, base_interpolate, section, option, rawval, vars): + value = rawval + depth = CP.MAX_INTERPOLATION_DEPTH + repl = lambda match: _env_replace( + match, parser, base_interpolate, section, option, rawval, vars) + while depth and _env_cre.search(value): + depth -= 1 + value = _env_cre.sub(repl, value) + if _env_cre.search(value): + raise CP.InterpolationDepthError(option, section, rawval) + if '%(SUPER)s' in value: + raise InterpolationMissingSuperError(option, section, rawval, 'SUPER') + return base_interpolate(parser, section, option, value, vars) + +#------------------------------------------------------------------------------ +def _env_replace(match, parser, base_interpolate, section, option, rawval, vars): + if match.group(1) in os.environ: + return os.environ.get(match.group(1)) + if match.group(2) is not None: + return match.group(3) + raise InterpolationMissingEnvError(option, section, rawval, match.group(1)) + +#------------------------------------------------------------------------------ +# end of $Id$ +# $ChangeLog$ +#------------------------------------------------------------------------------ diff --git a/iniherit/parser.py b/iniherit/parser.py index c3f9db1..a186cfe 100644 --- a/iniherit/parser.py +++ b/iniherit/parser.py @@ -27,11 +27,6 @@ __all__ = ( ) #------------------------------------------------------------------------------ -class Loader(object): - def load(self, name, encoding=None): - if encoding is None: - return open(name) - return open(name, encoding=encoding) _real_RawConfigParser = CP.RawConfigParser _real_ConfigParser = CP.ConfigParser @@ -40,6 +35,15 @@ _real_SafeConfigParser = CP.SafeConfigParser DEFAULT_INHERITTAG = '%inherit' #------------------------------------------------------------------------------ +class Loader(object): + def load(self, name, encoding=None): + # todo: these fp are leaked... need to use "contextlib.closing" somehow... + if encoding is None: + return open(name) + return open(name, encoding=encoding) + + +#------------------------------------------------------------------------------ # TODO: this would probably be *much* simpler with meta-classes... #------------------------------------------------------------------------------ @@ -188,6 +192,8 @@ class SafeConfigParser(ConfigParser, _real_SafeConfigParser): ConfigParser.__init__(self, loader=loader) _real_SafeConfigParser.__init__(self, *args, **kw) + #------------------------------------------------------------------------------ # end of $Id$ +# $ChangeLog$ #------------------------------------------------------------------------------ |