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,24 @@
import asyncio
import glob
import os
from typing_extensions import Annotated
import typer
from anki.anki_api import AnkiConnectApi
from anki.file_handler import FileHandler
from anki.parser import FlashcardParser
from anki.typst_compiler import TypstCompiler
cli = typer.Typer(name="typstar-anki")
async def export_flashcards(root_dir, typst_cmd, anki_url, anki_key):
async def export_flashcards(root_dir, clear_cache, typst_cmd, anki_url, anki_key):
parser = FlashcardParser()
compiler = TypstCompiler(root_dir, typst_cmd)
api = AnkiConnectApi(anki_url, anki_key)
# parse flashcards
if clear_cache:
parser.clear_file_hashes()
flashcards = parser.parse_directory(root_dir)
# async typst compilation
@@ -30,6 +30,7 @@ async def export_flashcards(root_dir, typst_cmd, anki_url, anki_key):
finally:
# write id updates to files
parser.update_ids_in_source()
parser.save_file_hashes()
print("Done")
@@ -37,10 +38,12 @@ async def export_flashcards(root_dir, typst_cmd, anki_url, anki_key):
def cmd(root_dir: Annotated[
str, typer.Option(help="Directory scanned for flashcards and passed over to typst compile command")] = os.getcwd(),
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, typst_cmd, anki_url, anki_key))
asyncio.run(export_flashcards(root_dir, clear_cache, typst_cmd, anki_url, anki_key))
def main():