Skip to content

Commit

Permalink
fix: correctly sync dirs in bind mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff committed Oct 23, 2023
1 parent ff5fd40 commit 3e269ee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
17 changes: 12 additions & 5 deletions application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ def request(action: Any, **params: Any) -> Dict[str, Any]:


def main(
recur_dir: Path,
project_dir: Path,
input_dir: Path,
host_output_dir: Path,
watch: Annotated[
bool,
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")

if not host_output_dir.exists():
msg.info(f"Creating output directory {host_output_dir}")
host_output_dir.mkdir(parents=True, exist_ok=True)

sentry_sdk.init(
dsn="https://37f17d6aa7742424652663a04154e032@o4506053997166592.ingest.sentry.io/4506053999984640",
Expand All @@ -54,7 +60,7 @@ def main(
],
card_exporter=AnkiPackageGenerator(), # Step 3, get the cards from the prompts
).run(
input_path=recur_dir,
input_path=input_dir,
)

decks = defaultdict(list)
Expand All @@ -66,15 +72,16 @@ def main(
deck_bundle = AnkiPackageGenerator().cards_to_deck_bundle(cards=decks[deck])
sync_deck(
deck_bundle=deck_bundle,
dir_path=Path(__file__).parent,
sync_dir_path=host_output_dir,
save_dir_path=Path("/output"),
max_wait_for_ankiconnect=30,
)

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


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion docker_cmd.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
docker build . -t personal-mnemonic-medium -f Dockerfile

docker volume create ankidecks

docker run -itd \
-v $HOME/Library/Mobile\ Documents/iCloud~md~obsidian/Documents/Life\ Lessons\ iCloud/:/input \
-v $HOME/ankidecks:/output \
--restart unless-stopped \
personal-mnemonic-medium \
python application/main.py /input/ Life.apkg --watch
python application/main.py /input/ $HOME/ankidecks --watch
11 changes: 7 additions & 4 deletions src/personal_mnemonic_medium/exporters/anki/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def anki_connect_is_live() -> bool:
# Borrowed from https://github.com/lukesmurray/markdown-anki-decks/blob/de6556d7ecd2d39335607c05171f8a9c39c8f422/markdown_anki_decks/sync.py#L64
def sync_deck(
deck_bundle: DeckBundle,
dir_path: Path,
save_dir_path: Path,
sync_dir_path: Path,
delete_cards: bool = True,
max_wait_for_ankiconnect: int = 30,
):
Expand Down Expand Up @@ -101,9 +102,10 @@ def sync_deck(
msg.info("\tNotes removed: ")
msg.info(f"\t\t{removed_note_guids}")

package_path = deck_bundle.save_deck_to_file(dir_path / "deck.apkg")
package_path = deck_bundle.save_deck_to_file(save_dir_path / "deck.apkg")
try:
invoke("importPackage", path=str(package_path))
sync_path = str(sync_dir_path / "deck.apkg")
invoke("importPackage", path=sync_path)
print(f"Imported {deck_bundle.deck.name}!")

if delete_cards:
Expand All @@ -127,7 +129,8 @@ def sync_deck(
traceback.print_exc()
except Exception as e:
print(f"Unable to sync {package_path} to anki")
print(f"\t{e}")
print(f"{e}")
traceback.print_exc()
else:
msg.info("Skipped")
msg.info(f"{deck_bundle.deck.name}")
Expand Down

0 comments on commit 3e269ee

Please sign in to comment.