feat: integrate message to worker
This commit is contained in:
parent
ca0f498d4e
commit
9c05ef1551
33
bopytex/message.py
Normal file
33
bopytex/message.py
Normal file
@ -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
|
@ -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)
|
||||
|
||||
|
@ -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])
|
||||
|
||||
try:
|
||||
with open(output, "w") as out:
|
||||
out.write(tpl2tex(template, metas=args))
|
||||
yield f"GENERATE - {deps[0]} to {output}"
|
||||
|
||||
return Message(0, [f"GENERATE - {deps[0]} to {output}"], [])
|
||||
|
||||
except Exception as e:
|
||||
return Message(0, [], [e])
|
||||
|
||||
|
||||
def tpl2tex(template: Template, metas: dict = {}) -> str:
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"]
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user