Skip to content

Commit

Permalink
perf: check for anki before note sync (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Nov 5, 2024
1 parent 0bdfd1e commit d4f33b9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
36 changes: 21 additions & 15 deletions memium/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,42 @@ def main(
dry_run: bool,
push_all: bool = False,
):
source_prompts = DocumentPromptSource(
document_ingester=MarkdownDocumentSource(directory=input_dir),
prompt_extractors=[
QAPromptExtractor(question_prefix="Q.", answer_prefix="A."),
TableExtractor(),
],
).get_prompts()
# Setup gateway as first step. If Anki is not running, no need to parse all the prompts.
gateway = AnkiConnectGateway(
ankiconnect_url=ANKICONNECT_URL,
base_deck=base_deck,
tmp_read_dir=host_input_dir() if in_docker() else input_dir,
tmp_write_dir=input_dir,
max_deletions_per_run=max_deletions_per_run,
max_wait_seconds=3600,
)

dest_class = AnkiConnectDestination if not dry_run else DryRunDestination
destination = dest_class(
gateway=AnkiConnectGateway(
ankiconnect_url=ANKICONNECT_URL,
base_deck=base_deck,
tmp_read_dir=host_input_dir() if in_docker() else input_dir,
tmp_write_dir=input_dir,
max_deletions_per_run=max_deletions_per_run,
max_wait_seconds=30,
),
gateway=gateway,
prompt_converter=AnkiPromptConverter(
base_deck=base_deck,
card_css=Path("memium/destination/ankiconnect/default_styling.css").read_text(),
),
)

# Get the inputs
source_prompts = DocumentPromptSource(
document_ingester=MarkdownDocumentSource(directory=input_dir),
prompt_extractors=[
QAPromptExtractor(question_prefix="Q.", answer_prefix="A."),
TableExtractor(),
],
).get_prompts()

# Get the updates
update_commands = (
[PushPrompts(prompts=source_prompts)]
if push_all
else PromptDiffDeterminer().sync(
source_prompts=source_prompts, destination_prompts=destination.get_all_prompts()
)
)
# Send them

destination.update(commands=update_commands)
8 changes: 4 additions & 4 deletions memium/destination/ankiconnect/ankiconnect_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ def __post_init__(self) -> None:
if seconds_waited >= self.max_wait_seconds:
raise ConnectionError(f"Could not connect to AnkiConnect at {self.ankiconnect_url}")

wait_seconds = 2
log.info(f"AnkiConnect is not live, waiting {wait_seconds} seconds...")
seconds_waited += wait_seconds
sleep(wait_seconds)
poll_seconds = 10
log.info(f"AnkiConnect is not live, waiting {poll_seconds} seconds...")
seconds_waited += poll_seconds
sleep(poll_seconds)

def update_model(self, model: genanki.Model) -> None:
existing_model_names = self._invoke(AnkiConnectCommand.GET_MODEL_NAMES)
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4f33b9

Please sign in to comment.