Skip to content

Commit

Permalink
make file parsing optional
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Mar 9, 2018
1 parent 0d573d1 commit b7d9198
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bison/__version__.py
Expand Up @@ -3,7 +3,7 @@
__title__ = 'bison'
__description__ = 'Python application configuration'
__url__ = 'https://github.com/edaniszewski/bison'
__version__ = '0.0.3'
__version__ = '0.0.4'
__author__ = 'Erick Daniszewski'
__author_email__ = 'edaniszewski@gmail.com'
__license__ = 'MIT'
Expand Down
24 changes: 19 additions & 5 deletions bison/bison.py
Expand Up @@ -135,10 +135,15 @@ def validate(self):
if self.scheme:
self.scheme.validate(self.config)

def parse(self):
"""Parse the configuration sources into `Bison`."""
def parse(self, requires_cfg=True):
"""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_config()
self._parse_config(requires_cfg)
self._parse_env()

def _find_config(self):
Expand All @@ -164,12 +169,21 @@ def _find_config(self):
raise BisonError('No file named {} found in search paths {}'.format(
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
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:
self._find_config()
try:
self._find_config()
except BisonError:
if not requires_cfg:
return
raise
try:
with open(self.config_file, 'r') as f:
parsed = self._fmt_to_parser[self.config_format](f)
Expand Down
33 changes: 33 additions & 0 deletions tests/test_bison.py
Expand Up @@ -235,6 +235,39 @@ def test_parse_config_fail(self, bad_yaml_config):
assert b.config_file == os.path.join(bad_yaml_config.dirname, bad_yaml_config.basename)
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):
"""Parse the defaults when there is no Scheme."""
b = bison.Bison()
Expand Down

0 comments on commit b7d9198

Please sign in to comment.