mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-21 17:16:46 +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',
|
||||
download_url='https://pypi.python.org/pypi/dependency_injector',
|
||||
install_requires=requirements,
|
||||
extras_require={
|
||||
'yaml': [
|
||||
'pyyaml',
|
||||
],
|
||||
},
|
||||
# extras_require={
|
||||
# 'yaml': [
|
||||
# 'pyyaml',
|
||||
# ],
|
||||
# },
|
||||
package_dir={
|
||||
'': 'src',
|
||||
},
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -26,10 +26,11 @@ try:
|
|||
except ImportError:
|
||||
import configparser as iniconfigparser
|
||||
|
||||
try:
|
||||
import yaml
|
||||
except ImportError:
|
||||
yaml = None
|
||||
# try:
|
||||
# import yaml
|
||||
# except ImportError:
|
||||
# yaml = None
|
||||
# yaml = None
|
||||
|
||||
from .errors import (
|
||||
Error,
|
||||
|
@ -1021,7 +1022,9 @@ cdef class Configuration(Object):
|
|||
print(config.section1.option2()) # 2
|
||||
"""
|
||||
|
||||
def __init__(self, name='config', default=None):
|
||||
DEFAULT_NAME = 'config'
|
||||
|
||||
def __init__(self, name=None, default=None):
|
||||
"""Initializer.
|
||||
|
||||
:param name: Name of configuration unit.
|
||||
|
@ -1032,6 +1035,9 @@ cdef class Configuration(Object):
|
|||
"""
|
||||
super(Configuration, self).__init__(default)
|
||||
|
||||
if name is None:
|
||||
name = self.DEFAULT_NAME
|
||||
|
||||
self.__name = name
|
||||
self.__children = self._create_children(default)
|
||||
self.__linked = list()
|
||||
|
@ -1197,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()
|
||||
|
|
|
@ -381,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]"',
|
||||
# )
|
||||
|
|
Loading…
Reference in New Issue
Block a user