From 1227bc7d002578134d6138f29df93c2f9cc7285a Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Wed, 1 Jul 2020 17:49:04 -0500 Subject: [PATCH] Move Jinja2 handling to a helper function. --- metrics | 24 ++++-------------------- modules/jenkins.py | 21 +++++---------------- modules/utilities.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 36 deletions(-) create mode 100755 modules/utilities.py diff --git a/metrics b/metrics index 27ab4fa..ba26ed1 100755 --- a/metrics +++ b/metrics @@ -18,8 +18,8 @@ import argparse import logging as log import sqlite3 -from jinja2 import Template from modules.jenkins import JenkinsModule +from modules.utilities import * from os import path from shutil import copytree, rmtree @@ -62,26 +62,10 @@ def summary(): are going to have different averages. """ - # Open the template file as a Jinja2 Template - t_path = path.join("templates", "index.html") - with open(t_path) as templatef: - template = "" - for text in templatef.readlines(): - template += text - template = Template(template) - - # Render the template with the values from summary_page + src = path.join("templates", "index.html") + dest = path.join("output", "index.html") log.debug("summary_page: " + str(summary_page)) - template = template.render(page=summary_page) - - # Write it back to the output file - # We don't have to worry about creating the output dir, since the - # module-specific template rendering should already do this for us - # - # FIXME: Writing to/from files is done in several places, maybe centralize - # the code in its own "internal" module? - with open(path.join("output", "index.html"), "w+") as f: - f.write(template) + jinja2_template(src, dest, page=summary_page) def main(module): diff --git a/modules/jenkins.py b/modules/jenkins.py index 1809b6d..36a848d 100755 --- a/modules/jenkins.py +++ b/modules/jenkins.py @@ -20,7 +20,7 @@ import requests_cache import time from jenkinsapi.custom_exceptions import NoBuildData from jenkinsapi.jenkins import Jenkins -from jinja2 import Template +from modules.utilities import * from os import getenv, makedirs, path requests_cache.install_cache("jenkins", backend="sqlite", expire_after=300) @@ -171,25 +171,14 @@ class JenkinsModule: "failing": zip(_data["date"], _data["failing"]), "total": zip(_data["date"], _data["total"])} - # Grab our template from templates/ and store it as a Template - t_path = path.join("templates", "jenkins.html") - with open(t_path) as templatef: - template = "" - for text in templatef.readlines(): - template += text - template = Template(template) - - # Render the template - template = template.render(jenkins=jenkins, average=average, days=days) - # Make the output dir if it doesn't already exist if not path.exists("output"): makedirs("output") - # Write it back to the filename in the output dir - with open(path.join("output", "jenkins_%sdays.html" % days), - "w+") as f: - f.write(template) + src = path.join("templates", "jenkins.html") + dest = path.join("output", "jenkins_%sdays.html" % days) + jinja2_template(src, dest, jenkins=jenkins, average=average, + days=days) # Return the averages for use in the summary return (average["nonpassing"], average["failing"], average["total"]) diff --git a/modules/utilities.py b/modules/utilities.py new file mode 100755 index 0000000..6c184c1 --- /dev/null +++ b/modules/utilities.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2020 Simon Quigley +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from jinja2 import Template +from os import path + + +def jinja2_template(src, dest, **kwargs): + """Wrapper for working with Jinja templates + + src is the Jinja2 template to use, dest is where the rendered file needs + to go, and any other keywords are passed directly to Template.render() + """ + + # Open the template file as a Jinja2 Template + with open(src) as templatef: + template = "" + for text in templatef.readlines(): + template += text + template = Template(template) + + # Render the template + template = template.render(**kwargs) + + # Write the template to the dest + with open(dest, "w+") as f: + f.write(template) + + return True