From 73ccb20703f86d968cbcf617eb0eafa3e89c6b92 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 13 Jul 2019 19:27:18 -0400 Subject: lps_gen: Remove `lp_t` command line argument. * docs/install/index.rst: Update usage command output. * docs/schedule/index.rst: Update lps_gen command example. * docs/speakers/index.rst: Update lps_gen command example. * lps_gen.py (RenderHTML): Change argument template -> template_name. Read template from package. (main): Remove `lp_t` command line argument. * tests/test_lps_gen.py (TestLPS.test_RenderHTML) (TestLPS.test_RenderHTML_sessions_only) (TestLPS.test_RenderHTML_nonexistent_template) (TestLPSTBA.setup, TestLPSpeakers.test_RenderHTML): Update RenderHTML call; pass template name instead of template path. --- docs/install/index.rst | 3 +-- docs/schedule/index.rst | 6 +++--- docs/speakers/index.rst | 4 ++-- lps_gen.py | 29 +++++++++++++---------------- tests/test_lps_gen.py | 16 ++++++---------- 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/docs/install/index.rst b/docs/install/index.rst index 577febb..b3e3e55 100644 --- a/docs/install/index.rst +++ b/docs/install/index.rst @@ -67,10 +67,9 @@ Do:: You must get:: - usage: lps_gen [-h] [-s | -sp] [--ical ICAL] [--version] lp_t lp_md + usage: lps_gen [-h] [-s | -sp] [--ical ICAL] [--version] lp_md positional arguments: - lp_t Path to the LP template. lp_md Path to the LP markdown. optional arguments: diff --git a/docs/schedule/index.rst b/docs/schedule/index.rst index 0e0d2ff..691abf0 100644 --- a/docs/schedule/index.rst +++ b/docs/schedule/index.rst @@ -188,11 +188,11 @@ Generate HTML from Markdown ~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: - $ lps_gen -s path/to/lp-sch.jinja2 path/to/lp-sch.md > path/to/program-schedule.html + $ lps_gen -s path/to/lp-sch.md > path/to/program-schedule.html or:: - $ lps_gen --schedule path/to/lp-schedule.jinja2 path/to/lp-sch.md > path/to/program-schedule.html + $ lps_gen --schedule path/to/lp-sch.md > path/to/program-schedule.html iCal export @@ -201,7 +201,7 @@ iCal export The ``--ical`` switch enables iCal export while generating LP schedule:: - $ lps_gen -s --ical 2016 path/to/lp-schedule.jinja2 path/to/lp-schedule.md > path/to/program-schedule.html + $ lps_gen -s --ical 2016 path/to/lp-schedule.md > path/to/program-schedule.html The year of the conference must be given as an argument to the ``--ical`` switch. diff --git a/docs/speakers/index.rst b/docs/speakers/index.rst index f98dbdb..8add74e 100644 --- a/docs/speakers/index.rst +++ b/docs/speakers/index.rst @@ -63,11 +63,11 @@ Generate HTML from Markdown ~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: - $ lps_gen -sp path/to/lp-speakers.jinja2 path/to/lp-speakers.md > path/to/speakers-content.html + $ lps_gen -sp path/to/lp-speakers.md > path/to/speakers-content.html or:: - $ lps_gen --speakers path/to/lp-speakers.jinja2 path/to/lp-speakers.md > path/to/speakers-content.html + $ lps_gen --speakers path/to/lp-speakers.md > path/to/speakers-content.html If you run into issues, ask for :ref:`help `. diff --git a/lps_gen.py b/lps_gen.py index 82d20df..48a82a8 100644 --- a/lps_gen.py +++ b/lps_gen.py @@ -604,22 +604,19 @@ class LPSpeakersMarkdown(Markdown): return lpspeakers_dict -def RenderHTML(lp_dict, template): +def RenderHTML(lp_dict, template_name): """Renders LP schedule/speakers in HTML from a python dictionary. Returns the HTML as a string. """ - env = Environment(loader=FileSystemLoader(path.dirname(template)), - trim_blocks=True, lstrip_blocks=True) + template_content = template_read(template_name) + if not template_content: + exit('Unable to read {} template'.format(template_name)) - template_name = path.basename(template) - template = None - - try: - template = env.get_template(template_name) - except TemplateNotFound as e: - print('Template {} not found.'.format(template_name)) - exit(1) + template = Environment( + trim_blocks=True, + lstrip_blocks=True + ).from_string(template_content) lp_html = template.render(lp_dict=lp_dict) @@ -642,26 +639,26 @@ def main(): version='lpschedule-generator version {}' .format(__version__), help="Show version number and exit.") - parser.add_argument("lp_t", - help="Path to the LP template.") parser.add_argument("lp_md", help="Path to the LP markdown.") args = parser.parse_args() - lp_template = args.lp_t lp_md_content = read_file(path.abspath(args.lp_md)) - if path.exists(lp_template) and lp_md_content: + if lp_md_content: + template_name = '' if args.schedule: markdown = LPSMarkdown() + template_name = 'schedule' elif args.speakers: markdown = LPSpeakersMarkdown() + template_name = 'speakers' else: parser.error('No action requested, add -s or -sp switch') lp_dict = markdown(lp_md_content) - lp_html = RenderHTML(lp_dict, lp_template) + lp_html = RenderHTML(lp_dict, template_name) if args.ical and args.schedule: LPiCal(lp_dict, args.ical).to_ical() diff --git a/tests/test_lps_gen.py b/tests/test_lps_gen.py index 81a3ab6..9551e18 100644 --- a/tests/test_lps_gen.py +++ b/tests/test_lps_gen.py @@ -557,7 +557,7 @@ class TestLPS(object): def test_RenderHTML(self): """Testing `RenderHTML` function with LP schedule """ - lps_html = RenderHTML(self.lps_dict, self.SCH_TEMPLATE) + lps_html = RenderHTML(self.lps_dict, 'schedule') print(lps_html) # TODO: Scrape and test html output @@ -568,19 +568,15 @@ class TestLPS(object): 'lp-sch-sessions-only.md')) lps_html = RenderHTML(self.markdown(md_content), - self.SCH_TEMPLATE) + 'schedule') print(lps_html) # TODO: Scrape and test html output @raises(SystemExit) def test_RenderHTML_nonexistent_template(self): """Testing `RenderHTML` function - LP schedule - ith non-existent template """ - with mock.patch('sys.stdout', new_callable=StringIO) as out: - nonexistent_template = 'lpsch-template.null' - - lps_html = RenderHTML(self.lps_dict, nonexistent_template) - expected_out = 'Template %s not found.\n' % template_name - assert out.getvalue() == expected_out + with mock.patch('sys.stderr', new_callable=StringIO) as out: + lps_html = RenderHTML(self.lps_dict, 'nonexistent') def teardown(self): @@ -628,7 +624,7 @@ class TestLPSTBA(object): """Runs before each test in this class. """ - lp_html = RenderHTML(self.lps_dict, self.SCH_TEMPLATE) + lp_html = RenderHTML(self.lps_dict, 'schedule') self.soup = BeautifulSoup(lp_html, 'html.parser') @@ -969,7 +965,7 @@ class TestLPSpeakers(object): def test_RenderHTML(self): """Testing `RenderHTML` function with LP speakers """ - lps_html = RenderHTML(self.lpspeakers_dict, self.SPEAKERS_TEMPLATE) + lps_html = RenderHTML(self.lpspeakers_dict, 'speakers') print(lps_html) # TODO: Scrape and test html output. -- cgit v1.2.3