From 6359c719d6ae03764cf487c3e80e598cd19c5596 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Fri, 20 Apr 2012 17:37:11 -0400 Subject: The [html]extra_css configuration value is a file path to a CSS file that gets copied into the HTML report. --- coverage/config.py | 3 +++ coverage/control.py | 11 +++++++++-- coverage/html.py | 15 +++++++++++++++ coverage/htmlfiles/index.html | 3 +++ coverage/htmlfiles/pyfile.html | 3 +++ 5 files changed, 33 insertions(+), 2 deletions(-) (limited to 'coverage') diff --git a/coverage/config.py b/coverage/config.py index ef45ba5..49d74e7 100644 --- a/coverage/config.py +++ b/coverage/config.py @@ -52,6 +52,7 @@ class CoverageConfig(object): # Defaults for [html] self.html_dir = "htmlcov" + self.extra_css = None # Defaults for [xml] self.xml_output = "coverage.xml" @@ -125,6 +126,8 @@ class CoverageConfig(object): # [html] if cp.has_option('html', 'directory'): self.html_dir = cp.get('html', 'directory') + if cp.has_option('html', 'extra_css'): + self.extra_css = cp.get('html', 'extra_css') # [xml] if cp.has_option('xml', 'output'): diff --git a/coverage/control.py b/coverage/control.py index 7373b31..c21d885 100644 --- a/coverage/control.py +++ b/coverage/control.py @@ -582,15 +582,22 @@ class coverage(object): reporter.report(morfs, directory=directory) def html_report(self, morfs=None, directory=None, ignore_errors=None, - omit=None, include=None): + omit=None, include=None, extra_css=None): """Generate an HTML report. + The HTML is written to `directory`. The file "index.html" is the + overview starting point, with links to more detailed pages for + individual modules. + + `extra_css` is a path to a file of other CSS to apply on the page. + It will be copied into the HTML directory. + See `coverage.report()` for other arguments. """ self.config.from_args( ignore_errors=ignore_errors, omit=omit, include=include, - html_dir=directory, + html_dir=directory, extra_css=extra_css, ) reporter = HtmlReporter(self, self.config) reporter.report(morfs) diff --git a/coverage/html.py b/coverage/html.py index af27edf..f39bf94 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -58,6 +58,7 @@ class HtmlReporter(Reporter): self.files = [] self.arcs = self.coverage.data.has_arcs() self.status = HtmlStatus() + self.extra_css = None def report(self, morfs): """Generate an HTML report for `morfs`. @@ -78,6 +79,10 @@ class HtmlReporter(Reporter): self.status.reset() self.status.set_settings_hash(these_settings) + # The user may have extra CSS they want copied. + if self.config.extra_css: + self.extra_css = os.path.basename(self.config.extra_css) + # Process all the files. self.report_files(self.html_file, morfs, self.config.html_dir) @@ -91,12 +96,20 @@ class HtmlReporter(Reporter): def make_local_static_report_files(self): """Make local instances of static files for HTML report.""" + # The files we provide must always be copied. for static in self.STATIC_FILES: shutil.copyfile( data_filename("htmlfiles/" + static), os.path.join(self.directory, static) ) + # The user may have extra CSS they want copied. + if self.extra_css: + shutil.copyfile( + self.config.extra_css, + os.path.join(self.directory, self.extra_css) + ) + def write_html(self, fname, html): """Write `html` to `fname`, properly encoded.""" fout = open(fname, "wb") @@ -202,6 +215,7 @@ class HtmlReporter(Reporter): # Write the HTML page for this file. html_filename = flat_rootname + ".html" html_path = os.path.join(self.directory, html_filename) + extra_css = self.extra_css html = spaceless(self.source_tmpl.render(locals())) if sys.version_info < (3, 0): @@ -228,6 +242,7 @@ class HtmlReporter(Reporter): arcs = self.arcs totals = sum([f['nums'] for f in files]) + extra_css = self.extra_css self.write_html( os.path.join(self.directory, "index.html"), diff --git a/coverage/htmlfiles/index.html b/coverage/htmlfiles/index.html index 04b314a..c6d9eec 100644 --- a/coverage/htmlfiles/index.html +++ b/coverage/htmlfiles/index.html @@ -4,6 +4,9 @@ Coverage report + {% if extra_css %} + + {% endif %} diff --git a/coverage/htmlfiles/pyfile.html b/coverage/htmlfiles/pyfile.html index ee0a3b1..434edfd 100644 --- a/coverage/htmlfiles/pyfile.html +++ b/coverage/htmlfiles/pyfile.html @@ -7,6 +7,9 @@ Coverage for {{cu.name|escape}}: {{nums.pc_covered_str}}% + {% if extra_css %} + + {% endif %} -- cgit v1.2.1