From be4f86318de93e331951e9b698a69d9c4c6cd514 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Fri, 17 Apr 2009 15:43:53 +1000 Subject: [PATCH 1/1] Use jinja2 instead of django We only need a template engine, not the whole web application framework. Signed-off-by: Jeremy Kerr --- hiprofile.py | 33 ++++++++++++++++++--------------- scripts/hiprofile | 7 ------- setup.py | 2 +- share/hiprofile/binary.html | 10 +++++----- share/hiprofile/report.html | 10 +++++----- share/hiprofile/symbol.html | 6 +++--- 6 files changed, 32 insertions(+), 36 deletions(-) diff --git a/hiprofile.py b/hiprofile.py index 212fbf1..c1d1052 100644 --- a/hiprofile.py +++ b/hiprofile.py @@ -6,7 +6,7 @@ import shutil import os import socket from xml.dom.minidom import parse as parseXML -from django.template.loader import render_to_string +from jinja2 import Environment, FileSystemLoader b_id = 0 s_id = 0 @@ -306,24 +306,27 @@ def write_report(report, resourcedir, outdir): os.mkdir(outdir) + # set up template engine + env = Environment(loader = FileSystemLoader(resourcedir), + autoescape = True) + templates = {} + for name in ['report', 'binary', 'symbol']: + templates[name] = env.get_template('%s.html' % name) + # copy required files over files = ['style.css', 'hiprofile.js', 'bar.png', 'jquery-1.3.1.min.js'] for file in files: shutil.copy(os.path.join(resourcedir, file), outdir) - f = open(os.path.join(outdir, 'index.html'), 'w') - f.write(render_to_string('report.html', { 'report': report })) - f.close() + reportfile = os.path.join(outdir, 'index.html') + templates['report'].stream(report = report).dump(reportfile) for binary in report.binaries: - f = open(os.path.join(outdir, binary.filename()), 'w') - f.write(render_to_string('binary.html', - { 'report': report, 'binary': binary })) - f.close() - - for ref in binary.references: - f = open(os.path.join(outdir, ref.filename()), 'w') - f.write(render_to_string('symbol.html', - { 'report': report, 'binary': binary, 'symbol': ref })) - f.close() - + binaryfile = os.path.join(outdir, binary.filename()) + templates['binary'].stream(report = report, binary = binary). \ + dump(binaryfile) + + for symbol in binary.references: + symbolfile = os.path.join(outdir, symbol.filename()) + templates['symbol'].stream(report = report, binary = binary, + symbol = symbol).dump(symbolfile) diff --git a/scripts/hiprofile b/scripts/hiprofile index aa1e361..f663cce 100755 --- a/scripts/hiprofile +++ b/scripts/hiprofile @@ -2,8 +2,6 @@ import sys, os from optparse import OptionParser -from django.conf import settings - from hiprofile import Report, Connection, write_report resourcedir = os.path.join(os.path.dirname(__file__), @@ -31,11 +29,6 @@ def main(args): (options, args) = parser.parse_args() - # set up django template engine - settings.configure(TEMPLATE_LOADERS = - ('django.template.loaders.filesystem.load_template_source',), - TEMPLATE_DIRS = (resourcedir,)) - conn = Connection(options.host) report = Report.extract(conn, options) diff --git a/setup.py b/setup.py index bf41414..f3bd3ff 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup(name = 'hiprofile', url = 'http://ozlabs.org/~jk/projects/hiprofile/', author = 'Jeremy Kerr', author_email = 'jk@ozlabs.org', - requires = ['django'], + requires = ['jinja2'], py_modules = ['hiprofile'], scripts = ['scripts/hiprofile'], data_files = [('share/hiprofile/', diff --git a/share/hiprofile/binary.html b/share/hiprofile/binary.html index 6e71cf7..f94a758 100644 --- a/share/hiprofile/binary.html +++ b/share/hiprofile/binary.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block heading %} -Binary: {{ binary.shortname }} +Binary: {{ binary.shortname() }} {% endblock %} {% block content %} @@ -24,12 +24,12 @@ Binary: {{ binary.shortname }} {% for symbol in binary.references %} - {{ symbol.name }} + {{ symbol.name }} {{ symbol.count }} - {{ symbol.percentage|floatformat:2 }} + {{ "%.2f"|format(symbol.percentage) }} {{ symbol.percentage|floatformat:1 }}% + width="{{ "%d"|format(symbol.percentage) }}" + alt="{{ "%.1f"|format(symbol.percentage) }}%"/> {% endfor %} diff --git a/share/hiprofile/report.html b/share/hiprofile/report.html index 0cd07c1..c2fe2c3 100644 --- a/share/hiprofile/report.html +++ b/share/hiprofile/report.html @@ -32,13 +32,13 @@ Oprofile report {% for binary in report.binaries %} - {{binary.shortname}} + {{binary.shortname()}} {{ binary.count }} - {{ binary.percentage|floatformat:2 }} + {{ "%.2f"|format(binary.percentage) }} {{ binary.percentage|floatformat:1 }}% + width="{{ "%d"|format(binary.percentage) }}" + alt="{{ "%.1f"|format(binary.percentage) }}%"/> {% endfor %} diff --git a/share/hiprofile/symbol.html b/share/hiprofile/symbol.html index e776568..17b8cd9 100644 --- a/share/hiprofile/symbol.html +++ b/share/hiprofile/symbol.html @@ -28,8 +28,8 @@ {% for insn in symbol.insns %} 0x{{ insn.addr }} - {{ insn.samples }} - {{ insn.percentage|floatformat:2 }} + {{ insn.samples }} + {{ "%.2f"|format(insn.percentage) }} {% if insn.source %} + {% else %} @@ -37,7 +37,7 @@ {% endif %}
{{insn.source}}
-
{{ insn.asm }}
+
{{ insn.asm }}
{% endfor %} -- 2.39.2