From edbd3f573bde3941034b67e89d7de0fe578f594c Mon Sep 17 00:00:00 2001 From: Osvaldo Santana Neto Date: Fri, 1 Feb 2019 15:50:24 -0200 Subject: [PATCH] Remove invalid configuration files from discovery --- prettyconf/loaders.py | 33 +++++++++++++++++++++++---------- tests/test_cfgfile.py | 2 +- tests/test_filediscover.py | 6 +++--- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/prettyconf/loaders.py b/prettyconf/loaders.py index 8688154..b0ec270 100644 --- a/prettyconf/loaders.py +++ b/prettyconf/loaders.py @@ -43,6 +43,9 @@ def __contains__(self, item): def __getitem__(self, item): raise NotImplementedError() # pragma: no cover + def check(self): + return True + # noinspection PyAbstractClass class AbstractConfigurationFileLoader(AbstractConfigurationLoader): @@ -107,18 +110,22 @@ def _parse(self): self._initialized = True - def __contains__(self, item): + def check(self): try: self._parse() except (FileNotFoundError, InvalidConfigurationFile, MissingSettingsSection): return False + return super().check() + + def __contains__(self, item): + if not self.check(): + return False + return self.parser.has_option(self.section, self.var_format(item)) def __getitem__(self, item): - try: - self._parse() - except (FileNotFoundError, MissingSettingsSection): + if not self.check(): raise KeyError("{!r}".format(item)) try: @@ -228,7 +235,6 @@ def _parse_line(line): return key, value def _parse(self): - if self.configs is not None: return @@ -242,18 +248,22 @@ def _parse(self): self.configs[key] = value - def __contains__(self, item): + def check(self): try: self._parse() except FileNotFoundError: return False + return super().check() + + def __contains__(self, item): + if not self.check(): + return False + return self.var_format(item) in self.configs def __getitem__(self, item): - try: - self._parse() - except FileNotFoundError: + if not self.check(): raise KeyError("{!r}".format(item)) return self.configs[self.var_format(item)] @@ -309,7 +319,10 @@ def _scan_path(self, path): for patterns, Loader in self.filetypes: for filename in self.get_filenames(path, patterns): try: - config_files.append(Loader(filename=filename)) + loader = Loader(filename=filename) + if not loader.check(): + continue + config_files.append(loader) except InvalidConfigurationFile: continue diff --git a/tests/test_cfgfile.py b/tests/test_cfgfile.py index e92f6a0..58d8852 100644 --- a/tests/test_cfgfile.py +++ b/tests/test_cfgfile.py @@ -46,7 +46,7 @@ def test_skip_missing_key(self): def test_skip_invalid_ini_file(self): config = IniFile(self.test_files_path + "/invalid_chars.cfg") - with self.assertRaises(InvalidConfigurationFile): + with self.assertRaises(KeyError): return config['some_value'] def test_default_var_format(self): diff --git a/tests/test_filediscover.py b/tests/test_filediscover.py index 1e4b232..b97fb4d 100644 --- a/tests/test_filediscover.py +++ b/tests/test_filediscover.py @@ -23,7 +23,7 @@ def test_config_file_parsing(self): self._create_file(self.test_files_path + "/../settings.ini", "[settings]\nFOO=bar") discovery = RecursiveSearch(os.path.dirname(self.test_files_path)) self.assertTrue(repr(discovery).startswith("RecursiveSearch(starting_path=")) - self.assertEqual(len(discovery.config_files), 3) # 2 *valid* files created + prettyconf project itself + self.assertEqual(len(discovery.config_files), 2) # 2 *valid* files created self.assertIn('FOO', discovery) self.assertEqual(discovery['FOO'], 'bar') @@ -32,7 +32,7 @@ def test_config_file_parsing(self): def test_should_not_look_for_parent_directory_when_it_finds_valid_configurations(self): starting_path = self.test_files_path + '/recursive/valid/' discovery = RecursiveSearch(starting_path, root_path=self.test_files_path) - self.assertEqual(len(discovery.config_files), 6) # FIXME + self.assertEqual(len(discovery.config_files), 4) filenames = [cfg.filename for cfg in discovery.config_files] self.assertIn(starting_path + '.env', filenames) self.assertIn(starting_path + 'settings.ini', filenames) @@ -41,7 +41,7 @@ def test_should_look_for_parent_directory_when_it_finds_invalid_configurations(s starting_path = self.test_files_path + '/recursive/valid/invalid/' valid_path = self.test_files_path + '/recursive/valid/' discovery = RecursiveSearch(starting_path, root_path=self.test_files_path) - self.assertEqual(len(discovery.config_files), 6) # FIXME + self.assertEqual(len(discovery.config_files), 4) filenames = [cfg.filename for cfg in discovery.config_files] self.assertIn(valid_path + '.env', filenames) self.assertIn(valid_path + 'settings.ini', filenames)