mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-07-03 20:03:32 +03:00
Update from_ini() method
This commit is contained in:
parent
fbf01c0f3b
commit
ff4cb0e9a8
File diff suppressed because it is too large
Load Diff
|
@ -1312,7 +1312,7 @@ cdef class ConfigurationOption(Provider):
|
|||
"""
|
||||
self.override(value)
|
||||
|
||||
def from_ini(self, filepath):
|
||||
def from_ini(self, filepath, required=UNDEFINED):
|
||||
"""Load configuration from the ini file.
|
||||
|
||||
Loaded configuration is merged recursively over existing configuration.
|
||||
|
@ -1320,12 +1320,17 @@ cdef class ConfigurationOption(Provider):
|
|||
:param filepath: Path to the configuration file.
|
||||
:type filepath: str
|
||||
|
||||
:param required: When required is True, raise an exception if file does not exist.
|
||||
:type required: bool
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
try:
|
||||
parser = _parse_ini_file(filepath)
|
||||
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)
|
||||
raise
|
||||
return
|
||||
|
@ -1626,7 +1631,7 @@ cdef class Configuration(Object):
|
|||
"""
|
||||
self.override(value)
|
||||
|
||||
def from_ini(self, filepath):
|
||||
def from_ini(self, filepath, required=UNDEFINED):
|
||||
"""Load configuration from the ini file.
|
||||
|
||||
Loaded configuration is merged recursively over existing configuration.
|
||||
|
@ -1634,12 +1639,17 @@ cdef class Configuration(Object):
|
|||
:param filepath: Path to the configuration file.
|
||||
:type filepath: str
|
||||
|
||||
:param required: When required is True, raise an exception if file does not exist.
|
||||
:type required: bool
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
try:
|
||||
parser = _parse_ini_file(filepath)
|
||||
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)
|
||||
raise
|
||||
return
|
||||
|
|
|
@ -444,6 +444,25 @@ class ConfigFromIniTests(unittest.TestCase):
|
|||
with self.assertRaises(IOError):
|
||||
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):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user