mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 19:14:00 +03:00
Restore configuration methods
This commit is contained in:
parent
0dad8727cf
commit
e22edcdadd
File diff suppressed because it is too large
Load Diff
|
@ -26,11 +26,11 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import configparser as iniconfigparser
|
import configparser as iniconfigparser
|
||||||
|
|
||||||
# try:
|
try:
|
||||||
# import yaml
|
import yaml
|
||||||
# except ImportError:
|
except ImportError:
|
||||||
# yaml = None
|
yaml = None
|
||||||
# yaml = None
|
yaml = None
|
||||||
|
|
||||||
from .errors import (
|
from .errors import (
|
||||||
Error,
|
Error,
|
||||||
|
@ -1203,30 +1203,30 @@ cdef class Configuration(Object):
|
||||||
current_config = {}
|
current_config = {}
|
||||||
self.override(merge_dicts(current_config, config))
|
self.override(merge_dicts(current_config, config))
|
||||||
|
|
||||||
# def from_yaml(self, filepath):
|
def from_yaml(self, filepath):
|
||||||
# """Load configuration from yaml file.
|
"""Load configuration from yaml file.
|
||||||
#
|
|
||||||
# Loaded configuration is merged recursively over current configuration.
|
Loaded configuration is merged recursively over current configuration.
|
||||||
#
|
|
||||||
# :param filepath: Path to the configuration file.
|
:param filepath: Path to the configuration file.
|
||||||
# :type filepath: str
|
:type filepath: str
|
||||||
#
|
|
||||||
# :rtype: None
|
:rtype: None
|
||||||
# """
|
"""
|
||||||
# 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. '
|
||||||
# 'Install PyYAML or install Dependency Injector with yaml extras: '
|
'Install PyYAML or install Dependency Injector with yaml extras: '
|
||||||
# '"pip install dependency-injector[yaml]"'
|
'"pip install dependency-injector[yaml]"'
|
||||||
# )
|
)
|
||||||
#
|
|
||||||
# with open(filepath) as opened_file:
|
with open(filepath) as opened_file:
|
||||||
# config = yaml.load(opened_file, yaml.Loader)
|
config = yaml.load(opened_file, yaml.Loader)
|
||||||
#
|
|
||||||
# current_config = self.__call__()
|
current_config = self.__call__()
|
||||||
# if not current_config:
|
if not current_config:
|
||||||
# current_config = {}
|
current_config = {}
|
||||||
# self.override(merge_dicts(current_config, config))
|
self.override(merge_dicts(current_config, config))
|
||||||
|
|
||||||
def _create_children(self, value):
|
def _create_children(self, value):
|
||||||
children = dict()
|
children = dict()
|
||||||
|
@ -2409,7 +2409,7 @@ def merge_dicts(dict1, dict2):
|
||||||
"""
|
"""
|
||||||
for key, value in dict1.items():
|
for key, value in dict1.items():
|
||||||
if key in dict2:
|
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])
|
dict2[key] = merge_dicts(value, dict2[key])
|
||||||
result = dict1.copy()
|
result = dict1.copy()
|
||||||
result.update(dict2)
|
result.update(dict2)
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
"""Dependency injector config providers unit tests."""
|
"""Dependency injector config providers unit tests."""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
import unittest2 as unittest
|
import unittest2 as unittest
|
||||||
|
|
||||||
from dependency_injector import containers, providers
|
from dependency_injector import containers, providers, errors
|
||||||
|
|
||||||
|
|
||||||
class ConfigTests(unittest.TestCase):
|
class ConfigTests(unittest.TestCase):
|
||||||
|
@ -380,91 +381,91 @@ class ConfigFromIniTests(unittest.TestCase):
|
||||||
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'})
|
||||||
self.assertEqual(self.config.section3.value3(), '3')
|
self.assertEqual(self.config.section3.value3(), '3')
|
||||||
#
|
|
||||||
#
|
|
||||||
# class ConfigFromYamlTests(unittest.TestCase):
|
class ConfigFromYamlTests(unittest.TestCase):
|
||||||
#
|
|
||||||
# def setUp(self):
|
def setUp(self):
|
||||||
# self.config = providers.Configuration(name='config')
|
self.config = providers.Configuration(name='config')
|
||||||
#
|
|
||||||
# _, self.config_file_1 = tempfile.mkstemp()
|
_, self.config_file_1 = tempfile.mkstemp()
|
||||||
# with open(self.config_file_1, 'w') as config_file:
|
with open(self.config_file_1, 'w') as config_file:
|
||||||
# config_file.write(
|
config_file.write(
|
||||||
# 'section1:\n'
|
'section1:\n'
|
||||||
# ' value1: 1\n'
|
' value1: 1\n'
|
||||||
# '\n'
|
'\n'
|
||||||
# 'section2:\n'
|
'section2:\n'
|
||||||
# ' value2: 2\n'
|
' value2: 2\n'
|
||||||
# )
|
)
|
||||||
#
|
|
||||||
# _, self.config_file_2 = tempfile.mkstemp()
|
_, self.config_file_2 = tempfile.mkstemp()
|
||||||
# with open(self.config_file_2, 'w') as config_file:
|
with open(self.config_file_2, 'w') as config_file:
|
||||||
# config_file.write(
|
config_file.write(
|
||||||
# 'section1:\n'
|
'section1:\n'
|
||||||
# ' value1: 11\n'
|
' value1: 11\n'
|
||||||
# ' value11: 11\n'
|
' value11: 11\n'
|
||||||
# 'section3:\n'
|
'section3:\n'
|
||||||
# ' value3: 3\n'
|
' value3: 3\n'
|
||||||
# )
|
)
|
||||||
#
|
|
||||||
# def tearDown(self):
|
def tearDown(self):
|
||||||
# del self.config
|
del self.config
|
||||||
# os.unlink(self.config_file_1)
|
os.unlink(self.config_file_1)
|
||||||
# os.unlink(self.config_file_2)
|
os.unlink(self.config_file_2)
|
||||||
#
|
|
||||||
# def test(self):
|
def test(self):
|
||||||
# self.config.from_yaml(self.config_file_1)
|
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}, 'section2': {'value2': 2}})
|
||||||
# self.assertEqual(self.config.section1(), {'value1': 1})
|
self.assertEqual(self.config.section1(), {'value1': 1})
|
||||||
# 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})
|
||||||
# self.assertEqual(self.config.section2.value2(), 2)
|
self.assertEqual(self.config.section2.value2(), 2)
|
||||||
#
|
|
||||||
# def test_merge(self):
|
def test_merge(self):
|
||||||
# self.config.from_yaml(self.config_file_1)
|
self.config.from_yaml(self.config_file_1)
|
||||||
# self.config.from_yaml(self.config_file_2)
|
self.config.from_yaml(self.config_file_2)
|
||||||
#
|
|
||||||
# self.assertEqual(
|
self.assertEqual(
|
||||||
# self.config(),
|
self.config(),
|
||||||
# {
|
{
|
||||||
# 'section1': {
|
'section1': {
|
||||||
# 'value1': 11,
|
'value1': 11,
|
||||||
# 'value11': 11,
|
'value11': 11,
|
||||||
# },
|
},
|
||||||
# 'section2': {
|
'section2': {
|
||||||
# 'value2': 2,
|
'value2': 2,
|
||||||
# },
|
},
|
||||||
# 'section3': {
|
'section3': {
|
||||||
# 'value3': 3,
|
'value3': 3,
|
||||||
# },
|
},
|
||||||
# },
|
},
|
||||||
# )
|
)
|
||||||
# self.assertEqual(self.config.section1(), {'value1': 11, 'value11': 11})
|
self.assertEqual(self.config.section1(), {'value1': 11, 'value11': 11})
|
||||||
# self.assertEqual(self.config.section1.value1(), 11)
|
self.assertEqual(self.config.section1.value1(), 11)
|
||||||
# self.assertEqual(self.config.section1.value11(), 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.section2.value2(), 2)
|
self.assertEqual(self.config.section2.value2(), 2)
|
||||||
# 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_no_yaml_installed(self):
|
def test_no_yaml_installed(self):
|
||||||
# @contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
# def no_yaml_module():
|
def no_yaml_module():
|
||||||
# yaml = providers.yaml
|
yaml = providers.yaml
|
||||||
# providers.yaml = None
|
providers.yaml = None
|
||||||
#
|
|
||||||
# yield
|
yield
|
||||||
#
|
|
||||||
# providers.yaml = yaml
|
providers.yaml = yaml
|
||||||
#
|
|
||||||
# with no_yaml_module():
|
with no_yaml_module():
|
||||||
# with self.assertRaises(errors.Error) as error:
|
with self.assertRaises(errors.Error) as error:
|
||||||
# self.config.from_yaml(self.config_file_1)
|
self.config.from_yaml(self.config_file_1)
|
||||||
#
|
|
||||||
# self.assertEqual(
|
self.assertEqual(
|
||||||
# error.exception.args[0],
|
error.exception.args[0],
|
||||||
# 'Unable to load yaml configuration - PyYAML is not installed. '
|
'Unable to load yaml configuration - PyYAML is not installed. '
|
||||||
# 'Install PyYAML or install Dependency Injector with yaml extras: '
|
'Install PyYAML or install Dependency Injector with yaml extras: '
|
||||||
# '"pip install dependency-injector[yaml]"',
|
'"pip install dependency-injector[yaml]"',
|
||||||
# )
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user