示例#1
0
def test_to_filename_csv():
    expected = "read-this-file.csv"

    assert expected == CsvFile.to_filename_csv("read this file")
    assert expected == CsvFile.to_filename_csv("read-this-file")
    assert expected == CsvFile.to_filename_csv("read-this-file.csv")
    assert expected == CsvFile.to_filename_csv("read          this        file")
示例#2
0
    def execute(self):
        self.setup_repo_structure()

        # Create the Deck Parts used
        headers_ca, note_models_all_ca, notes_ca, media_group_ca = self.parts_from_crowdanki(
            self.crowdanki_folder)

        headers = headers_ca.execute().part
        headers_name = LOC_HEADERS + "header1.yaml"
        headers.dump_to_yaml(headers_name)
        # TODO: desc file

        note_models = [m.part for m in note_models_all_ca.execute()]

        notes = notes_ca.execute().part
        used_note_models_in_notes = notes.get_all_known_note_model_names()

        media_group_ca.execute()

        note_model_mappings = [
            NoteModelMapping.Representation(
                [model.name for model in note_models])
        ]
        file_mappings: List[FileMapping.Representation] = []

        csv_files = []

        for model in note_models:
            if model.name in used_note_models_in_notes:
                csv_file_path = os.path.join(
                    LOC_DATA,
                    CsvFile.to_filename_csv(model.name, self.delimiter))
                column_headers = ["guid"
                                  ] + model.field_names_lowercase + ["tags"]
                CsvFile.create_file_with_headers(csv_file_path,
                                                 column_headers,
                                                 delimiter=self.delimiter)

                file_mappings.append(
                    FileMapping.Representation(file=csv_file_path,
                                               note_model=model.name,
                                               delimiter=self.delimiter))

                csv_files.append(csv_file_path)

        deck_path = os.path.join(
            LOC_BUILD, folder_name_from_full_path(self.crowdanki_folder))

        # Generate the Source files that will be kept in the repo
        save_note_models_to_folder = SaveNoteModelsToFolder.from_repr(
            SaveNoteModelsToFolder.Representation(
                [m.name for m in note_models], LOC_NOTE_MODELS, True))
        model_name_to_file_dict = save_note_models_to_folder.execute()

        save_media_to_folder = SaveMediaGroupsToFolder.from_repr(
            SaveMediaGroupsToFolder.Representation(parts=[RECIPE_MEDIA],
                                                   folder=LOC_MEDIA,
                                                   recursive=True,
                                                   clear_folder=True))
        save_media_to_folder.execute()

        generate_csvs = CsvsGenerate.from_repr({
            'notes': RECIPE_NOTES,
            'note_model_mappings': note_model_mappings,
            'file_mappings': file_mappings
        })
        generate_csvs.execute()

        # Create Recipes

        # Anki to Source
        headers_recipe, note_models_all_recipe, notes_recipe, media_group_recipe = self.parts_from_crowdanki(
            deck_path)

        build_part_tasks: List[BuildPartTask] = [
            headers_recipe,
            notes_recipe,
            note_models_all_recipe,
            media_group_recipe,
        ]
        dp_builder = PartsBuilder(build_part_tasks)

        top_level_tasks: List[TopLevelBuildTask] = [
            dp_builder, save_media_to_folder, generate_csvs
        ]
        self.create_yaml_from_top_level(
            top_level_tasks, os.path.join(LOC_RECIPES, "anki_to_source"))

        # Source to Anki
        note_models_from_yaml = [
            NoteModelsFromYamlPart.from_repr(
                NoteModelsFromYamlPart.Representation(name, file))
            for name, file in model_name_to_file_dict.items()
        ]

        media_group_from_folder = MediaGroupFromFolder.from_repr(
            MediaGroupFromFolder.Representation(part_id=RECIPE_MEDIA,
                                                source=LOC_MEDIA,
                                                recursive=True))

        headers_from_yaml = HeadersFromYamlPart.from_repr(
            HeadersFromYamlPart.Representation(part_id=RECIPE_HEADERS,
                                               file=headers_name))

        notes_from_csv = NotesFromCsvs.from_repr({
            'part_id': RECIPE_NOTES,
            'note_model_mappings': note_model_mappings,
            'file_mappings': file_mappings
        })

        build_part_tasks: List[BuildPartTask] = note_models_from_yaml + [
            headers_from_yaml,
            notes_from_csv,
            media_group_from_folder,
        ]
        dp_builder = PartsBuilder(build_part_tasks)

        generate_guids_in_csv = GenerateGuidsInCsvs.from_repr(
            GenerateGuidsInCsvs.Representation(source=csv_files,
                                               columns=["guid"],
                                               delimiter=self.delimiter))

        generate_crowdanki = CrowdAnkiGenerate.from_repr(
            CrowdAnkiGenerate.Representation(
                folder=deck_path,
                notes=NotesToCrowdAnki.Representation(
                    part_id=RECIPE_NOTES).encode(),
                headers=RECIPE_HEADERS,
                media=MediaGroupToCrowdAnki.Representation(
                    parts=[RECIPE_MEDIA]).encode(),
                note_models=NoteModelsToCrowdAnki.Representation(parts=[
                    NoteModelsToCrowdAnki.NoteModelListItem.Representation(
                        name).encode()
                    for name, file in model_name_to_file_dict.items()
                ]).encode()))

        top_level_tasks: List[TopLevelBuildTask] = [
            generate_guids_in_csv, dp_builder, generate_crowdanki
        ]
        source_to_anki_path = os.path.join(LOC_RECIPES, "source_to_anki")
        self.create_yaml_from_top_level(top_level_tasks, source_to_anki_path)

        print(
            f"\nRepo Init complete. You should now run `brainbrew run {source_to_anki_path}`"
        )
示例#3
0
def test_to_filename_csv():
    assert "read-this-file.csv" == CsvFile.to_filename_csv("read-this-file")
    assert "read-this-file.csv" == CsvFile.to_filename_csv(
        "read-this-file.csv")
    assert "read-this-file.tsv" == CsvFile.to_filename_csv(
        "read-this-file.tsv")