Skip to content

Commit

Permalink
refactor get rid of tmp_read_dir and tmp_write_dir (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBernstorff authored Dec 13, 2023
2 parents 1866b7c + e9dc794 commit 4b91f01
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 44 deletions.
38 changes: 14 additions & 24 deletions personal_mnemonic_medium/v2/data_access/ankiconnect_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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))
20 changes: 9 additions & 11 deletions personal_mnemonic_medium/v2/data_access/test_ankiconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
21 changes: 17 additions & 4 deletions personal_mnemonic_medium/v2/presentation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 4b91f01

Please sign in to comment.