Skip to content

Commit

Permalink
Fix type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Oct 5, 2024
1 parent 29816d4 commit 0b63fdd
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/sanescansrv/generate_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@
CORE: Final = SOURCE_ROOT / "src" / "sanescansrv"

TEMPLATE_FOLDER: Final = CORE / "templates"
TEMPLATE_FUNCTIONS: dict[str, Callable[[], str]] = {}
TEMPLATE_FUNCTIONS: dict[Path, Callable[[], str]] = {}
STATIC_FOLDER: Final = CORE / "static"
STATIC_FUNCTIONS: dict[str, Callable[[], str]] = {}
STATIC_FUNCTIONS: dict[Path, Callable[[], str]] = {}


def save_content(path: str, content: str) -> None:
def save_content(path: Path, content: str) -> None:
"""Save content to given path."""
with open(path, "w", encoding="utf-8") as fp:
fp.write(content)
fp.write("\n")
path.write_text(content + "\n", encoding="utf-8", newline="\n")
print(f"Saved content to {path}")


Expand Down Expand Up @@ -480,26 +478,25 @@ def generate_scan_status_get() -> str:
return template(title, body, head=head)


def matches_disk_files(new_files: dict[str, str]) -> bool:
def matches_disk_files(new_files: dict[Path, str]) -> bool:
"""Return if all new file contents match old file contents.
Copied from src/trio/_tools/gen_exports.py, dual licensed under
MIT and APACHE2.
"""
for path, new_source in new_files.items():
path_obj = Path(path)
if not path_obj.exists():
if not path.exists():
return False
# Strip trailing newline `save_content` adds.
old_source = path_obj.read_text(encoding="utf-8")[:-1]
old_source = path.read_text(encoding="utf-8")[:-1]
if old_source != new_source:
return False
return True


def process(do_test: bool) -> int:
"""Generate all page templates and static files. Return exit code."""
new_files: dict[str, str] = {}
new_files: dict[Path, str] = {}
for filename, function in TEMPLATE_FUNCTIONS.items():
new_files[filename] = function()
for filename, function in STATIC_FUNCTIONS.items():
Expand All @@ -513,7 +510,9 @@ def process(do_test: bool) -> int:
return 1
elif not matches_disk:
for path, new_source in new_files.items():
# types: arg-type error: Argument 1 to "save_content" has incompatible type "Path"; expected "str"
save_content(path, new_source)
# types: ^^^^
print("Regenerated sources successfully.")
# With pre-commit integration, show that we edited files.
return 1
Expand Down

0 comments on commit 0b63fdd

Please sign in to comment.