mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 13:34:24 -05:00
feat(anki): force scan of single file/directory
This commit is contained in:
@@ -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():
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user