Skip to content

Commit

Permalink
#15 extracted the default json name into a constant, ensured backward…
Browse files Browse the repository at this point in the history
…s compatibility in the importer, and changed the function description
  • Loading branch information
evolverine authored and Stvad committed Jul 29, 2019
1 parent 0a6523e commit 68a8f34
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crowd_anki/export/anki_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from ..anki.adapters.anki_deck import AnkiDeck
from ..representation import deck_initializer
from ..representation.deck import Deck
from ..utils.constants import DECK_FILE_EXTENSION, MEDIA_SUBDIRECTORY_NAME
from ..utils.constants import DECK_FILE_NAME, DECK_FILE_EXTENSION, MEDIA_SUBDIRECTORY_NAME
from ..utils.filesystem.name_sanitizer import sanitize_anki_deck_name


Expand All @@ -28,7 +28,7 @@ def export_to_directory(self, deck: AnkiDeck, output_dir=Path("."), copy_media=T
deck = deck_initializer.from_collection(self.collection, deck.name)
self.last_exported_count = deck.get_note_count()

deck_filename = deck_directory.joinpath('deck').with_suffix(DECK_FILE_EXTENSION)
deck_filename = deck_directory.joinpath(DECK_FILE_NAME).with_suffix(DECK_FILE_EXTENSION)
with deck_filename.open(mode='w', encoding="utf8") as deck_file:
deck_file.write(json.dumps(deck,
default=Deck.default_json,
Expand Down
11 changes: 7 additions & 4 deletions crowd_anki/importer/anki_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import aqt
import aqt.utils
from ..representation import deck_initializer
from ..utils.constants import DECK_FILE_EXTENSION, MEDIA_SUBDIRECTORY_NAME
from ..utils.constants import DECK_FILE_NAME, DECK_FILE_EXTENSION, MEDIA_SUBDIRECTORY_NAME


class AnkiJsonImporter(object):
Expand All @@ -32,8 +32,8 @@ def load_from_file(self, file_path):
def load_from_directory(self, directory_path, import_media=True):
"""
Load deck serialized to directory
Assumes that deck json file is located in the directory and named
same way as a directory but with json file extension.
Assumes that deck json file is located in the directory
and named 'deck.json'
:param import_media: Should we copy media?
:type directory_path: Path
"""
Expand All @@ -42,7 +42,10 @@ def load_from_directory(self, directory_path, import_media=True):
aqt.mw.progress.start(immediate=True)

try:
self.load_from_file(directory_path.joinpath('deck').with_suffix(DECK_FILE_EXTENSION))
try:
self.load_from_file(directory_path.joinpath(DECK_FILE_NAME).with_suffix(DECK_FILE_EXTENSION))
except ValueError:
self.load_from_file(directory_path.joinpath(directory_path.name).with_suffix(DECK_FILE_EXTENSION))

if import_media:
media_directory = directory_path.joinpath(MEDIA_SUBDIRECTORY_NAME)
Expand Down
2 changes: 1 addition & 1 deletion crowd_anki/utils/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path

UUID_FIELD_NAME = 'crowdanki_uuid'

DECK_FILE_NAME = "deck"
DECK_FILE_EXTENSION = ".json"
MEDIA_SUBDIRECTORY_NAME = "media"

Expand Down

0 comments on commit 68a8f34

Please sign in to comment.