From 941eeb588459098a85266b4c5d176d6deed2eb53 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 14 Sep 2014 16:44:21 -0400 Subject: Progress on plugins --- coverage/codeunit.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'coverage/codeunit.py') diff --git a/coverage/codeunit.py b/coverage/codeunit.py index 35167a72..3ec9c390 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -10,14 +10,16 @@ from coverage.phystokens import source_token_lines, source_encoding from coverage.django import DjangoTracer -def code_unit_factory(morfs, file_locator, get_ext=None): +def code_unit_factory(morfs, file_locator, get_plugin=None): """Construct a list of CodeUnits from polymorphic inputs. `morfs` is a module or a filename, or a list of same. `file_locator` is a FileLocator that can help resolve filenames. - `get_ext` TODO + `get_plugin` is a function taking a filename, and returning a plugin + responsible for the file. It can also return None if there is no plugin + claiming the file. Returns a list of CodeUnit objects. @@ -26,15 +28,14 @@ def code_unit_factory(morfs, file_locator, get_ext=None): if not isinstance(morfs, (list, tuple)): morfs = [morfs] - django_tracer = DjangoTracer() - code_units = [] for morf in morfs: - ext = None - if isinstance(morf, string_class) and get_ext: - ext = get_ext(morf) - if ext: - klass = DjangoTracer # NOT REALLY! TODO + plugin = None + if isinstance(morf, string_class) and get_plugin: + plugin = get_plugin(morf) + if plugin: + klass = plugin.code_unit_class(morf) + #klass = DjangoTracer # NOT REALLY! TODO # Hacked-in Mako support. Define COVERAGE_MAKO_PATH as a fragment of # the path that indicates the Python file is actually a compiled Mako # template. THIS IS TEMPORARY! @@ -162,6 +163,9 @@ class CodeUnit(object): """ return False + def get_parser(self, exclude=None): + raise NotImplementedError + class PythonCodeUnit(CodeUnit): """Represents a Python file.""" -- cgit v1.2.1 From ebfa5a5bcbd0256193e01076bd4cad0d3b22b56f Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 14 Sep 2014 17:08:33 -0400 Subject: Don't ask people to pass source back into CodeUnits --- coverage/codeunit.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'coverage/codeunit.py') diff --git a/coverage/codeunit.py b/coverage/codeunit.py index 3ec9c390..4e752aaa 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -92,6 +92,8 @@ class CodeUnit(object): self.name = n self.modname = modname + self._source = None + def __repr__(self): return "" % (self.name, self.filename) @@ -132,6 +134,11 @@ class CodeUnit(object): return root.replace('\\', '_').replace('/', '_').replace('.', '_') def source(self): + if self._source is None: + self._source = self.get_source() + return self._source + + def get_source(self): """Return the source code, as a string.""" if os.path.exists(self.filename): # A regular text file: open it. @@ -148,10 +155,9 @@ class CodeUnit(object): "No source for code '%s'." % self.filename ) - def source_token_lines(self, source): + def source_token_lines(self): """Return the 'tokenized' text for the code.""" - # TODO: Taking source here is wrong, change it? - for line in source.splitlines(): + for line in self.source().splitlines(): yield [('txt', line)] def should_be_python(self): @@ -242,11 +248,11 @@ class PythonCodeUnit(CodeUnit): # Everything else is probably not Python. return False - def source_token_lines(self, source): - return source_token_lines(source) + def source_token_lines(self): + return source_token_lines(self.source()) - def source_encoding(self, source): - return source_encoding(source) + def source_encoding(self): + return source_encoding(self.source()) class MakoParser(CodeParser): @@ -275,26 +281,25 @@ class MakoCodeUnit(CodeUnit): py_source = open(self.filename).read() self.metadata = ModuleInfo.get_module_source_metadata(py_source, full_line_map=True) - def source(self): + def get_source(self): return open(self.metadata['filename']).read() def get_parser(self, exclude=None): return MakoParser(self.metadata) - def source_encoding(self, source): - # TODO: Taking source here is wrong, change it! + def source_encoding(self): return self.metadata['source_encoding'] class DjangoCodeUnit(CodeUnit): - def source(self): + def get_source(self): with open(self.filename) as f: return f.read() def get_parser(self, exclude=None): return DjangoParser(self.filename) - def source_encoding(self, source): + def source_encoding(self): return "utf8" -- cgit v1.2.1 From f491f4f37ff2e18090b1f8c74d20a95591f4fc81 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 21 Sep 2014 09:25:23 -0400 Subject: Pylint upgrade and cleanup. --- coverage/codeunit.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'coverage/codeunit.py') diff --git a/coverage/codeunit.py b/coverage/codeunit.py index 4e752aaa..c9ab2622 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -7,8 +7,6 @@ from coverage.misc import CoverageException, NoSource from coverage.parser import CodeParser, PythonParser from coverage.phystokens import source_token_lines, source_encoding -from coverage.django import DjangoTracer - def code_unit_factory(morfs, file_locator, get_plugin=None): """Construct a list of CodeUnits from polymorphic inputs. -- cgit v1.2.1