Skip to content

Commit

Permalink
More robust detection of Anki package being installed
Browse files Browse the repository at this point in the history
Previously a typo caused a ModuleNotFoundError even though Anki was
installed.
  • Loading branch information
gmcmanus committed Mar 25, 2020
1 parent da992f1 commit 4491486
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
20 changes: 15 additions & 5 deletions anki_roam_import/anki.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
from dataclasses import dataclass
from typing import Iterable, Optional

try:
from .model import JsonData, AnkiNote


def is_anki_package_installed() -> bool:
try:
import anki
except ModuleNotFoundError:
return False
else:
return True


if is_anki_package_installed():
from anki.collection import _Collection
from anki.models import NoteType
from anki.notes import Note
from anki.types import NoteType
from anki.utils import splitFields
from aqt.main import AnkiQt
except ModuleNotFoundError:
else:
# allow running tests without anki package installed
from typing import Any, Dict

Expand All @@ -25,8 +37,6 @@ def __init__(self, collection, model):
def splitFields(fields):
return []

from .model import JsonData, AnkiNote


@dataclass
class AnkiModelNotes:
Expand Down
16 changes: 9 additions & 7 deletions anki_roam_import/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
from dataclasses import dataclass
from typing import Iterable, List

try:
from .anki import (
AnkiAddonData, AnkiCollection, AnkiModelNotes, is_anki_package_installed,
)
from .anki_format import make_anki_note
from .model import AnkiNote
from .roam import extract_roam_blocks, load_roam_pages

if is_anki_package_installed():
from anki.utils import stripHTMLMedia
except ModuleNotFoundError:
else:
# allow running tests without anki package installed
# noinspection PyPep8Naming
def stripHTMLMedia(content): return content

from .anki import AnkiAddonData, AnkiCollection, AnkiModelNotes
from .model import AnkiNote
from .roam import load_roam_pages, extract_roam_blocks
from .anki_format import make_anki_note


@dataclass
class AnkiNoteImporter:
Expand Down

0 comments on commit 4491486

Please sign in to comment.