Skip to content

Commit

Permalink
misc.
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff committed Oct 27, 2023
1 parent 40cc8e6 commit e512c5b
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SRC_PATH = personal_mnemonic_medium
SRC_PATH = src/personal_mnemonic_medium

install-dev:
pip install -r dev-requirements.txt
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ ignore = [
"ANN401",
"E402",
"E501",
"F401",
"F841",
"RET504",
"COM812",
Expand Down
10 changes: 6 additions & 4 deletions src/personal_mnemonic_medium/exporters/anki/card_types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def to_genanki_note(self) -> genanki.Note:

if len(self.html_fields) < len(self.genanki_model.fields): # type: ignore
while len(self.html_fields) < len(
self.genanki_model.fields # type: ignore
self.genanki_model.fields # type: ignore
): # type: ignore
before_extras_field = len(self.html_fields) == 2
if before_extras_field:
Expand All @@ -137,7 +137,7 @@ def to_genanki_note(self) -> genanki.Note:

def make_ref_pair(self, filename: str) -> tuple[Path, str]:
"""Take a filename relative to the card, and make it absolute."""
newname = "%".join(filename.split(os.sep)) # type: ignore # noqa: PTH206
newname = "%".join(filename.split(os.sep)) # type: ignore # noqa: PTH206

if os.path.isabs(filename): # noqa
abspath = Path(filename)
Expand All @@ -163,13 +163,15 @@ def determine_media_references(
def process_match(m) -> str: # noqa # type: ignore
initial_contents = m.group(1) # type: ignore
abspath, newpath = self.make_ref_pair(
initial_contents # type: ignore
initial_contents # type: ignore
) # type: ignore
results.append((abspath, newpath)) # noqa # type: ignore
return r'src="' + newpath + '"'

current_stage = re.sub(
regex, process_match, current_stage # type: ignore
regex,
process_match,
current_stage, # type: ignore
) # type: ignore

yield from results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from personal_mnemonic_medium.exporters.anki.card_types.base import (
AnkiCard,
)
from personal_mnemonic_medium.exporters.anki.globals import CONFIG
from personal_mnemonic_medium.exporters.markdown_to_html.html_compiler import (
compile_field,
)
Expand Down Expand Up @@ -35,13 +36,12 @@ def __init__(

@property
def genanki_model(self) -> genanki.Model:
global CONFIG # noqa
return genanki.Model(
model_id=simple_hash(CONFIG["card_model_name_cloze"]), # type: ignore
name=CONFIG["card_model_name_cloze"],
fields=CONFIG["card_model_fields_cloze"],
templates=CONFIG["card_model_template_cloze"],
css=CONFIG["card_model_css"],
css=CONFIG["card_model_css"], # type: ignore
model_type=1, # This is the model_type number for genanki, takes 0 for QA or 1 for cloze
)

Expand Down
4 changes: 3 additions & 1 deletion src/personal_mnemonic_medium/exporters/anki/card_types/qa.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from collections.abc import Callable
from collections.abc import Callable # noqa: I001
from pathlib import Path

import genanki
from personal_mnemonic_medium.exporters.anki.card_types.base import (
AnkiCard,
)
from personal_mnemonic_medium.exporters.anki.globals import CONFIG
from personal_mnemonic_medium.exporters.markdown_to_html.html_compiler import (
compile_field,
)
Expand All @@ -13,6 +14,7 @@
)
from personal_mnemonic_medium.prompt_extractors.prompt import Prompt
from personal_mnemonic_medium.utils.hasher import simple_hash
from personal_mnemonic_medium.exporters.anki.globals import CONFIG # noqa


class AnkiQA(AnkiCard):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def cards_to_deck_bundle(cards: list[AnkiCard]) -> DeckBundle:
def cards_to_deck(
cards: Sequence[AnkiCard]
) -> tuple[genanki.Deck, set[str]]:
media = set() # type: ignore
media = set() # type: ignore

deck_name = cards[0].deckname
deck_id = simple_hash(deck_name)
Expand All @@ -86,7 +86,7 @@ def cards_to_deck(
copyfile(
abspath, newpath
) # This is inefficient but definitely works on all platforms.
media.add(newpath) # type: ignore
media.add(newpath) # type: ignore
except FileNotFoundError as e:
log.debug(
f"Could not find file {abspath} for media, {e}."
Expand All @@ -99,7 +99,7 @@ def cards_to_deck(
f"Could not add card {card} to deck {deck_name}, {e}."
)

return deck, media # type: ignore
return deck, media # type: ignore

def prompts_to_cards(
self, prompts: Sequence[Prompt]
Expand Down
29 changes: 15 additions & 14 deletions src/personal_mnemonic_medium/exporters/anki/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def sync_deck(
)
else:
msg.info("Skipped")
msg.info(f"{deck_bundle.deck.name}") # type: ignore
msg.info(f"{deck_bundle.deck.name}") # type: ignore
msg.info("\tNo notes added or removed")
print("\n")

Expand All @@ -125,7 +125,7 @@ def _sync_deck(
md_note_guids: set[str],
):
msg.info(" Syncing deck: ")
msg.info(f"\t{deck_bundle.deck.name}") # type: ignore
msg.info(f"\t{deck_bundle.deck.name}") # type: ignore

added_note_guids = md_note_guids - anki_note_guids
if added_note_guids:
Expand All @@ -143,7 +143,7 @@ def _sync_deck(
try:
sync_path = str(sync_dir_path / "deck.apkg")
invoke("importPackage", path=sync_path)
print(f"Imported {deck_bundle.deck.name}!") # type: ignore
print(f"Imported {deck_bundle.deck.name}!") # type: ignore

if delete_cards:
try:
Expand All @@ -159,7 +159,7 @@ def _sync_deck(

except Exception:
msg.fail(
f"Unable to delete cards in {deck_bundle.deck.name}" # type: ignore
f"Unable to delete cards in {deck_bundle.deck.name}" # type: ignore
)
# Print full stack trace
traceback.print_exc()
Expand All @@ -170,7 +170,7 @@ def _sync_deck(


def get_md_note_infos(deck_bundle: DeckBundle) -> set[str]:
md_notes: list[Note] = deck_bundle.deck.notes # type: ignore
md_notes: list[Note] = deck_bundle.deck.notes # type: ignore
md_note_guids = {str(n.guid) for n in md_notes}
return md_note_guids

Expand All @@ -179,7 +179,8 @@ def get_anki_note_infos(
deck_bundle: DeckBundle
) -> tuple[dict[str, Any], set[str]]:
anki_card_ids: list[int] = invoke(
"findCards", query=f'"deck:{deck_bundle.deck.name}"' # type: ignore
"findCards",
query=f'"deck:{deck_bundle.deck.name}"', # type: ignore
)

# get a list of anki notes in the deck
Expand Down Expand Up @@ -209,7 +210,7 @@ def sync_model(model: Model):
model_names_to_ids = {}
try:
model_names_to_ids = invoke("modelNamesAndIds")
if model.name not in model_names_to_ids: # type: ignore
if model.name not in model_names_to_ids: # type: ignore
return
except Exception as e:
msg.good(
Expand All @@ -222,29 +223,29 @@ def sync_model(model: Model):
invoke(
"updateModelTemplates",
model={
"name": model.name, # type: ignore
"name": model.name, # type: ignore
"templates": {
t["name"]: {
"qfmt": t["qfmt"],
"afmt": t["afmt"],
}
for t in model.templates # type: ignore
for t in model.templates # type: ignore
},
},
)
msg.good(f"\tUpdated model {model.name} template") # type: ignore
msg.good(f"\tUpdated model {model.name} template") # type: ignore
except Exception as e:
msg.good(
f"\tUnable to update model {model.name} template" # type: ignore
f"\tUnable to update model {model.name} template" # type: ignore
)
msg.good(f"\t\t{e}")

try:
invoke(
"updateModelStyling",
model={"name": model.name, "css": model.css}, # type: ignore
model={"name": model.name, "css": model.css}, # type: ignore
)
msg.good(f"\tUpdated model {model.name} css") # type: ignore
msg.good(f"\tUpdated model {model.name} css") # type: ignore
except Exception as e:
msg.good(f"\tUnable to update model {model.name} css") # type: ignore
msg.good(f"\tUnable to update model {model.name} css") # type: ignore
msg.good(f"\t\t{e}")
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def field_to_html(field: Any) -> str:

for instance in token_instances:
field = field.replace(
instance, replacement + instance[1:-1] + replacement
instance,
replacement + instance[1:-1] + replacement, # type: ignore
) # type: ignore

# Make sure every \n converts into a newline
Expand Down
8 changes: 6 additions & 2 deletions src/personal_mnemonic_medium/note_factories/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def get_note_from_file(self, file_path: Path) -> Document:
note_title = file_path.stem

if self.cut_note_after in file_contents:
file_contents = file_contents.split(self.cut_note_after)[0]
file_contents = file_contents.split(
self.cut_note_after
)[0]

return Document(
title=note_title,
Expand All @@ -53,7 +55,9 @@ def get_note_from_file(self, file_path: Path) -> Document:
source_path=file_path,
)

def get_notes_from_dir(self, dir_path: Path) -> Sequence[Document]:
def get_notes_from_dir(
self, dir_path: Path
) -> Sequence[Document]:
notes: list[Document] = []

for parent_dir, _, files in os.walk(dir_path):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def extract_prompts(
)
)

prompts.append( # type: ignore
prompts.append( # type: ignore
ClozePrompt(
content=prompt_content,
tags=note.tags,
Expand All @@ -106,4 +106,4 @@ def extract_prompts(
)
)

return prompts # type: ignore
return prompts # type: ignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def extract_prompts(self, note: Document) -> Sequence[QAPrompt]:
)
continue

prompts.append( # type: ignore
prompts.append( # type: ignore
QAPrompt(
question=question,
answer=answer,
Expand All @@ -112,4 +112,4 @@ def extract_prompts(self, note: Document) -> Sequence[QAPrompt]:
)
block_starting_line_nr += block_lines

return prompts # type: ignore
return prompts # type: ignore
4 changes: 2 additions & 2 deletions tests/prompt_extractors/test_qa_prompt_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_has_qa_matches(qa_extractor: QAPromptExtractor):
matches = [
string
for string in example_strings
if qa_extractor._has_qa(string) # type: ignore
if qa_extractor._has_qa(string) # type: ignore
]

assert len(matches) == 3
Expand All @@ -56,7 +56,7 @@ def test_has_qa_does_not_match(qa_extractor: QAPromptExtractor):
matches = 0

for string in example_strings:
if qa_extractor._has_qa(string): # type: ignore
if qa_extractor._has_qa(string): # type: ignore
matches += 1

assert matches == 0
Expand Down

0 comments on commit e512c5b

Please sign in to comment.