From 4f335a919fc7224b1f52a90fee823c1955bd1809 Mon Sep 17 00:00:00 2001 From: arne314 <73391160+arne314@users.noreply.github.com> Date: Tue, 31 Dec 2024 14:51:17 +0100 Subject: [PATCH] feat(anki): force scan of single file/directory --- src/anki/main.py | 15 +++++++++------ src/anki/parser.py | 24 ++++++++++++++++++------ src/anki/typst_compiler.py | 4 ++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/anki/main.py b/src/anki/main.py index 1b7c908..7a5f5e2 100644 --- a/src/anki/main.py +++ b/src/anki/main.py @@ -1,5 +1,6 @@ import asyncio import os +from pathlib import Path from typing_extensions import Annotated import typer @@ -11,7 +12,7 @@ from anki.typst_compiler import TypstCompiler cli = typer.Typer(name="typstar-anki") -async def export_flashcards(root_dir, clear_cache, typst_cmd, anki_url, anki_key): +async def export_flashcards(root_dir, force_scan, clear_cache, typst_cmd, anki_url, anki_key): parser = FlashcardParser() compiler = TypstCompiler(root_dir, typst_cmd) api = AnkiConnectApi(anki_url, anki_key) @@ -19,7 +20,7 @@ async def export_flashcards(root_dir, clear_cache, typst_cmd, anki_url, anki_key # parse flashcards if clear_cache: parser.clear_file_hashes() - flashcards = parser.parse_directory(root_dir) + flashcards = parser.parse_directory(root_dir, force_scan) # async typst compilation await compiler.compile_flashcards(flashcards) @@ -36,14 +37,16 @@ async def export_flashcards(root_dir, clear_cache, typst_cmd, anki_url, anki_key @cli.command() def cmd(root_dir: Annotated[ - str, typer.Option(help="Directory scanned for flashcards and passed over to typst compile command")] = os.getcwd(), + Path, typer.Option(help="Directory scanned for flashcards and passed over to typst compile command")] = os.getcwd(), + force_scan: Annotated[Path, typer.Option(help="File/directory to scan for flashcards while ignoring stored " + "file hashes (e.g. on preamble change)")] = None, + clear_cache: Annotated[bool, typer.Option(help="Clear all stored file hashes (more aggressive than force-scan " + "as it clears hashes regardless of their path)")] = False, typst_cmd: Annotated[str, typer.Option(help="Typst command used for flashcard compilation")] = "typst", - clear_cache: Annotated[bool, typer.Option(help="Clear stored file hashes and force compilation and " - "push of all flashcards (e.g. on preamble change)")] = False, anki_url: Annotated[str, typer.Option(help="Url for Anki-Connect")] = "http://127.0.0.1:8765", anki_key: Annotated[str, typer.Option(help="Api key for Anki-Connect")] = None, ): - asyncio.run(export_flashcards(root_dir, clear_cache, typst_cmd, anki_url, anki_key)) + asyncio.run(export_flashcards(root_dir, force_scan, clear_cache, typst_cmd, anki_url, anki_key)) def main(): diff --git a/src/anki/parser.py b/src/anki/parser.py index 5e22618..d7b8178 100644 --- a/src/anki/parser.py +++ b/src/anki/parser.py @@ -98,26 +98,38 @@ class FlashcardParser: cards.append(card) return cards - def parse_directory(self, root_dir): - print(f"Parsing flashcards in {root_dir}...") - root_dir = Path(root_dir) + def parse_directory(self, scan_dir, force_scan: Path = None): + single_file = None + is_force_scan = force_scan is not None + if is_force_scan: + if force_scan.is_file(): + single_file = force_scan + scan_dir = force_scan.parent + else: + scan_dir = force_scan + + print(f"Parsing flashcards in {scan_dir if single_file is None else single_file} ...") preambles = {} flashcards = [] @cache def get_preamble(path: Path) -> str | None: - while path != root_dir: + while path != scan_dir.parent: if preamble := preambles.get(path): return preamble path = path.parent - for file in sorted(glob.glob(f"{root_dir}/**/**.typ", include_hidden=True, recursive=True)): + for file in sorted(glob.glob(f"{scan_dir}/**/**.typ", include_hidden=True, recursive=True)): file = Path(file) if file.name == ".anki.typ": preambles[file.parent] = file.read_text(encoding="utf-8") continue + if single_file is not None and file != single_file: + continue + fh = FileHandler(file) - if self._hash_changed(fh): + file_changed = self._hash_changed(fh) + if is_force_scan or file_changed: cards = self._parse_file(fh, get_preamble(file.parent)) self.file_handlers.append((fh, cards)) flashcards.extend(cards) diff --git a/src/anki/typst_compiler.py b/src/anki/typst_compiler.py index 505e640..6b89bd4 100644 --- a/src/anki/typst_compiler.py +++ b/src/anki/typst_compiler.py @@ -25,10 +25,10 @@ class TypstCompilationError(ValueError): class TypstCompiler: preamble: str typst_cmd: str - typst_root_dir: str + typst_root_dir: Path max_processes: int - def __init__(self, typst_root_dir: str, typst_cmd: str): + def __init__(self, typst_root_dir: Path, typst_cmd: str): self.typst_cmd = typst_cmd self.typst_root_dir = typst_root_dir self.max_processes = round(os.cpu_count() * 1.5)