♻️ Refactor implementation for future new parsers additions (#16)

This commit is contained in:
2025-10-01 20:38:30 +02:00
committed by GitHub
parent a9d753ed25
commit a1e97fc1b2
9 changed files with 179 additions and 104 deletions

View File

@@ -1,14 +1,11 @@
# Copyright (c) Paillat-dev
# SPDX-License-Identifier: MIT
import hashlib
import json
import pathlib
import sys
from typing import Any
from download_build import dowload
from extract import extract_emojis_from_str
from parsers import PARSERS
def main() -> None:
@@ -16,34 +13,43 @@ def main() -> None:
build_path = pathlib.Path.cwd() / "build"
build_path.mkdir(exist_ok=True)
out_path = build_path / "emojis.json"
hash_path = build_path / "hash.txt"
changes: bool = False
if not out_path.exists():
out_path.touch()
build_download: str = dowload()
for parser_cls in PARSERS:
parser = parser_cls(build_download)
out_path = build_path / parser.FILE_NAME
hash_path = build_path / f".{parser.FILE_NAME}.hash"
if hash_path.exists():
with hash_path.open("r", encoding="utf-8") as hash_file:
current_hash = hash_file.read()
else:
current_hash = ""
if not out_path.exists():
out_path.touch()
new: dict[Any, Any] = extract_emojis_from_str(dowload()) # pyright: ignore[reportExplicitAny]
new_dump = json.dumps(new, indent=4, ensure_ascii=False).encode("utf-8")
new_hash = hashlib.sha256(string=new_dump).hexdigest()
if hash_path.exists():
with hash_path.open("r", encoding="utf-8") as hash_file:
current_hash = hash_file.read()
else:
current_hash = ""
if current_hash == new_hash:
print("No changes")
new_dump: bytes
new_hash: str
new_dump, new_hash = parser()
if current_hash == new_hash:
print(f"No changes for {parser.FILE_NAME}")
continue
with out_path.open("wb") as out_file:
out_file.write(new_dump)
with hash_path.open("w", encoding="utf-8") as hash_file:
hash_file.write(new_hash)
changes = True
print("Updated emojis.json")
if not changes:
sys.exit(3) # No changes
with out_path.open("wb") as out_file:
out_file.write(new_dump)
with hash_path.open("w", encoding="utf-8") as hash_file:
hash_file.write(new_hash)
print("Updated emojis.json")
if __name__ == "__main__":
main()