Update from_ini()

This commit is contained in:
Roman Mogylatov 2021-01-23 10:26:34 -05:00
parent 6386a0b94d
commit ce736c867d
3 changed files with 6538 additions and 5964 deletions

File diff suppressed because it is too large Load Diff

View File

@ -65,19 +65,16 @@ if sys.version_info[0] == 3:
def _parse_ini_file(filepath): def _parse_ini_file(filepath):
parser = iniconfigparser.ConfigParser(interpolation=EnvInterpolation()) parser = iniconfigparser.ConfigParser(interpolation=EnvInterpolation())
parser.read(filepath) with open(filepath) as config_file:
parser.read_file(config_file)
return parser return parser
else: else:
import StringIO import StringIO
def _parse_ini_file(filepath): def _parse_ini_file(filepath):
parser = iniconfigparser.ConfigParser() parser = iniconfigparser.ConfigParser()
try:
with open(filepath) as config_file: with open(filepath) as config_file:
config_string = os.path.expandvars(config_file.read()) config_string = os.path.expandvars(config_file.read())
except IOError:
return parser
else:
parser.readfp(StringIO.StringIO(config_string)) parser.readfp(StringIO.StringIO(config_string))
return parser return parser
@ -1325,7 +1322,13 @@ cdef class ConfigurationOption(Provider):
:rtype: None :rtype: None
""" """
try:
parser = _parse_ini_file(filepath) parser = _parse_ini_file(filepath)
except IOError as exception:
if self._is_strict_mode_enabled() and exception.errno in (errno.ENOENT, errno.EISDIR):
exception.strerror = 'Unable to load configuration file {0}'.format(exception.strerror)
raise
return
config = {} config = {}
for section in parser.sections(): for section in parser.sections():
@ -1349,8 +1352,6 @@ cdef class ConfigurationOption(Provider):
:rtype: None :rtype: None
""" """
cdef Configuration root
if yaml is None: if yaml is None:
raise Error( raise Error(
'Unable to load yaml configuration - PyYAML is not installed. ' 'Unable to load yaml configuration - PyYAML is not installed. '
@ -1366,15 +1367,9 @@ cdef class ConfigurationOption(Provider):
with open(filepath) as opened_file: with open(filepath) as opened_file:
config = yaml.load(opened_file, loader) config = yaml.load(opened_file, loader)
except IOError as exception: except IOError as exception:
root = self.__root_ref() if self._is_strict_mode_enabled() and exception.errno in (errno.ENOENT, errno.EISDIR):
if not root:
return
if root.__strict and exception.errno in (errno.ENOENT, errno.EISDIR):
exception.strerror = 'Unable to load configuration file {0}'.format(exception.strerror) exception.strerror = 'Unable to load configuration file {0}'.format(exception.strerror)
raise raise
return return
current_config = self.__call__() current_config = self.__call__()
@ -1411,6 +1406,15 @@ cdef class ConfigurationOption(Provider):
value = os.getenv(name, default) value = os.getenv(name, default)
self.override(value) self.override(value)
def _is_strict_mode_enabled(self):
cdef Configuration root
root = self.__root_ref()
if not root:
return False
return root.__strict
cdef class TypedConfigurationOption(Callable): cdef class TypedConfigurationOption(Callable):
@ -1619,7 +1623,13 @@ cdef class Configuration(Object):
:rtype: None :rtype: None
""" """
try:
parser = _parse_ini_file(filepath) parser = _parse_ini_file(filepath)
except IOError as exception:
if self.__strict and exception.errno in (errno.ENOENT, errno.EISDIR):
exception.strerror = 'Unable to load configuration file {0}'.format(exception.strerror)
raise
return
config = {} config = {}
for section in parser.sections(): for section in parser.sections():

View File

@ -426,6 +426,24 @@ class ConfigFromIniTests(unittest.TestCase):
self.assertEqual(self.config.section3(), {'value3': '3'}) self.assertEqual(self.config.section3(), {'value3': '3'})
self.assertEqual(self.config.section3.value3(), '3') self.assertEqual(self.config.section3.value3(), '3')
def test_file_does_not_exist(self):
self.config.from_ini('./does_not_exist.ini')
self.assertEqual(self.config(), {})
def test_file_does_not_exist_strict_mode(self):
self.config = providers.Configuration(strict=True)
with self.assertRaises(IOError):
self.config.from_ini('./does_not_exist.ini')
def test_option_file_does_not_exist(self):
self.config.option.from_ini('does_not_exist.ini')
self.assertIsNone(self.config.option.undefined())
def test_option_file_does_not_exist_strict_mode(self):
self.config = providers.Configuration(strict=True)
with self.assertRaises(IOError):
self.config.option.from_ini('./does_not_exist.ini')
class ConfigFromIniWithEnvInterpolationTests(unittest.TestCase): class ConfigFromIniWithEnvInterpolationTests(unittest.TestCase):
@ -535,7 +553,7 @@ class ConfigFromYamlTests(unittest.TestCase):
def test_file_does_not_exist_strict_mode(self): def test_file_does_not_exist_strict_mode(self):
self.config = providers.Configuration(strict=True) self.config = providers.Configuration(strict=True)
with self.assertRaises(OSError): with self.assertRaises(IOError):
self.config.from_yaml('./does_not_exist.yml') self.config.from_yaml('./does_not_exist.yml')
def test_option_file_does_not_exist(self): def test_option_file_does_not_exist(self):
@ -544,7 +562,7 @@ class ConfigFromYamlTests(unittest.TestCase):
def test_option_file_does_not_exist_strict_mode(self): def test_option_file_does_not_exist_strict_mode(self):
self.config = providers.Configuration(strict=True) self.config = providers.Configuration(strict=True)
with self.assertRaises(OSError): with self.assertRaises(IOError):
self.config.option.from_yaml('./does_not_exist.yml') self.config.option.from_yaml('./does_not_exist.yml')
def test_no_yaml_installed(self): def test_no_yaml_installed(self):