Skip to content

Commit

Permalink
Remove invalid configuration files from discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
osantana committed Feb 1, 2019
1 parent ea8e2a1 commit edbd3f5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
33 changes: 23 additions & 10 deletions prettyconf/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -228,7 +235,6 @@ def _parse_line(line):
return key, value

def _parse(self):

if self.configs is not None:
return

Expand All @@ -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)]
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cfgfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_filediscover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit edbd3f5

Please sign in to comment.