Update from_ini() method

This commit is contained in:
Roman Mogylatov 2021-01-24 08:37:00 -05:00
parent fbf01c0f3b
commit ff4cb0e9a8
3 changed files with 4309 additions and 4066 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1312,7 +1312,7 @@ cdef class ConfigurationOption(Provider):
""" """
self.override(value) self.override(value)
def from_ini(self, filepath): def from_ini(self, filepath, required=UNDEFINED):
"""Load configuration from the ini file. """Load configuration from the ini file.
Loaded configuration is merged recursively over existing configuration. Loaded configuration is merged recursively over existing configuration.
@ -1320,12 +1320,17 @@ cdef class ConfigurationOption(Provider):
:param filepath: Path to the configuration file. :param filepath: Path to the configuration file.
:type filepath: str :type filepath: str
:param required: When required is True, raise an exception if file does not exist.
:type required: bool
:rtype: None :rtype: None
""" """
try: try:
parser = _parse_ini_file(filepath) parser = _parse_ini_file(filepath)
except IOError as exception: except IOError as exception:
if self._is_strict_mode_enabled() and exception.errno in (errno.ENOENT, errno.EISDIR): if required is not False \
and (self._is_strict_mode_enabled() or required is True) \
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
@ -1626,7 +1631,7 @@ cdef class Configuration(Object):
""" """
self.override(value) self.override(value)
def from_ini(self, filepath): def from_ini(self, filepath, required=UNDEFINED):
"""Load configuration from the ini file. """Load configuration from the ini file.
Loaded configuration is merged recursively over existing configuration. Loaded configuration is merged recursively over existing configuration.
@ -1634,12 +1639,17 @@ cdef class Configuration(Object):
:param filepath: Path to the configuration file. :param filepath: Path to the configuration file.
:type filepath: str :type filepath: str
:param required: When required is True, raise an exception if file does not exist.
:type required: bool
:rtype: None :rtype: None
""" """
try: try:
parser = _parse_ini_file(filepath) parser = _parse_ini_file(filepath)
except IOError as exception: except IOError as exception:
if self._is_strict_mode_enabled() and exception.errno in (errno.ENOENT, errno.EISDIR): if required is not False \
and (self._is_strict_mode_enabled() or required is True) \
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

View File

@ -444,6 +444,25 @@ class ConfigFromIniTests(unittest.TestCase):
with self.assertRaises(IOError): with self.assertRaises(IOError):
self.config.option.from_ini('./does_not_exist.ini') self.config.option.from_ini('./does_not_exist.ini')
def test_required_file_does_not_exist(self):
with self.assertRaises(IOError):
self.config.from_ini('./does_not_exist.ini', required=True)
def test_required_option_file_does_not_exist(self):
with self.assertRaises(IOError):
self.config.option.from_ini('./does_not_exist.ini', required=True)
def test_not_required_file_does_not_exist_strict_mode(self):
self.config = providers.Configuration(strict=True)
self.config.from_ini('./does_not_exist.ini', required=False)
self.assertEqual(self.config(), {})
def test_not_required_option_file_does_not_exist_strict_mode(self):
self.config = providers.Configuration(strict=True)
self.config.option.from_ini('./does_not_exist.ini', required=False)
with self.assertRaises(errors.Error):
self.config.option()
class ConfigFromIniWithEnvInterpolationTests(unittest.TestCase): class ConfigFromIniWithEnvInterpolationTests(unittest.TestCase):