diff --git a/personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py b/personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py index 92d3eff..3b86ac1 100644 --- a/personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py +++ b/personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py @@ -37,9 +37,17 @@ class AnkiConnectCommand(Enum): class AnkiConnectGateway: - def __init__(self, ankiconnect_url: str, deck_name: str) -> None: + def __init__( + self, + ankiconnect_url: str, + deck_name: str, + tmp_read_dir: Path, + tmp_write_dir: Path, + ) -> None: self.ankiconnect_url = ankiconnect_url self.deck_name = deck_name + self.tmp_read_dir = tmp_read_dir + self.tmp_write_dir = tmp_write_dir def update_model(self, model: genanki.Model) -> None: self._invoke( @@ -57,17 +65,12 @@ def update_model(self, model: genanki.Model) -> None: model={"name": model.name, "css": model.css}, # type: ignore ) - def import_package( - self, - package: genanki.Package, - tmp_write_dir: Path, - tmp_read_dir: Path, - ) -> None: + def import_package(self, package: genanki.Package) -> None: apkg_name = f"{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.apkg" - write_path = tmp_write_dir / apkg_name + write_path = self.tmp_write_dir / apkg_name package.write_to_file(write_path) # type: ignore - read_path = tmp_read_dir / apkg_name + read_path = self.tmp_read_dir / apkg_name try: self._invoke( AnkiConnectCommand.IMPORT_PACKAGE, path=str(read_path) @@ -140,8 +143,6 @@ class FakeAnkiCommand: @dataclass(frozen=True) class ImportPackage(FakeAnkiCommand): package: genanki.Package - tmp_write_dir: Path - tmp_read_dir: Path @dataclass(frozen=True) @@ -161,16 +162,5 @@ def get_all_note_infos(self) -> Sequence[NoteInfo]: def update_model(self, model: genanki.Model) -> None: self.executed_commands.append(UpdateModel(model=model)) - def import_package( - self, - package: genanki.Package, - tmp_write_dir: Path, - tmp_read_dir: Path, - ) -> None: - self.executed_commands.append( - ImportPackage( - package=package, - tmp_write_dir=tmp_write_dir, - tmp_read_dir=tmp_read_dir, - ) - ) + def import_package(self, package: genanki.Package) -> None: + self.executed_commands.append(ImportPackage(package=package)) diff --git a/personal_mnemonic_medium/v2/data_access/test_ankiconnect.py b/personal_mnemonic_medium/v2/data_access/test_ankiconnect.py index 2a604f9..cb94748 100644 --- a/personal_mnemonic_medium/v2/data_access/test_ankiconnect.py +++ b/personal_mnemonic_medium/v2/data_access/test_ankiconnect.py @@ -32,15 +32,17 @@ class MockNoteInfo(NoteInfo): reason="Tests require a running AnkiConnect server", ) class TestAnkiConnectGateway: + output_path = Path("/output") gateway = AnkiConnectGateway( - ankiconnect_url=ANKICONNECT_URL, deck_name="Test deck" + ankiconnect_url=ANKICONNECT_URL, + deck_name="Test deck", + tmp_read_dir=Path("/Users/Leisure/ankidecks"), + tmp_write_dir=output_path, ) - def test_full_sequence(self): - # Setup - output_path = Path("/output") - - for f in output_path.glob("*.apkg"): + def test_import_package(self): + # Delete all .apkg in the output directory + for f in self.output_path.glob("*.apkg"): f.unlink() deck = genanki.Deck(deck_id=1, name="Test deck") @@ -68,11 +70,7 @@ def test_full_sequence(self): package = genanki.Package(deck_or_decks=deck) # Phase 1: Importing - self.gateway.import_package( - package=package, - tmp_write_dir=Path("/output"), - tmp_read_dir=Path("/Users/Leisure/ankidecks"), - ) + self.gateway.import_package(package=package) assert len(list(Path("/output").glob("*.apkg"))) == 0 # Phase 2: Getting diff --git a/personal_mnemonic_medium/v2/domain/prompt_destination/anki_connect/ankiconnect_destination.py b/personal_mnemonic_medium/v2/domain/prompt_destination/anki_connect/ankiconnect_destination.py index c1eb43f..163f574 100644 --- a/personal_mnemonic_medium/v2/domain/prompt_destination/anki_connect/ankiconnect_destination.py +++ b/personal_mnemonic_medium/v2/domain/prompt_destination/anki_connect/ankiconnect_destination.py @@ -102,11 +102,7 @@ def _push_prompts(self, command: PushPrompts) -> None: package = self._create_package(cards) - self.gateway.import_package( - package, - tmp_write_dir=command.tmp_write_dir, - tmp_read_dir=command.tmp_read_dir, - ) + self.gateway.import_package(package) def update( self, commands: Sequence[PromptDestinationCommand] diff --git a/personal_mnemonic_medium/v2/presentation/cli.py b/personal_mnemonic_medium/v2/presentation/cli.py index 386002c..883c6f3 100644 --- a/personal_mnemonic_medium/v2/presentation/cli.py +++ b/personal_mnemonic_medium/v2/presentation/cli.py @@ -28,13 +28,18 @@ msg = Printer(timestamp=True) -def _sync_deck(deck_name: str): +def _sync_deck( + deck_name: str, input_dir: Path, apkg_output_filepath: Path +): # TODO: https://github.com/MartinBernstorff/personal-mnemonic-medium/issues/309 feat: use markdown promptsource source_prompts = FakePromptSource().get_prompts() destination = AnkiConnectDestination( gateway=AnkiConnectGateway( - ankiconnect_url=ANKICONNECT_URL, deck_name=deck_name + ankiconnect_url=ANKICONNECT_URL, + deck_name=deck_name, + tmp_read_dir=input_dir, + tmp_write_dir=apkg_output_filepath, ), prompt_converter=AnkiPromptConverter( base_deck=deck_name, card_css=CARD_MODEL_CSS @@ -81,14 +86,22 @@ def main( environment=get_env(default="None"), ) - _sync_deck(deck_name) + _sync_deck( + deck_name=deck_name, + input_dir=input_dir, + apkg_output_filepath=apkg_output_filepath, + ) if watch: sleep_seconds = 60 msg.good( f"Sync complete, sleeping for {sleep_seconds} seconds" ) - _sync_deck(deck_name) + _sync_deck( + deck_name=deck_name, + input_dir=input_dir, + apkg_output_filepath=apkg_output_filepath, + ) if __name__ == "__main__":