Skip to content

Commit

Permalink
fix: PromptID to NoteID mapping (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Dec 12, 2023
2 parents 94c044c + 20dd84e commit 30c2913
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
12 changes: 4 additions & 8 deletions personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,21 @@ 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:
self._invoke(
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)

Expand Down
27 changes: 23 additions & 4 deletions personal_mnemonic_medium/v2/data_access/test_ankiconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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}}<hr id=answer>{{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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions personal_mnemonic_medium/v2/domain/prompts/cloze_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions personal_mnemonic_medium/v2/domain/prompts/qa_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 30c2913

Please sign in to comment.