diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-07-04 22:19:27 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-07-04 22:19:27 -0400 |
commit | 1aeeca32c2c17eb99ab83e6e3ebf7feb5cafedff (patch) | |
tree | fee87e2e12cd2eee6b0ae7031c9dbab9891359a5 /coverage/codeunit.py | |
parent | be5178fda38750c0aae78061bf1b1bf3397a467d (diff) | |
parent | f346f85e04e44294e4c26f876e8dc75b17c4f8d7 (diff) | |
download | python-coveragepy-git-1aeeca32c2c17eb99ab83e6e3ebf7feb5cafedff.tar.gz |
Merged in the Django work
Diffstat (limited to 'coverage/codeunit.py')
-rw-r--r-- | coverage/codeunit.py | 72 |
1 files changed, 54 insertions, 18 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py index 9282687d..35167a72 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -7,14 +7,18 @@ 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): + +def code_unit_factory(morfs, file_locator, get_ext=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 + Returns a list of CodeUnit objects. """ @@ -22,19 +26,28 @@ def code_unit_factory(morfs, file_locator): if not isinstance(morfs, (list, tuple)): morfs = [morfs] + django_tracer = DjangoTracer() + code_units = [] for morf in morfs: - # 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! - MAKO_PATH = os.environ.get('COVERAGE_MAKO_PATH') - if MAKO_PATH and isinstance(morf, string_class) and MAKO_PATH in morf: - # Super hack! Do mako both ways! - if 0: - cu = PythonCodeUnit(morf, file_locator) - cu.name += '_fako' - code_units.append(cu) - klass = MakoCodeUnit + ext = None + if isinstance(morf, string_class) and get_ext: + ext = get_ext(morf) + if ext: + 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! + #MAKO_PATH = os.environ.get('COVERAGE_MAKO_PATH') + #if MAKO_PATH and isinstance(morf, string_class) and MAKO_PATH in morf: + # # Super hack! Do mako both ways! + # if 0: + # cu = PythonCodeUnit(morf, file_locator) + # cu.name += '_fako' + # code_units.append(cu) + # klass = MakoCodeUnit + #elif isinstance(morf, string_class) and morf.endswith(".html"): + # klass = DjangoCodeUnit else: klass = PythonCodeUnit code_units.append(klass(morf, file_locator)) @@ -134,6 +147,12 @@ class CodeUnit(object): "No source for code '%s'." % self.filename ) + def source_token_lines(self, source): + """Return the 'tokenized' text for the code.""" + # TODO: Taking source here is wrong, change it? + for line in source.splitlines(): + yield [('txt', line)] + def should_be_python(self): """Does it seem like this file should contain Python? @@ -258,12 +277,29 @@ class MakoCodeUnit(CodeUnit): def get_parser(self, exclude=None): return MakoParser(self.metadata) - def source_token_lines(self, source): - """Return the 'tokenized' text for the code.""" - # TODO: Taking source here is wrong, change it? - for line in source.splitlines(): - yield [('txt', line)] - def source_encoding(self, source): # TODO: Taking source here is wrong, change it! return self.metadata['source_encoding'] + + +class DjangoCodeUnit(CodeUnit): + def 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): + return "utf8" + + +class DjangoParser(CodeParser): + def __init__(self, filename): + self.filename = filename + + def parse_source(self): + with open(self.filename) as f: + source = f.read() + executable = set(range(1, len(source.splitlines())+1)) + return executable, set() |