Merge pull request #5 from edaniszewski/optional-cfg
make file parsing optional
This commit is contained in:
commit
bc7adb48de
|
@ -3,7 +3,7 @@
|
||||||
__title__ = 'bison'
|
__title__ = 'bison'
|
||||||
__description__ = 'Python application configuration'
|
__description__ = 'Python application configuration'
|
||||||
__url__ = 'https://github.com/edaniszewski/bison'
|
__url__ = 'https://github.com/edaniszewski/bison'
|
||||||
__version__ = '0.0.3'
|
__version__ = '0.0.4'
|
||||||
__author__ = 'Erick Daniszewski'
|
__author__ = 'Erick Daniszewski'
|
||||||
__author_email__ = 'edaniszewski@gmail.com'
|
__author_email__ = 'edaniszewski@gmail.com'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
|
|
|
@ -135,10 +135,15 @@ class Bison(object):
|
||||||
if self.scheme:
|
if self.scheme:
|
||||||
self.scheme.validate(self.config)
|
self.scheme.validate(self.config)
|
||||||
|
|
||||||
def parse(self):
|
def parse(self, requires_cfg=True):
|
||||||
"""Parse the configuration sources into `Bison`."""
|
"""Parse the configuration sources into `Bison`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
requires_cfg (bool): Specify whether or not parsing should fail
|
||||||
|
if a config file is not found. (default: True)
|
||||||
|
"""
|
||||||
self._parse_default()
|
self._parse_default()
|
||||||
self._parse_config()
|
self._parse_config(requires_cfg)
|
||||||
self._parse_env()
|
self._parse_env()
|
||||||
|
|
||||||
def _find_config(self):
|
def _find_config(self):
|
||||||
|
@ -164,12 +169,21 @@ class Bison(object):
|
||||||
raise BisonError('No file named {} found in search paths {}'.format(
|
raise BisonError('No file named {} found in search paths {}'.format(
|
||||||
self.config_name, self.config_paths))
|
self.config_name, self.config_paths))
|
||||||
|
|
||||||
def _parse_config(self):
|
def _parse_config(self, requires_cfg=True):
|
||||||
"""Parse the configuration file, if one is configured, and add it to
|
"""Parse the configuration file, if one is configured, and add it to
|
||||||
the `Bison` state.
|
the `Bison` state.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
requires_cfg (bool): Specify whether or not parsing should fail
|
||||||
|
if a config file is not found. (default: True)
|
||||||
"""
|
"""
|
||||||
if len(self.config_paths) > 0:
|
if len(self.config_paths) > 0:
|
||||||
self._find_config()
|
try:
|
||||||
|
self._find_config()
|
||||||
|
except BisonError:
|
||||||
|
if not requires_cfg:
|
||||||
|
return
|
||||||
|
raise
|
||||||
try:
|
try:
|
||||||
with open(self.config_file, 'r') as f:
|
with open(self.config_file, 'r') as f:
|
||||||
parsed = self._fmt_to_parser[self.config_format](f)
|
parsed = self._fmt_to_parser[self.config_format](f)
|
||||||
|
|
|
@ -235,6 +235,39 @@ class TestBison:
|
||||||
assert b.config_file == os.path.join(bad_yaml_config.dirname, bad_yaml_config.basename)
|
assert b.config_file == os.path.join(bad_yaml_config.dirname, bad_yaml_config.basename)
|
||||||
assert len(b._config) == 0
|
assert len(b._config) == 0
|
||||||
|
|
||||||
|
def test_parse_config_not_required_found(self, yaml_config):
|
||||||
|
"""Parse the file config when it isn't required."""
|
||||||
|
b = bison.Bison()
|
||||||
|
b.add_config_paths(yaml_config.dirname)
|
||||||
|
|
||||||
|
assert b.config_file is None
|
||||||
|
assert len(b._config) == 0
|
||||||
|
|
||||||
|
b._parse_config(requires_cfg=False)
|
||||||
|
|
||||||
|
assert b.config_file == os.path.join(yaml_config.dirname, yaml_config.basename)
|
||||||
|
assert len(b._config) == 2
|
||||||
|
assert b.config == {
|
||||||
|
'foo': True,
|
||||||
|
'bar': {
|
||||||
|
'baz': 1,
|
||||||
|
'test': 'value'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_parse_config_not_required_not_found(self):
|
||||||
|
"""Parse the file config when it isn't required."""
|
||||||
|
b = bison.Bison()
|
||||||
|
b.add_config_paths('.')
|
||||||
|
|
||||||
|
assert b.config_file is None
|
||||||
|
assert len(b._config) == 0
|
||||||
|
|
||||||
|
b._parse_config(requires_cfg=False)
|
||||||
|
|
||||||
|
assert b.config_file is None
|
||||||
|
assert len(b._config) == 0
|
||||||
|
|
||||||
def test_parse_defaults_no_scheme(self):
|
def test_parse_defaults_no_scheme(self):
|
||||||
"""Parse the defaults when there is no Scheme."""
|
"""Parse the defaults when there is no Scheme."""
|
||||||
b = bison.Bison()
|
b = bison.Bison()
|
||||||
|
|
Loading…
Reference in New Issue