From 6055667e8090fc09e55b96ae49d5ac6a37706ede Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 12 Dec 2014 08:51:19 -0500 Subject: Source is always Unicode in HTML code. More refactoring to come. --- coverage/files.py | 67 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 23 deletions(-) (limited to 'coverage/files.py') diff --git a/coverage/files.py b/coverage/files.py index c2a5ec72..1400b6eb 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -1,10 +1,12 @@ """File wrangling.""" -from coverage.backward import to_string -from coverage.misc import CoverageException, join_regex import fnmatch, os, os.path, re, sys import ntpath, posixpath +from coverage.backward import to_string, open_python_source +from coverage.misc import CoverageException, join_regex + + class FileLocator(object): """Understand how filenames work.""" @@ -47,29 +49,48 @@ class FileLocator(object): self.canonical_filename_cache[filename] = cf return self.canonical_filename_cache[filename] - def get_zip_data(self, filename): - """Get data from `filename` if it is a zip file path. - Returns the string data read from the zip file, or None if no zip file - could be found or `filename` isn't in it. The data returned will be - an empty string if the file is empty. +def get_python_source(filename): + """Return the source code, as a string.""" + if os.path.exists(filename): + # A regular text file: open it. + with open_python_source(filename) as f: + return f.read() - """ - import zipimport - markers = ['.zip'+os.sep, '.egg'+os.sep] - for marker in markers: - if marker in filename: - parts = filename.split(marker) - try: - zi = zipimport.zipimporter(parts[0]+marker[:-1]) - except zipimport.ZipImportError: - continue - try: - data = zi.get_data(parts[1]) - except IOError: - continue - return to_string(data) - return None + # Maybe it's in a zip file? + source = get_zip_data(filename) + if source is not None: + return source + + # Couldn't find source. + raise CoverageException( + "No source for code: '%s'." % filename + ) + + +def get_zip_data(filename): + """Get data from `filename` if it is a zip file path. + + Returns the string data read from the zip file, or None if no zip file + could be found or `filename` isn't in it. The data returned will be + an empty string if the file is empty. + + """ + import zipimport + markers = ['.zip'+os.sep, '.egg'+os.sep] + for marker in markers: + if marker in filename: + parts = filename.split(marker) + try: + zi = zipimport.zipimporter(parts[0]+marker[:-1]) + except zipimport.ZipImportError: + continue + try: + data = zi.get_data(parts[1]) + except IOError: + continue + return to_string(data) + return None if sys.platform == 'win32': -- cgit v1.2.1