mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Comment yaml-related code
This commit is contained in:
parent
f7871dfc49
commit
52c98bbe49
10
setup.py
10
setup.py
|
@ -40,11 +40,11 @@ setup(name='dependency-injector',
|
||||||
url='https://github.com/ets-labs/python-dependency-injector',
|
url='https://github.com/ets-labs/python-dependency-injector',
|
||||||
download_url='https://pypi.python.org/pypi/dependency_injector',
|
download_url='https://pypi.python.org/pypi/dependency_injector',
|
||||||
install_requires=requirements,
|
install_requires=requirements,
|
||||||
extras_require={
|
# extras_require={
|
||||||
'yaml': [
|
# 'yaml': [
|
||||||
'pyyaml',
|
# 'pyyaml',
|
||||||
],
|
# ],
|
||||||
},
|
# },
|
||||||
package_dir={
|
package_dir={
|
||||||
'': 'src',
|
'': 'src',
|
||||||
},
|
},
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -26,10 +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
|
||||||
|
|
||||||
from .errors import (
|
from .errors import (
|
||||||
Error,
|
Error,
|
||||||
|
@ -1021,7 +1022,9 @@ cdef class Configuration(Object):
|
||||||
print(config.section1.option2()) # 2
|
print(config.section1.option2()) # 2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, name='config', default=None):
|
DEFAULT_NAME = 'config'
|
||||||
|
|
||||||
|
def __init__(self, name=None, default=None):
|
||||||
"""Initializer.
|
"""Initializer.
|
||||||
|
|
||||||
:param name: Name of configuration unit.
|
:param name: Name of configuration unit.
|
||||||
|
@ -1032,6 +1035,9 @@ cdef class Configuration(Object):
|
||||||
"""
|
"""
|
||||||
super(Configuration, self).__init__(default)
|
super(Configuration, self).__init__(default)
|
||||||
|
|
||||||
|
if name is None:
|
||||||
|
name = self.DEFAULT_NAME
|
||||||
|
|
||||||
self.__name = name
|
self.__name = name
|
||||||
self.__children = self._create_children(default)
|
self.__children = self._create_children(default)
|
||||||
self.__linked = list()
|
self.__linked = list()
|
||||||
|
@ -1197,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()
|
||||||
|
|
|
@ -381,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