<commit_before>import json
import shutil
from pathlib import Path

import aqt
import anki.hooks

from crowd_anki.utils.constants import DECK_FILE_EXTENSION, MEDIA_SUBDIRECTORY_NAME
from crowd_anki.representation.deck import Deck


class AnkiJsonImporter(object):
    def __init__(self, collection):
        self.collection = collection

    def load_from_file(self, file_path):
        """
        Load deck from json file
        :type file_path: Path
        """
        with file_path.open() as deck_file:
            deck_json = json.load(deck_file)
            deck = Deck.from_json(deck_json)

            if aqt.mw:
                aqt.mw.backup()

            deck.save_to_collection(self.collection)

    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.
        :param import_media: Should we copy media?
        :type directory_path: Path
        """
        self.load_from_file(directory_path.joinpath(directory_path.name).with_suffix(DECK_FILE_EXTENSION))

        if not import_media:
            return<commit_msg>fix update deck view on import<commit_after>import json
import shutil
from pathlib import Path

import aqt
import anki.hooks

from crowd_anki.utils.constants import DECK_FILE_EXTENSION, MEDIA_SUBDIRECTORY_NAME
from crowd_anki.representation.deck import Deck


class AnkiJsonImporter(object):
    def __init__(self, collection):
        self.collection = collection

    def load_from_file(self, file_path):
        """
        Load deck from json file
        :type file_path: Path
        """
        with file_path.open() as deck_file:
            deck_json = json.load(deck_file)
            deck = Deck.from_json(deck_json)

            if aqt.mw:
                aqt.mw.backup()
                aqt.mw.progress.start(immediate=True)

            deck.save_to_collection(self.collection)

            if aqt.mw:
                aqt.mw.progress.finish()
                aqt.mw.deckBrowser.show()

    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.
        :param import_media: Should we copy media?
        :type directory_path: Path
        """
        self.load_from_file(directory_path.joinpath(directory_path.name).with_suffix(DECK_FILE_EXTENSION))

        if not import_media:
            return
{'fraction_non_alphanumeric': 0.06797583081570997, 'fraction_numerical': 0.0, 'mean_word_length': 2.494722955145119, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7629960', 'n_tokens_mistral': 771, 'n_tokens_neox': 746, 'n_words': 201}