Skip to content

Commit 89b32f0

Browse files
refactor: ♻️ use pathlib for paths
1 parent 5a2e752 commit 89b32f0

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

build.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from dataclasses import dataclass
1111
from enum import IntFlag, StrEnum
1212
from io import TextIOWrapper
13+
from pathlib import Path
1314
from typing import Literal, cast
1415

1516
type HeaderLevel = Literal[1, 2, 3]
@@ -42,12 +43,12 @@ def from_str(ctx: str) -> Context:
4243
raise RuntimeError(f"unknown context: {ctx!r}")
4344

4445
@staticmethod
45-
def from_path(path: str) -> Context:
46-
if path == "README.md":
46+
def from_path(path: Path) -> Context:
47+
if path.name == "README.md":
4748
return Context.README
48-
if path == "description.txt":
49+
if path.name == "description.txt":
4950
return Context.DESCRIPTION
50-
if path == "description_workshop.txt":
51+
if path.name == "description_workshop.txt":
5152
return Context.WORKSHOP
5253
raise RuntimeError(f"unknown contextual file: {path!r}")
5354

@@ -378,27 +379,34 @@ def custom_decoder(dct):
378379
return dct
379380

380381

382+
TMOD = Path("tmod.json")
383+
DESCRIPTION = Path("description.txt")
384+
DESCRIPTION_WORKSHOP = Path("description_workshop.txt")
385+
README = Path("README.md")
386+
BUILD = Path("build.txt")
387+
388+
381389
def main() -> None:
382390
if len(sys.argv) == 3 and sys.argv[1] == "tag":
383391
tag = sys.argv[-1]
384-
with open("tmod.json", encoding="utf-8") as file:
392+
with TMOD.open(encoding="utf-8") as file:
385393
tmod = json.load(file)
386394
tmod["version"] = tag
387-
with open("tmod.json", "w", encoding="utf-8") as file:
395+
with TMOD.open("w", encoding="utf-8") as file:
388396
json.dump(tmod, file, indent=2)
389397
file.write("\n")
390398

391-
with open("tmod.json", encoding="utf-8") as tmod_json:
399+
with TMOD.open(encoding="utf-8") as tmod_json:
392400
tmod = ModSchema.load(tmod_json)
393401

394402
if tmod.description:
395-
for path in ("description.txt", "description_workshop.txt"):
403+
for path in (DESCRIPTION, DESCRIPTION_WORKSHOP):
396404
ctx = Context.from_path(path)
397405
with open(path, "w", encoding="utf-8") as file:
398406
file.write(tmod.description.render(ctx, tmod))
399407

400408
readme_header_contents: str | None = None
401-
with open("README.md", encoding="utf-8") as file:
409+
with README.open(encoding="utf-8") as file:
402410
contents = file.read()
403411
header_content_start = contents.find("<!-- #region README Header -->")
404412
header_content_end = contents.find("<!-- #endregion -->")
@@ -408,7 +416,7 @@ def main() -> None:
408416
+ len("<!-- #endregion -->")
409417
]
410418

411-
with open("README.md", "w", encoding="utf-8") as file:
419+
with README.open("w", encoding="utf-8") as file:
412420
ctx = Context.README
413421
if readme_header_contents:
414422
file.write(Title().render(ctx, tmod) + "\n\n")
@@ -417,7 +425,7 @@ def main() -> None:
417425
else:
418426
file.write(tmod.description.render(ctx, tmod))
419427

420-
with open("build.txt", "w", encoding="utf-8") as file:
428+
with BUILD.open("w", encoding="utf-8") as file:
421429
file.write(tmod.render())
422430

423431

0 commit comments

Comments
 (0)