diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2013-09-22 14:33:10 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2013-09-22 22:01:11 +0200 |
commit | 58dcfbf3ff7152016bf3906bbf5805e097a1ee0e (patch) | |
tree | ddfb6267918f7edaa7d05d9a4f8e7c89d869b918 | |
parent | 44fc843ea803118aeffc4914f17414eaee040e0b (diff) |
Provide function for template-based report rendering
This uses the Jinja template engine to produce the report HTML files.
This also renames the util module to output to better describe its
purpose.
-rw-r--r-- | webcheck/crawler.py | 2 | ||||
-rw-r--r-- | webcheck/output.py (renamed from webcheck/util.py) | 24 | ||||
-rw-r--r-- | webcheck/plugins/__init__.py | 2 |
3 files changed, 25 insertions, 3 deletions
diff --git a/webcheck/crawler.py b/webcheck/crawler.py index 0c1a2da..abd1828 100644 --- a/webcheck/crawler.py +++ b/webcheck/crawler.py @@ -41,7 +41,7 @@ import urlparse from webcheck import config from webcheck.db import Session, Base, Link, truncate_db -from webcheck.util import install_file +from webcheck.output import install_file import webcheck.parsers from sqlalchemy import create_engine diff --git a/webcheck/util.py b/webcheck/output.py index 911e3a2..d49ed21 100644 --- a/webcheck/util.py +++ b/webcheck/output.py @@ -1,5 +1,5 @@ -# util.py - utility functions for webcheck +# output.py - utility functions for webcheck # # Copyright (C) 1998, 1999 Albert Hopkins (marduk) # Copyright (C) 2002 Mike W. Meyer @@ -22,15 +22,22 @@ # The files produced as output from the software do not automatically fall # under the copyright of the software, unless explicitly stated otherwise. +"""Utility functions for generating the report.""" + import codecs import logging import os import shutil import sys +import time import urllib import urlparse +import jinja2 + from webcheck import config +from webcheck.db import Link +import webcheck logger = logging.getLogger(__name__) @@ -111,3 +118,18 @@ def install_file(source, is_text=False): # close files tfp.close() sfp.close() + + +env = jinja2.Environment( + loader=jinja2.PackageLoader('webcheck'), + extensions=['jinja2.ext.autoescape'], + autoescape=True, + trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True) + + +def render(output_file, **kwargs): + """Render the output file with the specified context variables.""" + template = env.get_template(output_file) + fp = open_file(output_file) + fp.write(template.render(**kwargs)) + fp.close() diff --git a/webcheck/plugins/__init__.py b/webcheck/plugins/__init__.py index 4c534fd..4d032a4 100644 --- a/webcheck/plugins/__init__.py +++ b/webcheck/plugins/__init__.py @@ -51,7 +51,7 @@ import webcheck from webcheck import config from webcheck.db import Link from webcheck.parsers.html import htmlescape -from webcheck.util import open_file +from webcheck.output import open_file def _floatformat(f): |