diff --git a/bison/__version__.py b/bison/__version__.py index 4196ea1..c2e3d74 100644 --- a/bison/__version__.py +++ b/bison/__version__.py @@ -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' diff --git a/bison/bison.py b/bison/bison.py index 737bbc0..f1b30f9 100644 --- a/bison/bison.py +++ b/bison/bison.py @@ -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): @@ -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) diff --git a/tests/test_bison.py b/tests/test_bison.py index 377bff2..5fe50f5 100644 --- a/tests/test_bison.py +++ b/tests/test_bison.py @@ -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()