Permalink
Browse files

InfoExtractor: Validate information kind

Adds an attribute "supported_info_kinds". A ValueError
is raise if the Type of Info instance being stored in the
InfoExtractor class is not contained in this class attribute.

Related to #101
  • Loading branch information...
satwikkansal committed Jun 18, 2017
1 parent 060d3ff commit b9e9746ff13fd51adaac761a1b3a0eee2ae37238
Showing with 45 additions and 0 deletions.
  1. +9 −0 coala_quickstart/info_extraction/InfoExtractor.py
  2. +36 −0 tests/info_extraction/InfoExtractorTest.py
@@ -1,12 +1,16 @@
import os
from coalib.parsing.Globbing import glob, fnmatch
from coala_quickstart.info_extraction.Info import Info
class InfoExtractor:
# tuple of file globs supported by the extractor.
supported_file_globs = tuple()
# tuple of ``Info`` classes that can be extracted.
supported_info_kinds = (Info,)
def __init__(self,
target_globs,
project_directory):
@@ -52,6 +56,11 @@ def _add_info(self, fname, info_to_add):
:param info_to_add: list of ``Info`` instances to add.
"""
for info in info_to_add:
if not isinstance(info, self.supported_info_kinds):
raise ValueError("The class {} is not present in "
"supported information kinds of {}".format(
info.name,
self.__class__.__name__))
if not info.extractor:
info.extractor = self
if self._information.get(fname):
@@ -53,6 +53,20 @@ def parse_file(self, fname, file_content):
def find_information(self, fname, parsed_file):
return []
class WrongSupportedInfoExtractor(InfoExtractor):
supported_file_globs = ("**",)
supported_info_kinds = (DummyInfoExtractor,)
def parse_file(self, fname, file_content):
return file_content
def find_information(self, fname, parsed_file):
# The extractor returns AnotherDummyInfo instead
# of DummyInfo.
return [AnotherDummyInfo(
fname,
'Some random information it is!')]
class TempfileExtractor(InfoExtractor):
supported_file_globs = ("tempfile**", "**tempfile")
@@ -69,6 +83,7 @@ def find_information(self, fname, parsed_file):
self.DummyMultiInfoExtractor = DummyMultiInfoExtractor
self.NoInfoExtractor = NoInfoExtractor
self.TempfileExtractor = TempfileExtractor
self.WrongSupportedInfoExtractor = WrongSupportedInfoExtractor
def test_implementation(self):
@@ -230,3 +245,24 @@ def test_unsupported_files(self):
extracted_info = uut.extract_information()
self.assertEqual(len(extracted_info.keys()), 1)
def test_supported_info_kinds(self):
target_filenames = ['target_file_1', ]
target_file_contents = ['Some content.']
with generate_files(
target_filenames,
target_file_contents,
self.current_dir) as gen_files:
uut = self.WrongSupportedInfoExtractor(
['target_file_**'],
self.current_dir)
with self.assertRaisesRegexp(
ValueError,
("The class AnotherDummyInfo is not present in supported "
"information kinds of WrongSupportedInfoExtractor")):
uut.extract_information()

0 comments on commit b9e9746

Please sign in to comment.