Commit 908d4702 by dorp

renamed meta template and added static variables

parent e280210d
......@@ -3,23 +3,27 @@ import shutil
from pathlib import Path
from common_helper_process import execute_shell_command
from pdf_generator.tex_generation.template_engine import TemplateEngine
from pdf_generator.tex_generation.template_engine import (
LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE, PLUGIN_TEMPLATE_BLUEPRINT, TemplateEngine
)
PDF_NAME = Path(MAIN_TEMPLATE).with_suffix('.pdf').name
def execute_latex(tmp_dir):
current_dir = os.getcwd()
os.chdir(tmp_dir)
execute_shell_command('env buf_size=1000000 pdflatex main.tex')
execute_shell_command('env buf_size=1000000 pdflatex {}'.format(MAIN_TEMPLATE))
os.chdir(current_dir)
def copy_fact_image(target):
shutil.copy(str(Path(__file__).parent / 'templates' / 'fact_logo.png'), str(Path(target) / 'fact_logo.png'))
shutil.copy(str(Path(__file__).parent / 'templates' / LOGO_FILE), str(Path(target) / LOGO_FILE))
def render_analysis_templates(engine, analysis):
return [
('{}.tex'.format(analysis_plugin), engine.render_analysis_template(analysis_plugin, analysis[analysis_plugin])) for analysis_plugin in analysis
(PLUGIN_TEMPLATE_BLUEPRINT.format(analysis_plugin), engine.render_analysis_template(analysis_plugin, analysis[analysis_plugin])) for analysis_plugin in analysis
]
......@@ -33,15 +37,15 @@ def compile_pdf(meta_data, tmp_dir):
copy_fact_image(tmp_dir)
execute_latex(tmp_dir)
target_path = Path(tmp_dir, create_report_filename(meta_data))
shutil.move(str(Path(tmp_dir, 'main.pdf')), str(target_path))
shutil.move(str(Path(tmp_dir, PDF_NAME)), str(target_path))
return target_path
def create_templates(analysis, meta_data, tmp_dir):
engine = TemplateEngine(tmp_dir=tmp_dir)
Path(tmp_dir, 'main.tex').write_text(engine.render_main_template(analysis=analysis, meta_data=meta_data))
Path(tmp_dir, 'meta.tex').write_text(engine.render_meta_template(meta_data))
Path(tmp_dir, MAIN_TEMPLATE).write_text(engine.render_main_template(analysis=analysis, meta_data=meta_data))
Path(tmp_dir, META_TEMPLATE).write_text(engine.render_meta_template(meta_data))
for filename, rendered_template in render_analysis_templates(engine=engine, analysis=analysis):
Path(tmp_dir, filename).write_text(rendered_template)
render_test.tex
\ No newline at end of file
render_test.tex
\ No newline at end of file
......@@ -9,6 +9,10 @@ import jinja2
from common_helper_files import human_readable_file_size
GENERIC_TEMPLATE = 'generic.tex'
MAIN_TEMPLATE = 'main.tex'
META_TEMPLATE = 'meta.tex'
PLUGIN_TEMPLATE_BLUEPRINT = '{}.tex'
LOGO_FILE = 'fact_logo.png'
def render_number_as_size(number, verbose=True):
......@@ -140,16 +144,16 @@ class TemplateEngine:
self._tmp_dir = tmp_dir
def render_main_template(self, analysis, meta_data):
template = self._environment.get_template('main.tex')
template = self._environment.get_template(MAIN_TEMPLATE)
return template.render(analysis=analysis, meta_data=meta_data)
def render_meta_template(self, meta_data):
template = self._environment.get_template('meta_data.tex')
template = self._environment.get_template(META_TEMPLATE)
return template.render(meta_data=meta_data)
def render_analysis_template(self, plugin, analysis):
try:
template = self._environment.get_template('{}.tex'.format(plugin))
template = self._environment.get_template(PLUGIN_TEMPLATE_BLUEPRINT.format(plugin))
except jinja2.TemplateNotFound:
logging.warning('Falling back on generic template for {}'.format(plugin))
template = self._environment.get_template(GENERIC_TEMPLATE)
......
......@@ -3,7 +3,8 @@ from pathlib import Path
import pytest
from pdf_generator.generator import (
copy_fact_image, create_report_filename, create_templates, execute_latex, render_analysis_templates
LOGO_FILE, MAIN_TEMPLATE, META_TEMPLATE, PLUGIN_TEMPLATE_BLUEPRINT, copy_fact_image, create_report_filename,
create_templates, execute_latex, render_analysis_templates
)
......@@ -38,7 +39,7 @@ def test_execute_latex(monkeypatch, tmpdir):
def test_copy_fact_image(tmpdir):
copy_fact_image(str(tmpdir))
assert Path(str(tmpdir), 'fact_logo.png').exists()
assert Path(str(tmpdir), LOGO_FILE).exists()
@pytest.mark.parametrize('device_name, pdf_name', [
......@@ -56,7 +57,7 @@ def test_create_analysis_templates():
assert len(templates) == 1
filename, result_code = templates[0]
assert filename == 'test.tex'
assert filename == PLUGIN_TEMPLATE_BLUEPRINT.format('test')
assert result_code == '{"result": "data"}'
......@@ -64,8 +65,8 @@ def test_create_templates(monkeypatch, tmpdir):
monkeypatch.setattr('pdf_generator.generator.TemplateEngine', MockEngine)
create_templates(analysis={'test': {'result': 'data'}}, meta_data={}, tmp_dir=str(tmpdir))
assert Path(str(tmpdir), 'main.tex').exists()
assert Path(str(tmpdir), 'meta.tex').exists()
assert Path(str(tmpdir), 'test.tex').exists()
assert Path(str(tmpdir), MAIN_TEMPLATE).exists()
assert Path(str(tmpdir), META_TEMPLATE).exists()
assert Path(str(tmpdir), PLUGIN_TEMPLATE_BLUEPRINT.format('test')).exists()
assert Path(str(tmpdir), 'test.tex').read_text() == '{"result": "data"}'
assert Path(str(tmpdir), PLUGIN_TEMPLATE_BLUEPRINT.format('test')).read_text() == '{"result": "data"}'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment