def test_run(self, global_config, note_model_name):
        file = DeckPartNoteModel(note_model_name)

        assert isinstance(file, DeckPartNoteModel)
        assert file.file_location == TestFiles.NoteModels.LOC + TestFiles.NoteModels.TEST_COMPLETE
        assert len(file.get_data().keys()) == 13

        assert file.name == "Test Model"
        assert file.id == "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
        assert file.fields == ["Word", "OtherWord"]
Beispiel #2
0
    def source_to_deck_parts(self):
        source_data = self.crowd_anki_export.get_data(deep_copy=True)

        # Headers
        header_keys_to_ignore = {CAKeys.NOTE_MODELS.value, CAKeys.NOTES.value, CAKeys.MEDIA_FILES.value}

        headers_data = {key: source_data[key] for key in source_data if key not in header_keys_to_ignore}
        self.headers.set_data(headers_data)

        # Note Models
        note_models = [
            DeckPartNoteModel.create(model[CANoteModelKeys.NAME.value], data_override=model)
            for model in source_data[CAKeys.NOTE_MODELS.value]
        ]

        note_models_id_name_dict = {model.id: model.name for model in note_models}

        # Media
        for filename, file in self.crowd_anki_export.known_media.items():
            dp_media_file = self.file_manager.media_file_if_exists(filename)
            if dp_media_file:
                dp_media_file.set_override(file.source_loc)
            else:
                self.file_manager.new_media_file(filename, file.source_loc)

        # Notes
        notes_json = source_data[CAKeys.NOTES.value]
        notes_data = self.notes_to_deck_parts(notes_json, note_models_id_name_dict)

        self.notes.set_data(notes_data)
Beispiel #3
0
    def deck_parts_to_source(self):
        ca_json = {}

        # Headers
        ca_json.update(self.headers.get_data())

        # Media
        media_files = self.notes.referenced_media_files
        ca_json.setdefault(CAKeys.MEDIA_FILES.value, list(sorted([file.filename for file in media_files])))

        for file in media_files:
            filename = file.filename
            if filename in self.crowd_anki_export.known_media:
                self.crowd_anki_export.known_media[filename].set_override(file.source_loc)
            else:
                self.crowd_anki_export.known_media.setdefault(
                    filename, MediaFile(self.crowd_anki_export.media_loc + filename,
                                        filename, MediaFile.ManagementType.TO_BE_CLONED, file.source_loc)
                )

        # Note Models
        note_models = [DeckPartNoteModel.create(name) for name in self.notes.get_all_known_note_model_names()]

        ca_json.setdefault(CAKeys.NOTE_MODELS.value, [model.get_data() for model in note_models])

        note_models_dict_id_name = {model.name: model.id for model in note_models}

        # Notes
        ca_json.setdefault(CAKeys.NOTES.value, self.notes_to_source(note_models_dict_id_name))

        ordered_keys = OrderedDict(sorted(ca_json.items()))

        self.crowd_anki_export.set_data(ordered_keys)
    def test_config_location_override(self, global_config):
        loc = "place_for_stuff/"
        filename = "what-a-great-file.json"

        global_config.deck_parts.note_models = loc

        file = DeckPartNoteModel(filename,
                                 read_now=False,
                                 data_override={
                                     CANoteModelKeys.NAME.value: "name",
                                     CANoteModelKeys.ID.value: "id",
                                     CANoteModelKeys.FIELDS.value: []
                                 })

        assert file.file_location == loc + filename
    def __init__(self, config_data: dict, read_now=True):
        self.setup_config_with_subconfig_replacement(config_data)
        self.verify_config_entry()

        columns = self.get_config(NoteModelMappingKeys.COLUMNS)
        personal_fields = self.get_config(NoteModelMappingKeys.PERSONAL_FIELDS,
                                          [])

        self.columns = [
            FieldMapping(field_type=FieldMapping.FieldMappingType.COLUMN,
                         field_name=field,
                         value=columns[field]) for field in columns
        ]

        self.personal_fields = [
            FieldMapping(
                field_type=FieldMapping.FieldMappingType.PERSONAL_FIELD,
                field_name=field,
                value="") for field in personal_fields
        ]

        self.note_model = DeckPartNoteModel.create(self.get_config(
            NoteModelMappingKeys.NOTE_MODEL),
                                                   read_now=read_now)
def temp_dp_note_model_file(tmpdir) -> DeckPartNoteModel:
    file = tmpdir.mkdir("note_models").join("file.json")
    file.write("{}")

    return DeckPartNoteModel(file.strpath, read_now=False)
def dp_note_model_test1(global_config) -> DeckPartNoteModel:
    return DeckPartNoteModel.create(TestFiles.NoteModels.TEST_COMPLETE)
Beispiel #8
0
def make_deck_part_note_model_mock(data):
    return DeckPartNoteModel(NamedTemporaryFile(suffix=".json",
                                                delete=False).name,
                             read_now=False,
                             data_override=data)