perf(anki): skip unchanged files using file hashes

This commit is contained in:
arne314
2024-12-27 22:11:05 +01:00
parent c3331683e5
commit 348a889521
7 changed files with 78 additions and 24 deletions

View File

@@ -1,24 +1,29 @@
import os.path
import hashlib
from pathlib import Path
from typing import List
import tree_sitter
class FileHandler:
file_path: str
file_path: Path
file_content: List[str]
def __init__(self, path):
def __init__(self, path: Path):
self.file_path = path
self.read()
@property
def directory_path(self) -> str:
return os.path.dirname(self.file_path)
def directory_path(self) -> Path:
return self.file_path.parent
def get_bytes(self) -> bytes:
return bytes("".join(self.file_content), encoding="utf-8")
def get_file_hash(self) -> str:
return hashlib.md5("".join(self.file_content).encode(), usedforsecurity=False).hexdigest()
def get_node_content(self, node: tree_sitter.Node, remove_outer=False):
content = "".join(
self.file_content[node.start_point.row:node.end_point.row + 1]
@@ -37,9 +42,9 @@ class FileHandler:
self.file_content = new_lines
def read(self):
with open(self.file_path, encoding="utf-8") as f:
with self.file_path.open(encoding="utf-8") as f:
self.file_content = f.readlines()
def write(self):
with open(self.file_path, "w", encoding="utf-8") as f:
with self.file_path.open("w", encoding="utf-8") as f:
f.writelines(self.file_content)