Restore configuration methods

This commit is contained in:
Roman Mogylatov 2020-06-24 21:04:30 -04:00
parent 0dad8727cf
commit e22edcdadd
3 changed files with 975 additions and 395 deletions

File diff suppressed because it is too large Load Diff

View File

@ -26,11 +26,11 @@ try:
except ImportError:
import configparser as iniconfigparser
# try:
# import yaml
# except ImportError:
# yaml = None
# yaml = None
try:
import yaml
except ImportError:
yaml = None
yaml = None
from .errors import (
Error,
@ -1203,30 +1203,30 @@ cdef class Configuration(Object):
current_config = {}
self.override(merge_dicts(current_config, config))
# def from_yaml(self, filepath):
# """Load configuration from yaml file.
#
# Loaded configuration is merged recursively over current configuration.
#
# :param filepath: Path to the configuration file.
# :type filepath: str
#
# :rtype: None
# """
# if yaml is None:
# raise Error(
# 'Unable to load yaml configuration - PyYAML is not installed. '
# 'Install PyYAML or install Dependency Injector with yaml extras: '
# '"pip install dependency-injector[yaml]"'
# )
#
# with open(filepath) as opened_file:
# config = yaml.load(opened_file, yaml.Loader)
#
# current_config = self.__call__()
# if not current_config:
# current_config = {}
# self.override(merge_dicts(current_config, config))
def from_yaml(self, filepath):
"""Load configuration from yaml file.
Loaded configuration is merged recursively over current configuration.
:param filepath: Path to the configuration file.
:type filepath: str
:rtype: None
"""
if yaml is None:
raise Error(
'Unable to load yaml configuration - PyYAML is not installed. '
'Install PyYAML or install Dependency Injector with yaml extras: '
'"pip install dependency-injector[yaml]"'
)
with open(filepath) as opened_file:
config = yaml.load(opened_file, yaml.Loader)
current_config = self.__call__()
if not current_config:
current_config = {}
self.override(merge_dicts(current_config, config))
def _create_children(self, value):
children = dict()
@ -2409,7 +2409,7 @@ def merge_dicts(dict1, dict2):
"""
for key, value in dict1.items():
if key in dict2:
if isinstance(value, type(dict)) and isinstance(dict2[key], type(dict)):
if isinstance(value, dict) and isinstance(dict2[key], dict):
dict2[key] = merge_dicts(value, dict2[key])
result = dict1.copy()
result.update(dict2)

View File

@ -1,11 +1,12 @@
"""Dependency injector config providers unit tests."""
import contextlib
import os
import tempfile
import unittest2 as unittest
from dependency_injector import containers, providers
from dependency_injector import containers, providers, errors
class ConfigTests(unittest.TestCase):
@ -380,91 +381,91 @@ class ConfigFromIniTests(unittest.TestCase):
self.assertEqual(self.config.section2.value2(), '2')
self.assertEqual(self.config.section3(), {'value3': '3'})
self.assertEqual(self.config.section3.value3(), '3')
#
#
# class ConfigFromYamlTests(unittest.TestCase):
#
# def setUp(self):
# self.config = providers.Configuration(name='config')
#
# _, self.config_file_1 = tempfile.mkstemp()
# with open(self.config_file_1, 'w') as config_file:
# config_file.write(
# 'section1:\n'
# ' value1: 1\n'
# '\n'
# 'section2:\n'
# ' value2: 2\n'
# )
#
# _, self.config_file_2 = tempfile.mkstemp()
# with open(self.config_file_2, 'w') as config_file:
# config_file.write(
# 'section1:\n'
# ' value1: 11\n'
# ' value11: 11\n'
# 'section3:\n'
# ' value3: 3\n'
# )
#
# def tearDown(self):
# del self.config
# os.unlink(self.config_file_1)
# os.unlink(self.config_file_2)
#
# def test(self):
# self.config.from_yaml(self.config_file_1)
#
# self.assertEqual(self.config(), {'section1': {'value1': 1}, 'section2': {'value2': 2}})
# self.assertEqual(self.config.section1(), {'value1': 1})
# self.assertEqual(self.config.section1.value1(), 1)
# self.assertEqual(self.config.section2(), {'value2': 2})
# self.assertEqual(self.config.section2.value2(), 2)
#
# def test_merge(self):
# self.config.from_yaml(self.config_file_1)
# self.config.from_yaml(self.config_file_2)
#
# self.assertEqual(
# self.config(),
# {
# 'section1': {
# 'value1': 11,
# 'value11': 11,
# },
# 'section2': {
# 'value2': 2,
# },
# 'section3': {
# 'value3': 3,
# },
# },
# )
# self.assertEqual(self.config.section1(), {'value1': 11, 'value11': 11})
# self.assertEqual(self.config.section1.value1(), 11)
# self.assertEqual(self.config.section1.value11(), 11)
# self.assertEqual(self.config.section2(), {'value2': 2})
# self.assertEqual(self.config.section2.value2(), 2)
# self.assertEqual(self.config.section3(), {'value3': 3})
# self.assertEqual(self.config.section3.value3(), 3)
#
# def test_no_yaml_installed(self):
# @contextlib.contextmanager
# def no_yaml_module():
# yaml = providers.yaml
# providers.yaml = None
#
# yield
#
# providers.yaml = yaml
#
# with no_yaml_module():
# with self.assertRaises(errors.Error) as error:
# self.config.from_yaml(self.config_file_1)
#
# self.assertEqual(
# error.exception.args[0],
# 'Unable to load yaml configuration - PyYAML is not installed. '
# 'Install PyYAML or install Dependency Injector with yaml extras: '
# '"pip install dependency-injector[yaml]"',
# )
class ConfigFromYamlTests(unittest.TestCase):
def setUp(self):
self.config = providers.Configuration(name='config')
_, self.config_file_1 = tempfile.mkstemp()
with open(self.config_file_1, 'w') as config_file:
config_file.write(
'section1:\n'
' value1: 1\n'
'\n'
'section2:\n'
' value2: 2\n'
)
_, self.config_file_2 = tempfile.mkstemp()
with open(self.config_file_2, 'w') as config_file:
config_file.write(
'section1:\n'
' value1: 11\n'
' value11: 11\n'
'section3:\n'
' value3: 3\n'
)
def tearDown(self):
del self.config
os.unlink(self.config_file_1)
os.unlink(self.config_file_2)
def test(self):
self.config.from_yaml(self.config_file_1)
self.assertEqual(self.config(), {'section1': {'value1': 1}, 'section2': {'value2': 2}})
self.assertEqual(self.config.section1(), {'value1': 1})
self.assertEqual(self.config.section1.value1(), 1)
self.assertEqual(self.config.section2(), {'value2': 2})
self.assertEqual(self.config.section2.value2(), 2)
def test_merge(self):
self.config.from_yaml(self.config_file_1)
self.config.from_yaml(self.config_file_2)
self.assertEqual(
self.config(),
{
'section1': {
'value1': 11,
'value11': 11,
},
'section2': {
'value2': 2,
},
'section3': {
'value3': 3,
},
},
)
self.assertEqual(self.config.section1(), {'value1': 11, 'value11': 11})
self.assertEqual(self.config.section1.value1(), 11)
self.assertEqual(self.config.section1.value11(), 11)
self.assertEqual(self.config.section2(), {'value2': 2})
self.assertEqual(self.config.section2.value2(), 2)
self.assertEqual(self.config.section3(), {'value3': 3})
self.assertEqual(self.config.section3.value3(), 3)
def test_no_yaml_installed(self):
@contextlib.contextmanager
def no_yaml_module():
yaml = providers.yaml
providers.yaml = None
yield
providers.yaml = yaml
with no_yaml_module():
with self.assertRaises(errors.Error) as error:
self.config.from_yaml(self.config_file_1)
self.assertEqual(
error.exception.args[0],
'Unable to load yaml configuration - PyYAML is not installed. '
'Install PyYAML or install Dependency Injector with yaml extras: '
'"pip install dependency-injector[yaml]"',
)