Update from_yaml()

This commit is contained in:
Roman Mogylatov 2021-01-23 09:54:39 -05:00
parent 500855895b
commit 922b7b8ec6
3 changed files with 5791 additions and 5456 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@
from __future__ import absolute_import from __future__ import absolute_import
import copy import copy
import errno
import functools import functools
import inspect import inspect
import os import os
@ -1365,6 +1366,8 @@ 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. '
@ -1379,7 +1382,16 @@ cdef class ConfigurationOption(Provider):
try: try:
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: except IOError as exception:
root = self.__root_ref()
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)
raise
return return
current_config = self.__call__() current_config = self.__call__()
@ -1661,7 +1673,10 @@ cdef class Configuration(Object):
try: try:
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: 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 return
current_config = self.__call__() current_config = self.__call__()

View File

@ -529,6 +529,24 @@ class ConfigFromYamlTests(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_yaml('./does_not_exist.yml')
self.assertEqual(self.config(), {})
def test_file_does_not_exist_strict_mode(self):
self.config = providers.Configuration(strict=True)
with self.assertRaises(OSError):
self.config.from_yaml('./does_not_exist.yml')
def test_option_file_does_not_exist(self):
self.config.option.from_yaml('./does_not_exist.yml')
self.assertIsNone(self.config.option())
def test_option_file_does_not_exist_strict_mode(self):
self.config = providers.Configuration(strict=True)
with self.assertRaises(OSError):
self.config.option.from_yaml('./does_not_exist.yml')
def test_no_yaml_installed(self): def test_no_yaml_installed(self):
@contextlib.contextmanager @contextlib.contextmanager
def no_yaml_module(): def no_yaml_module():