From 77f0b522e1a16b1dba36432d5aa7a099bfe84403 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 19 Dec 2015 17:41:41 -0500 Subject: updated lps_gen.RenderHTML - Now handles the case when the template does not exist. - HTML output is now prettified by BeautifulSoup. --- lps_gen.py | 16 ++++++++++++++-- setup.py | 10 +++++++--- tests/test_lps_gen.py | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lps_gen.py b/lps_gen.py index e4cd0e8..89d20e6 100644 --- a/lps_gen.py +++ b/lps_gen.py @@ -25,7 +25,9 @@ from argparse import ArgumentParser from collections import OrderedDict from os import path +from bs4 import BeautifulSoup from jinja2 import Environment, PackageLoader +from jinja2.exceptions import TemplateNotFound from mistune import Renderer, Markdown @@ -146,9 +148,19 @@ def RenderHTML(lps_dict, year): env = Environment(loader=PackageLoader('lps_gen', 'templates'), trim_blocks=True, lstrip_blocks=True) - template = env.get_template('lp-sch-%s.jinja2' % year) - return template.render(schedule=lps_dict) + template_name = 'lp-sch-%s.jinja2' % year + template = None + + try: + template = env.get_template(template_name) + except TemplateNotFound as e: + print "Template %s not found." % template_name + return + + lps_html = template.render(schedule=lps_dict) + + return BeautifulSoup(lps_html, 'html.parser').prettify() def main(): diff --git a/setup.py b/setup.py index c31e669..c767ef9 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# # Copyright (C) 2015 lpschedule-generator contributors. See CONTRIBUTORS. # # This file is part of lpschedule-generator. @@ -22,14 +25,15 @@ except ImportError: from distutils.core import setup config = { + 'name': 'lpschedule-generator', 'description': 'LibrePlanet schedule generator', 'author': 'rsiddharth', 'url': 'https://notabug.org/rsd/lpschedule-generator/', 'author_email': 'rsd@gnu.org', 'version': '0.0', - 'install_requires': ['nose', 'mistune', 'Jinja2'], + 'install_requires': ['nose', 'mock', 'mistune', 'Jinja2', 'beautifulsoup4'], 'py_modules': ['lps_gen'], - 'name': 'lpschedule-generator' - } + 'data_files':[('templates',['templates/lp-sch-2016.jinja2'])], +} setup(**config) diff --git a/tests/test_lps_gen.py b/tests/test_lps_gen.py index cab3fa8..8473955 100644 --- a/tests/test_lps_gen.py +++ b/tests/test_lps_gen.py @@ -22,8 +22,10 @@ import json import pprint import mistune +import mock from os import path +from StringIO import StringIO from nose.tools import * @@ -161,6 +163,18 @@ class TestLpsGen(object): print lps_html + def test_RenderHTML_invalid_year(self): + """Testing `RenderHTML` function - with invalid year + """ + with mock.patch('sys.stdout', new_callable=StringIO) as out: + invalid_year = '2016_invalid' + template_name = 'lp-sch-%s.jinja2' % invalid_year + + lps_html = RenderHTML(self.lps_dict, invalid_year) + expected_out = 'Template %s not found.\n' % template_name + assert out.getvalue() == expected_out + + def teardown(self): """Cleans up things after each test in this class.""" pass -- cgit v1.2.3