chore: Refactor build.py to use file context managers for reading and writing

This commit is contained in:
Théophile Diot 2024-07-31 10:33:31 +01:00
parent 52130ed6e8
commit b599fdae47
No known key found for this signature in database
GPG key ID: FA995104A0BA376A

View file

@ -100,13 +100,16 @@ def move_template(folder: Path, target_folder: Path):
if "global-config" in file.parts or "jobs" in file.parts or "services" in file.parts:
base_html = base_html.replace("data_server_builder[1:-1]", "data_server_builder")
content = file.read_text()
with file.open("r") as f:
content = f.read()
content = sub(r'(href|src)="\/(css|js|img|favicon|assets|js)\/[^<]*?(?=<|\/>)', format_template, content)
# get the content before <body>
content = content[: content.index("<body>")] + base_html
# write the new content
file.write_text(content)
with file.open("w") as f:
f.write(content)
if target_folder.joinpath(f"{file.parent.name}.html").exists():
target_folder.joinpath(f"{file.parent.name}.html").unlink()