From b7352f1961177661ce7de8b5f4c5a754564140dd Mon Sep 17 00:00:00 2001 From: Martin Bernstorff Date: Fri, 27 Oct 2023 18:43:52 +0000 Subject: [PATCH] feat: use functionalpy --- pyproject.toml | 1 + .../exporters/anki/card_types/base.py | 5 ++--- .../exporters/anki/card_types/cloze.py | 3 +-- .../exporters/anki/card_types/qa.py | 3 +-- .../exporters/anki/sync.py | 2 +- .../exporters/url_generators/obsidian_url.py | 2 +- .../note_factories/markdown.py | 2 +- .../note_factories/note.py | 4 ++-- .../prompt_extractors/cloze_extractor.py | 2 +- .../prompt_extractors/prompt.py | 6 ++---- tests/exporters/anki/test_card_converter.py | 14 +++++++------- tests/exporters/anki/test_package_generator.py | 4 ++-- 12 files changed, 22 insertions(+), 26 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ae4f4e1e..e1961c60 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ file = "README.md" content-type = "text/markdown" dependencies = [ + "functionalpy==0.6.0", "misaka==2.1.1", "genanki==0.13.0", "typer==0.9.0", diff --git a/src/personal_mnemonic_medium/exporters/anki/card_types/base.py b/src/personal_mnemonic_medium/exporters/anki/card_types/base.py index 862822d5..e94c3c51 100644 --- a/src/personal_mnemonic_medium/exporters/anki/card_types/base.py +++ b/src/personal_mnemonic_medium/exporters/anki/card_types/base.py @@ -25,7 +25,7 @@ def __init__( self, fields: list[str], source_prompt: Prompt, - url_generator: Callable[[Path, Optional[int]], str] = get_obsidian_url, + url_generator: Callable[[Path, int | None], str] = get_obsidian_url, html_compiler: Callable[[str], str] = compile_field, ): self.markdown_fields = fields @@ -164,8 +164,7 @@ def process_match(m) -> str: # noqa current_stage = re.sub(regex, process_match, current_stage) - for r in results: - yield r + yield from results # Anki seems to hate alt tags :( self.html_fields[i] = re.sub(r'alt="[^"]*?"', "", current_stage) diff --git a/src/personal_mnemonic_medium/exporters/anki/card_types/cloze.py b/src/personal_mnemonic_medium/exporters/anki/card_types/cloze.py index 51df772a..43216480 100644 --- a/src/personal_mnemonic_medium/exporters/anki/card_types/cloze.py +++ b/src/personal_mnemonic_medium/exporters/anki/card_types/cloze.py @@ -1,7 +1,6 @@ import re from collections.abc import Callable from pathlib import Path -from typing import List, Optional import genanki from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard @@ -21,7 +20,7 @@ def __init__( self, fields: list[str], source_prompt: Prompt, - url_generator: Callable[[Path, Optional[int]], str] = get_obsidian_url, + url_generator: Callable[[Path, int | None], str] = get_obsidian_url, html_compiler: Callable[[str], str] = compile_field, ): super().__init__( diff --git a/src/personal_mnemonic_medium/exporters/anki/card_types/qa.py b/src/personal_mnemonic_medium/exporters/anki/card_types/qa.py index 4ecccfa3..67aa6093 100644 --- a/src/personal_mnemonic_medium/exporters/anki/card_types/qa.py +++ b/src/personal_mnemonic_medium/exporters/anki/card_types/qa.py @@ -1,6 +1,5 @@ from collections.abc import Callable from pathlib import Path -from typing import List, Optional import genanki from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard @@ -20,7 +19,7 @@ def __init__( self, fields: list[str], source_prompt: Prompt, - url_generator: Callable[[Path, Optional[int]], str] = get_obsidian_url, + url_generator: Callable[[Path, int | None], str] = get_obsidian_url, html_compiler: Callable[[str], str] = compile_field, ): super().__init__( diff --git a/src/personal_mnemonic_medium/exporters/anki/sync.py b/src/personal_mnemonic_medium/exporters/anki/sync.py index 4996df2b..75096b57 100644 --- a/src/personal_mnemonic_medium/exporters/anki/sync.py +++ b/src/personal_mnemonic_medium/exporters/anki/sync.py @@ -3,7 +3,7 @@ import urllib.request from pathlib import Path from time import sleep -from typing import Any, Dict, List +from typing import Any from genanki import Model, Note from wasabi import Printer diff --git a/src/personal_mnemonic_medium/exporters/url_generators/obsidian_url.py b/src/personal_mnemonic_medium/exporters/url_generators/obsidian_url.py index 43f2f92c..96dd15a1 100644 --- a/src/personal_mnemonic_medium/exporters/url_generators/obsidian_url.py +++ b/src/personal_mnemonic_medium/exporters/url_generators/obsidian_url.py @@ -3,7 +3,7 @@ from typing import Optional -def get_obsidian_url(source_path: Path, line_nr: Optional[int] = None) -> str: +def get_obsidian_url(source_path: Path, line_nr: int | None = None) -> str: """Get the obsidian URI for the source document.""" vault: str = urllib.parse.quote(source_path.parent.name) # type: ignore file: str = urllib.parse.quote(source_path.name) # type: ignore diff --git a/src/personal_mnemonic_medium/note_factories/markdown.py b/src/personal_mnemonic_medium/note_factories/markdown.py index 8b548618..73a3ba4e 100644 --- a/src/personal_mnemonic_medium/note_factories/markdown.py +++ b/src/personal_mnemonic_medium/note_factories/markdown.py @@ -31,7 +31,7 @@ def get_and_append_new_uuid(self, file_path: Path) -> str: def get_note_id(self, file_string: str) -> str: return re.findall(r"