Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#15 json naming convention #56

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_fsname).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
13 changes: 8 additions & 5 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 @@ -21,7 +21,7 @@ def load_from_file(self, file_path):
:type file_path: Path
"""
if not file_path.exists():
raise ValueError("There is no {} file inside of the selected directory".format(file_path.name))
raise ValueError("There is no {} file inside of the selected directory".format(file_path))

with file_path.open(encoding='utf8') as deck_file:
deck_json = json.load(deck_file)
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(directory_path.name).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