diff --git a/bopytex/message.py b/bopytex/message.py new file mode 100644 index 0000000..bdfb69c --- /dev/null +++ b/bopytex/message.py @@ -0,0 +1,33 @@ +class Message(): + def __init__(self, status, out, err): + self._status = status + self._out = out + self._err = err + + @property + def status(self): + return self._status + + @property + def out(self): + return self._out + + @property + def err(self): + return self._err + +class SubprocessMessage(Message): + def __init__(self, process): + self._process = process + + @property + def status(self): + return self._process.wait() + + @property + def out(self): + return self._process.stdout + + @property + def err(self): + return self._process.stderr diff --git a/bopytex/worker/compile.py b/bopytex/worker/compile.py index 24a9531..256c0f5 100644 --- a/bopytex/worker/compile.py +++ b/bopytex/worker/compile.py @@ -1,4 +1,5 @@ import subprocess +from ..message import SubprocessMessage def latexmk(args: dict, deps, output): @@ -8,5 +9,5 @@ def latexmk(args: dict, deps, output): stderr=subprocess.PIPE, universal_newlines=True ) - yield from compile_process.stderr + return SubprocessMessage(compile_process) diff --git a/bopytex/worker/generate.py b/bopytex/worker/generate.py index ff80f76..b6a9cf3 100644 --- a/bopytex/worker/generate.py +++ b/bopytex/worker/generate.py @@ -1,12 +1,20 @@ from jinja2.environment import Template +from bopytex.message import Message + def generate(args, deps, output): env = args["jinja2"]["environment"] template = env.get_template(deps[0]) - with open(output, "w") as out: - out.write(tpl2tex(template, metas=args)) - yield f"GENERATE - {deps[0]} to {output}" + + try: + with open(output, "w") as out: + out.write(tpl2tex(template, metas=args)) + + return Message(0, [f"GENERATE - {deps[0]} to {output}"], []) + + except Exception as e: + return Message(0, [], [e]) def tpl2tex(template: Template, metas: dict = {}) -> str: diff --git a/bopytex/worker/join_pdf.py b/bopytex/worker/join_pdf.py index 45927f1..2a44f04 100644 --- a/bopytex/worker/join_pdf.py +++ b/bopytex/worker/join_pdf.py @@ -1,5 +1,7 @@ import subprocess +from bopytex.message import SubprocessMessage + def pdfjam(args: dict, deps, output): joining_process = subprocess.Popen( ["pdfjam"] + deps + ["-o", output], @@ -7,11 +9,5 @@ def pdfjam(args: dict, deps, output): stderr=subprocess.PIPE, universal_newlines=True, ) - # exit_code is always 66... - exit_code = joining_process.wait() - - if exit_code == 0: - yield "pdfjam success" - else: - yield "pdfjam failed" + return SubprocessMessage(joining_process) diff --git a/test/worker/test_compile.py b/test/worker/test_compile.py index 7e1c96e..edd2fc3 100644 --- a/test/worker/test_compile.py +++ b/test/worker/test_compile.py @@ -22,14 +22,14 @@ extra parameters or packages included. return source -def test_compile(tex_path): +def test_latexmk(tex_path): tmp_path = tex_path.parent os.chdir(tmp_path) texfile = str(tex_path.name) output = "source.pdf" - for err in latexmk({}, [texfile], "source.pdf"): - assert 0 + message = latexmk({}, [texfile], "source.pdf") + assert message.status == 0 assert Path(output).exists diff --git a/test/worker/test_generate.py b/test/worker/test_generate.py index ab90ba7..fbd3baf 100644 --- a/test/worker/test_generate.py +++ b/test/worker/test_generate.py @@ -34,12 +34,15 @@ def test_generate(template_path, jinja2_env): template = str(template_path.name) output = "output" - result = next(generate( + message = generate( args={"a": 2, "jinja2": {"environment": jinja2_env}}, deps=[template], output=output, - )) - assert result == "GENERATE - template.j2 to output" + ) + print(message.err) + assert message.status == 0 + assert message.out == ["GENERATE - template.j2 to output"] + with open(output, "r") as out: lines = out.readlines() assert lines == ["Plop 2"] diff --git a/test/worker/test_join_pdf.py b/test/worker/test_join_pdf.py index 5edd7a4..26ae726 100644 --- a/test/worker/test_join_pdf.py +++ b/test/worker/test_join_pdf.py @@ -31,9 +31,9 @@ def test_join_pdf(multiple_pdf): output = "joined.pdf" - for msg in pdfjam({"pwd": Path.cwd()}, deps, output): - assert msg == "pdfjam success" + message = pdfjam({"pwd": Path.cwd()}, deps, output) + assert message.status == 0 assert Path(output).exists() @@ -45,7 +45,7 @@ def test_join_pdf_failed(multiple_pdf): output = "joined.pdf" - for msg in pdfjam({"pwd": Path.cwd()}, deps, output): - assert msg == "pdfjam failed" + message = pdfjam({"pwd": Path.cwd()}, deps, output) + assert message.status == 66 assert not Path(output).exists()