Fix ini files parsing

This commit is contained in:
Roman Mogylatov 2020-06-25 23:37:01 -04:00
parent 1cd25e701f
commit 7491fc05a2
3 changed files with 4348 additions and 3868 deletions

View File

@ -11,6 +11,7 @@ follows `Semantic versioning`_
------ ------
- Add interpolation of environment variables to ``Configuration.from_yaml()`` and - Add interpolation of environment variables to ``Configuration.from_yaml()`` and
``Configuration.from_ini()``. ``Configuration.from_ini()``.
- Add ignoring of ``IOError`` to ``Configuration.from_yaml()``.
3.18.0 3.18.0
------ ------

File diff suppressed because it is too large Load Diff

View File

@ -67,11 +67,23 @@ if sys.version_info[0] == 3:
value = super().before_get(parser, section, option, value, defaults) value = super().before_get(parser, section, option, value, defaults)
return os.path.expandvars(value) return os.path.expandvars(value)
def get_ini_parser(): def _parse_ini_file(filepath):
return iniconfigparser.ConfigParser(interpolation=EnvInterpolation()) parser = iniconfigparser.ConfigParser(interpolation=EnvInterpolation())
parser.read(filepath)
return parser
else: else:
def get_ini_parser(): import StringIO
return iniconfigparser.SafeConfigParser(os.environ)
def _parse_ini_file(filepath):
parser = iniconfigparser.ConfigParser()
try:
with open(filepath) as config_file:
config_string = os.path.expandvars(config_file.read())
except IOError:
return parser
else:
parser.readfp(StringIO.StringIO(config_string))
return parser
cdef class Provider(object): cdef class Provider(object):
@ -1202,8 +1214,7 @@ cdef class Configuration(Object):
:rtype: None :rtype: None
""" """
parser = get_ini_parser() parser = _parse_ini_file(filepath)
parser.read([filepath])
config = {} config = {}
for section in parser.sections(): for section in parser.sections():
@ -1231,8 +1242,11 @@ cdef class Configuration(Object):
'"pip install dependency-injector[yaml]"' '"pip install dependency-injector[yaml]"'
) )
with open(filepath) as opened_file: try:
config = yaml.load(opened_file, yaml.Loader) with open(filepath) as opened_file:
config = yaml.load(opened_file, yaml.Loader)
except IOError:
return
current_config = self.__call__() current_config = self.__call__()
if not current_config: if not current_config: