Move Jinja2 handling to a helper function.

master
Simon Quigley 5 years ago
parent 39d32d1b49
commit 1227bc7d00

@ -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):

@ -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"])

@ -0,0 +1,43 @@
#!/usr/bin/env python3
# Copyright (C) 2020 Simon Quigley <tsimonq2@lubuntu.me>
#
# 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 <https://www.gnu.org/licenses/>.
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
Loading…
Cancel
Save