Skip to content

Commit ece2e38

Browse files
committed
refactor: use pathlib for files handling
1 parent 9ffec7e commit ece2e38

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/pyobsplot/obsplot.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -444,37 +444,36 @@ def typst_render(
444444

445445
with tempfile.TemporaryDirectory() as tmpdirname:
446446
tmpdir = Path(tmpdirname)
447+
jsdom_file = tmpdir / "jsdom.html"
447448
input_file = tmpdir / "input.typ"
448449
output_file = tmpdir / f"out.{format}"
449450

450451
# Write HTML jsdom output to file
451-
with open(tmpdir / "jsdom.html", "w") as jsdom_out:
452-
jsdom_out.write(str(figure.data))
452+
jsdom_file.write_text(str(figure.data))
453453
# Copy typst template
454454
shutil.copy(bundler_output_dir / "template.typ", tmpdir / "template.typ")
455455
# Create the typst input file
456-
with open(input_file, "w") as typst_file:
457-
typst_content = '#import "template.typ": obsplot\n#show: obsplot("jsdom.html",'
458-
if "margin" in options:
459-
value = options["margin"]
460-
if value.isnumeric():
461-
value = value + "pt"
462-
typst_content += f"margin: {value},"
463-
if "font" in options:
464-
value = options["font"]
465-
typst_content += f'font-family: "{value}",'
466-
if "scale" in options:
467-
value = options["scale"]
468-
typst_content += f"scale: {value},"
469-
if "legend-padding" in options:
470-
value = options["legend-padding"]
471-
if value.isnumeric():
472-
value = value + "pt"
473-
typst_content += f"legend-padding: {value},"
474-
typst_content += ")"
475-
typst_file.write(typst_content)
476-
477-
typst.compile(input_file, output=output_file, ppi=100, format=format) # type: ignore
456+
typst_content = '#import "template.typ": obsplot\n#show: obsplot("jsdom.html",'
457+
if "margin" in options:
458+
value = options["margin"]
459+
if value.isnumeric():
460+
value = value + "pt"
461+
typst_content += f"margin: {value},"
462+
if "font" in options:
463+
value = options["font"]
464+
typst_content += f'font-family: "{value}",'
465+
if "scale" in options:
466+
value = options["scale"]
467+
typst_content += f"scale: {value},"
468+
if "legend-padding" in options:
469+
value = options["legend-padding"]
470+
if value.isnumeric():
471+
value = value + "pt"
472+
typst_content += f"legend-padding: {value},"
473+
typst_content += ")"
474+
input_file.write_text(typst_content)
475+
476+
typst.compile(input=input_file, output=output_file, ppi=100, format=format) # pyright: ignore[reportPossiblyUnboundVariable]
478477

479478
mode = "rb" if format in ["png", "pdf"] else "r"
480479
with open(output_file, mode) as f:

0 commit comments

Comments
 (0)