diff --git a/personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py b/personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py index 4f8e73cb..92d3eff4 100644 --- a/personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py +++ b/personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py @@ -63,11 +63,9 @@ def import_package( tmp_write_dir: Path, tmp_read_dir: Path, ) -> None: - apkg_name = datetime.datetime.now().strftime( - "%Y-%m-%d-%H-%M-%S" - ) - output_path = tmp_write_dir / apkg_name - package.write_to_file(output_path) # type: ignore + apkg_name = f"{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.apkg" + write_path = tmp_write_dir / apkg_name + package.write_to_file(write_path) # type: ignore read_path = tmp_read_dir / apkg_name try: @@ -75,13 +73,11 @@ def import_package( AnkiConnectCommand.IMPORT_PACKAGE, path=str(read_path) ) print(f"Imported from {read_path}!") + write_path.unlink() except Exception: print(f"""Unable to sync from {read_path}.""") traceback.print_exc() - # Delete the package after importing it - output_path.unlink() - def delete_notes(self, note_ids: Sequence[int]) -> None: self._invoke(AnkiConnectCommand.DELETE_NOTES, notes=note_ids) diff --git a/personal_mnemonic_medium/v2/data_access/test_ankiconnect.py b/personal_mnemonic_medium/v2/data_access/test_ankiconnect.py index 043ff4f4..33159fd1 100644 --- a/personal_mnemonic_medium/v2/data_access/test_ankiconnect.py +++ b/personal_mnemonic_medium/v2/data_access/test_ankiconnect.py @@ -33,8 +33,7 @@ class MockNoteInfo(NoteInfo): ) class TestAnkiConnectGateway: gateway = AnkiConnectGateway( - ankiconnect_url=ANKICONNECT_URL, - deck_name="0. Don't click me::1. Active::Personal Mnemonic Medium", + ankiconnect_url=ANKICONNECT_URL, deck_name="Test deck" ) def test_import_package(self): @@ -44,9 +43,29 @@ def test_import_package(self): for f in output_path.glob("*.apkg"): f.unlink() - package = genanki.Package( - deck_or_decks=genanki.Deck(deck_id=1, name="Test deck") + deck = genanki.Deck(deck_id=1, name="Test deck") + model = genanki.Model( + model_id=1, + name="Test model", + fields=[{"name": "Question"}, {"name": "Answer"}], + templates=[ + { + "name": "Card 1", + "qfmt": "{{Question}}", + "afmt": "{{FrontSide}}
{{Answer}}", + } + ], ) + + deck.add_model(model) # type: ignore + deck.add_note( # type: ignore + genanki.Note( + model=model, + fields=["Capital of Argentina", "Buenos Aires"], + ) + ) + + package = genanki.Package(deck_or_decks=deck) self.gateway.import_package( package=package, tmp_write_dir=Path("/output"), diff --git a/personal_mnemonic_medium/v2/domain/prompt_destination/anki_connect/ankiconnect_destination.py b/personal_mnemonic_medium/v2/domain/prompt_destination/anki_connect/ankiconnect_destination.py index 437a8aab..c1eb43fd 100644 --- a/personal_mnemonic_medium/v2/domain/prompt_destination/anki_connect/ankiconnect_destination.py +++ b/personal_mnemonic_medium/v2/domain/prompt_destination/anki_connect/ankiconnect_destination.py @@ -14,8 +14,8 @@ NoteInfo, ) from ...prompts.base_prompt import BasePrompt -from ...prompts.cloze_prompt import ClozePromptWithoutDoc -from ...prompts.qa_prompt import QAPromptWithoutDoc +from ...prompts.cloze_prompt import RemoteClozePrompt +from ...prompts.qa_prompt import RemoteQAPrompt from ..base_prompt_destination import PromptDestination from .prompt_converter.anki_prompt_converter import ( AnkiPromptConverter, @@ -37,16 +37,18 @@ def _note_info_to_prompt(self, note_info: NoteInfo) -> BasePrompt: "Question" in note_info.fields and "Answer" in note_info.fields ): - return QAPromptWithoutDoc( + return RemoteQAPrompt( question=note_info.fields["Question"].value, answer=note_info.fields["Answer"].value, add_tags=note_info.tags, + remote_id=str(note_info.noteId), ) if "Text" in note_info.fields: - return ClozePromptWithoutDoc( + return RemoteClozePrompt( text=note_info.fields["Text"].value, add_tags=note_info.tags, + remote_id=str(note_info.noteId), ) raise ValueError( @@ -62,8 +64,6 @@ def get_all_prompts(self) -> Sequence[BasePrompt]: def _delete_prompts(self, prompts: Sequence[BasePrompt]) -> None: prompt_ids = {int(prompt.uid) for prompt in prompts} - - # TODO: https://github.com/MartinBernstorff/personal-mnemonic-medium/issues/284 Figure out how NoteIDs are generated, and how they map to PromptIDs self.gateway.delete_notes(list(prompt_ids)) def _grouped_cards_to_deck( diff --git a/personal_mnemonic_medium/v2/domain/prompts/cloze_prompt.py b/personal_mnemonic_medium/v2/domain/prompts/cloze_prompt.py index 1333ec6a..c57a1d3e 100644 --- a/personal_mnemonic_medium/v2/domain/prompts/cloze_prompt.py +++ b/personal_mnemonic_medium/v2/domain/prompts/cloze_prompt.py @@ -30,6 +30,16 @@ def tags(self) -> Sequence[str]: return self.add_tags +@dataclass(frozen=True) +class RemoteClozePrompt(ClozePrompt): + remote_id: str + add_tags: Sequence[str] + + @property + def tags(self) -> Sequence[str]: + return self.add_tags + + @dataclass(frozen=True) class ClozePromptFromDoc(ClozePrompt): text: str diff --git a/personal_mnemonic_medium/v2/domain/prompts/qa_prompt.py b/personal_mnemonic_medium/v2/domain/prompts/qa_prompt.py index bdfa32ea..179e3955 100644 --- a/personal_mnemonic_medium/v2/domain/prompts/qa_prompt.py +++ b/personal_mnemonic_medium/v2/domain/prompts/qa_prompt.py @@ -33,6 +33,16 @@ def tags(self) -> Sequence[str]: return self.add_tags +@dataclass(frozen=True) +class RemoteQAPrompt(QAPrompt): + remote_id: str + add_tags: Sequence[str] + + @property + def tags(self) -> Sequence[str]: + return self.add_tags + + @dataclass(frozen=True) class QAPromptFromDoc(QAPrompt): parent_doc: Document