Skip to content

Commit

Permalink
fix: import all decks
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff committed Dec 22, 2023
1 parent 5a220ff commit dda73bd
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ def _delete_prompts(
self.gateway.delete_notes(list(prompt_ids))

def _grouped_cards_to_deck(
self, cards: Mapping[str, Sequence[AnkiCard]]
self, grouped_cards: Mapping[str, Sequence[AnkiCard]]
) -> genanki.Deck:
deck_name = next(iter(cards.keys()))
deck_name = next(iter(grouped_cards.keys()))
deck = genanki.Deck(
name=deck_name, deck_id=hash_cleaned_str(deck_name)
)

for card in cards[deck_name]:
for card in grouped_cards[deck_name]:
deck.add_note(card.to_genanki_note()) # type: ignore

return deck
Expand All @@ -97,11 +97,12 @@ def _create_package(
cards_grouped_by_deck = Seq(cards).groupby(
lambda card: card.deck
)
decks = (
Seq([cards_grouped_by_deck])
.map(self._grouped_cards_to_deck)
.to_list()
)
decks = [
self._grouped_cards_to_deck(
{group: cards_grouped_by_deck[group]}
)
for group in cards_grouped_by_deck
]

return genanki.Package(deck_or_decks=decks)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def to_genanki_note(self) -> genanki.Note:

@property
def deck(self) -> str:
deck_prefix = "#anki/deck/"
deck_prefix = "anki/deck/"
deck_in_tags = (
tag.replace(deck_prefix, "")
for tag in self.tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ class FakeAnkiQA(AnkiQA):


def test_ankiqa_deck_inference():
card = FakeAnkiQA(tags=["#anki/deck/Subdeck"])
card = FakeAnkiQA(tags=["anki/deck/Subdeck"])
assert card.deck == "FakeBaseDeck::Subdeck"
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_ankiconnect_push_prompts():
QAWithoutDoc(
question="FakeQuestion",
answer="FakeAnswer",
add_tags=["#anki/deck/FakeSubdeck"],
add_tags=["anki/deck/FakeSubdeck"],
),
ClozeWithoutDoc(
text="FakeText", add_tags=["FakeTag"]
Expand All @@ -83,6 +83,7 @@ def test_ankiconnect_push_prompts():
import_package_command.package.decks[0].name # type: ignore
== "FakeDeck::FakeSubdeck"
)
assert len(import_package_command.package.decks) == 2 # type: ignore

for command in expected_commands:
assert (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def test_cloze_prompt_extractor(tmpdir: Path):
extractor[0].text
== r"What is the meaning of life? {{c734::42}}"
)
assert extractor[0].tags == ["#anki/tag/test_tag"]
assert extractor[0].tags == ["anki/tag/test_tag"]
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ def test_qa_prompt_extractor(tmpdir: Path):
prompt = extractor[0]
assert prompt.question == "What is the meaning of life?"
assert prompt.answer == "42"
assert prompt.tags == ["#anki/tag/test_tag"]
assert prompt.tags == ["anki/tag/test_tag"]
assert prompt.uid == 9385242780

0 comments on commit dda73bd

Please sign in to comment.