Skip to content

Commit

Permalink
format: line length
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff committed Oct 27, 2023
1 parent 804b6b8 commit 249592b
Show file tree
Hide file tree
Showing 23 changed files with 257 additions and 85 deletions.
30 changes: 23 additions & 7 deletions application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
AnkiPackageGenerator,
)
from personal_mnemonic_medium.exporters.anki.sync import sync_deck
from personal_mnemonic_medium.note_factories.markdown import MarkdownNoteFactory
from personal_mnemonic_medium.note_factories.markdown import (
MarkdownNoteFactory,
)
from personal_mnemonic_medium.prompt_extractors.cloze_extractor import (
ClozePromptExtractor,
)
Expand All @@ -32,12 +34,16 @@ def main(
host_output_dir: Path,
watch: Annotated[
bool,
typer.Option(help="Keep running, updating Anki deck every 15 seconds"),
typer.Option(
help="Keep running, updating Anki deck every 15 seconds"
),
],
):
"""Run the thing."""
if not input_dir.exists():
raise FileNotFoundError(f"Input directory {input_dir} does not exist")
raise FileNotFoundError(
f"Input directory {input_dir} does not exist"
)

if not host_output_dir.exists():
msg.info(f"Creating output directory {host_output_dir}")
Expand Down Expand Up @@ -65,11 +71,15 @@ def main(
input_path=input_dir,
)

grouped_cards = Seq(cards).group_by(lambda card: card.deckname).to_iter()
grouped_cards = (
Seq(cards).group_by(lambda card: card.deckname).to_iter()
)

for group in grouped_cards:
cards = group.group_contents.to_list()
deck_bundle = AnkiPackageGenerator().cards_to_deck_bundle(cards=cards)
deck_bundle = AnkiPackageGenerator().cards_to_deck_bundle(
cards=cards
)
sync_deck(
deck_bundle=deck_bundle,
sync_dir_path=host_output_dir,
Expand All @@ -79,9 +89,15 @@ def main(

if watch:
sleep_seconds = 60
msg.good(f"Sync complete, sleeping for {sleep_seconds} seconds")
msg.good(
f"Sync complete, sleeping for {sleep_seconds} seconds"
)
sleep(sleep_seconds)
main(input_dir=input_dir, watch=watch, host_output_dir=host_output_dir)
main(
input_dir=input_dir,
watch=watch,
host_output_dir=host_output_dir,
)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ reportMissingTypeStubs = false

[tool.ruff]
# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
line-length = 80
line-length = 70
select = [
"A",
"ANN",
Expand Down
16 changes: 12 additions & 4 deletions src/personal_mnemonic_medium/card_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from collections.abc import Sequence
from pathlib import Path

from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard
from personal_mnemonic_medium.exporters.anki.card_types.base import (
AnkiCard,
)
from personal_mnemonic_medium.exporters.base import CardExporter
from personal_mnemonic_medium.note_factories.base import DocumentFactory
from personal_mnemonic_medium.note_factories.base import (
DocumentFactory,
)
from personal_mnemonic_medium.note_factories.note import Document
from personal_mnemonic_medium.prompt_extractors.base import PromptExtractor
from personal_mnemonic_medium.prompt_extractors.base import (
PromptExtractor,
)
from personal_mnemonic_medium.prompt_extractors.prompt import Prompt


Expand All @@ -27,7 +33,9 @@ def run(
notes: list[Document] = []
if input_path.is_dir():
notes += list(
self.document_factory.get_notes_from_dir(dir_path=input_path)
self.document_factory.get_notes_from_dir(
dir_path=input_path
)
)

if not input_path.is_dir():
Expand Down
24 changes: 18 additions & 6 deletions src/personal_mnemonic_medium/exporters/anki/card_types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def __init__(
self,
fields: list[str],
source_prompt: Prompt,
url_generator: Callable[[Path, int | None], 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
Expand Down Expand Up @@ -112,7 +114,9 @@ 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
while len(self.html_fields) < len(
self.genanki_model.fields
): # type: ignore
before_extras_field = len(self.html_fields) == 2
if before_extras_field:
self.add_field(self.get_source_button())
Expand Down Expand Up @@ -146,7 +150,9 @@ def get_deck_dir(self) -> Path:
# This is all it takes
return Path(self.source_doc.source_path).parent

def determine_media_references(self) -> Iterator[tuple[Path, Path]]:
def determine_media_references(
self
) -> Iterator[tuple[Path, Path]]:
"""Find all media references in a card"""
for i, field in enumerate(self.html_fields):
current_stage = field
Expand All @@ -157,13 +163,19 @@ def determine_media_references(self) -> Iterator[tuple[Path, Path]]:

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
abspath, newpath = self.make_ref_pair(
initial_contents
) # type: ignore
results.append((abspath, newpath)) # noqa # type: ignore
return r'src="' + newpath + '"'

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

yield from results

# Anki seems to hate alt tags :(
self.html_fields[i] = re.sub(r'alt="[^"]*?"', "", current_stage)
self.html_fields[i] = re.sub(
r'alt="[^"]*?"', "", current_stage
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from pathlib import Path

import genanki
from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard
from personal_mnemonic_medium.exporters.anki.card_types.base import (
AnkiCard,
)
from personal_mnemonic_medium.exporters.markdown_to_html.html_compiler import (
compile_field,
)
Expand All @@ -19,7 +21,9 @@ def __init__(
self,
fields: list[str],
source_prompt: Prompt,
url_generator: Callable[[Path, int | None], str] = get_obsidian_url,
url_generator: Callable[
[Path, int | None], str
] = get_obsidian_url,
html_compiler: Callable[[str], str] = compile_field,
):
super().__init__(
Expand Down
8 changes: 6 additions & 2 deletions src/personal_mnemonic_medium/exporters/anki/card_types/qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from pathlib import Path

import genanki
from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard
from personal_mnemonic_medium.exporters.anki.card_types.base import (
AnkiCard,
)
from personal_mnemonic_medium.exporters.markdown_to_html.html_compiler import (
compile_field,
)
Expand All @@ -18,7 +20,9 @@ def __init__(
self,
fields: list[str],
source_prompt: Prompt,
url_generator: Callable[[Path, int | None], str] = get_obsidian_url,
url_generator: Callable[
[Path, int | None], str
] = get_obsidian_url,
html_compiler: Callable[[str], str] = compile_field,
):
super().__init__(
Expand Down
12 changes: 9 additions & 3 deletions src/personal_mnemonic_medium/exporters/anki/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import textwrap
from typing import Any

from personal_mnemonic_medium.exporters.anki.anki_css import CARD_MODEL_CSS
from personal_mnemonic_medium.exporters.anki.anki_css import (
CARD_MODEL_CSS,
)

ANKICONNECT_URL = (
"http://host.docker.internal:8765"
Expand All @@ -19,8 +21,12 @@
QUESTION_STR = r"{{ Question }}"
ANSWER_STR = r"{{ Answer }}"
EXTRA_STR = r"{{ Extra }}"
TTS_QUESTION_STR = r"{{ tts en_US voices=Apple_Samantha speed=1.05:Question }}"
TTS_ANSWER_STR = r"{{ tts en_US voices=Apple_Samantha speed=1.05:Answer }}"
TTS_QUESTION_STR = (
r"{{ tts en_US voices=Apple_Samantha speed=1.05:Question }}"
)
TTS_ANSWER_STR = (
r"{{ tts en_US voices=Apple_Samantha speed=1.05:Answer }}"
)

QA_MODEL_TEMPLATE = [
{
Expand Down
20 changes: 15 additions & 5 deletions src/personal_mnemonic_medium/exporters/anki/package_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@

import genanki

from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard
from personal_mnemonic_medium.exporters.anki.card_types.cloze import AnkiCloze
from personal_mnemonic_medium.exporters.anki.card_types.qa import AnkiQA
from personal_mnemonic_medium.exporters.anki.card_types.base import (
AnkiCard,
)
from personal_mnemonic_medium.exporters.anki.card_types.cloze import (
AnkiCloze,
)
from personal_mnemonic_medium.exporters.anki.card_types.qa import (
AnkiQA,
)
from personal_mnemonic_medium.exporters.base import CardExporter
from personal_mnemonic_medium.prompt_extractors.cloze_extractor import (
ClozePrompt,
)
from personal_mnemonic_medium.prompt_extractors.prompt import Prompt
from personal_mnemonic_medium.prompt_extractors.qa_extractor import QAPrompt
from personal_mnemonic_medium.prompt_extractors.qa_extractor import (
QAPrompt,
)
from personal_mnemonic_medium.utils.hasher import simple_hash

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -84,7 +92,9 @@ def cards_to_deck(
) # This is inefficient but definitely works on all platforms.
media.add(newpath)
except FileNotFoundError as e:
log.debug(f"Could not find file {abspath} for media, {e}.")
log.debug(
f"Could not find file {abspath} for media, {e}."
)

try:
deck.add_note(card.to_genanki_note())
Expand Down
32 changes: 24 additions & 8 deletions src/personal_mnemonic_medium/exporters/anki/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
from genanki import Model, Note
from wasabi import Printer

from personal_mnemonic_medium.exporters.anki.globals import ANKICONNECT_URL
from personal_mnemonic_medium.exporters.anki.package_generator import DeckBundle
from personal_mnemonic_medium.exporters.anki.globals import (
ANKICONNECT_URL,
)
from personal_mnemonic_medium.exporters.anki.package_generator import (
DeckBundle,
)

msg = Printer(timestamp=True)

Expand All @@ -29,7 +33,9 @@ def invoke(action: Any, **params: Any) -> Any:
Returns:
Any: the response from anki connect
"""
requestJson = json.dumps(request(action, **params)).encode("utf-8")
requestJson = json.dumps(request(action, **params)).encode(
"utf-8"
)
response = json.load(
urllib.request.urlopen(
urllib.request.Request(ANKICONNECT_URL, requestJson)
Expand Down Expand Up @@ -84,7 +90,9 @@ def sync_deck(
return

# get a list of anki cards in the deck
anki_note_info_by_guid, anki_note_guids = get_anki_note_infos(deck_bundle)
anki_note_info_by_guid, anki_note_guids = get_anki_note_infos(
deck_bundle
)

# get the unique guids of the md notes
md_note_guids = get_md_note_infos(deck_bundle)
Expand Down Expand Up @@ -125,7 +133,9 @@ def sync_deck(
"deleteNotes",
notes=note_ids,
)
msg.good(f"Deleted {len(guids_to_delete)} notes")
msg.good(
f"Deleted {len(guids_to_delete)} notes"
)

except Exception:
msg.fail(
Expand Down Expand Up @@ -159,7 +169,9 @@ def get_anki_note_infos(
)

# get a list of anki notes in the deck
anki_note_ids: list[int] = invoke("cardsToNotes", cards=anki_card_ids)
anki_note_ids: list[int] = invoke(
"cardsToNotes", cards=anki_card_ids
)

# get the note info for the notes in the deck
anki_notes_info = invoke("notesInfo", notes=anki_note_ids)
Expand All @@ -186,7 +198,9 @@ def sync_model(model: Model):
if model.name not in model_names_to_ids:
return
except Exception as e:
msg.good("\tUnable to fetch existing model names and ids from anki")
msg.good(
"\tUnable to fetch existing model names and ids from anki"
)
msg.good(f"\t\t{e}")

if anki_connect_is_live():
Expand All @@ -206,7 +220,9 @@ def sync_model(model: Model):
)
msg.good(f"\tUpdated model {model.name} template")
except Exception as e:
msg.good(f"\tUnable to update model {model.name} template")
msg.good(
f"\tUnable to update model {model.name} template"
)
msg.good(f"\t\t{e}")

try:
Expand Down
8 changes: 6 additions & 2 deletions src/personal_mnemonic_medium/exporters/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from abc import ABC, abstractmethod
from collections.abc import Sequence

from personal_mnemonic_medium.exporters.anki.card_types.base import AnkiCard
from personal_mnemonic_medium.exporters.anki.card_types.base import (
AnkiCard,
)
from personal_mnemonic_medium.prompt_extractors.prompt import Prompt


class CardExporter(ABC):
@abstractmethod
def prompts_to_cards(self, prompts: Sequence[Prompt]) -> list[AnkiCard]:
def prompts_to_cards(
self, prompts: Sequence[Prompt]
) -> list[AnkiCard]:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def field_to_html(field: Any) -> str:

def compile_field(fieldtext: str) -> str:
"""Turn source markdown into an HTML field suitable for Anki."""
fieldtext_sans_wiki = fieldtext.replace("[[", "<u>").replace("]]", "</u>")
fieldtext_sans_comments = re.sub(r"<!--.+-->", "", fieldtext_sans_wiki)
fieldtext_sans_wiki = fieldtext.replace("[[", "<u>").replace(
"]]", "</u>"
)
fieldtext_sans_comments = re.sub(
r"<!--.+-->", "", fieldtext_sans_wiki
)

return field_to_html(fieldtext_sans_comments)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from pathlib import Path


def get_obsidian_url(source_path: Path, line_nr: int | None = 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
Expand Down
Loading

0 comments on commit 249592b

Please sign in to comment.