示例#1
0
def main(
    output_file: str = typer.Argument(..., help="The json file where to write the results"),
    input_folder: Optional[str] = typer.Argument(None, help="The folder to process"),
    input_file: Optional[str] = typer.Argument(None, help="The file to enrich"),
    enrich: bool = typer.Option(
        False, help="Whether to enrich the dataset with an external database"
    ),
    force_enrich: bool = typer.Option(
        False,
        help=
        "To enrich again files that are already enriched. Useful if you want to update the infos from files."
    ),
):
    """Extracts file infos from a folder or enrich a file inside a json.
    If you provide not input_file or input_folder, output_file will be used as input file and enrich
    will be set to true.
    This is useful to enrich a file.
    """
    if input_file is None and input_folder is None:
        input_file = output_file
        enrich = True
    output_path = Path(output_file)
    if output_path.is_file() and Path(input_file) != output_path:
        raise FileExistsError(output_path)
    files_info = get_files_info(input_file, input_folder)
    if enrich or force_enrich:
        files_info = enrich_files(files_info, force_enrich)

    write_json(output_file, files_info)
示例#2
0
def test_get_files_info(folder, tmpdir):
    with pytest.raises(ValueError):
        get_files_info(None, None)

    file_info_path = os.path.join(tmpdir, "file_info.json")
    with pytest.raises(FileNotFoundError):
        get_files_info(file_info_path, None)
    files_info = [
        {
            "md5": "0cc175b9c0f1b6a831c399e269772661",
            "name": "toto",
            "tata": "tutu"
        },
        {
            "md5": "900150983cd24fb0d6963f7d28e17f72",
            "name": "trololo",
            "ignore": True
        },
    ]
    write_json(file_info_path, files_info)

    files_info = get_files_info(file_info_path, folder)
    # yapf: disable
    expected_files_info = [
        {"name": "folder_4", "size": 4, "md5": "e2fc714c4727ee9395f324cd2e7f331f"},
        {"name": "movie", "size": 2, "md5": "187ef4436122d1cc2f40dc2b92f0eba0"},
        {"name": "movie_4", "size": 0, "md5": "d41d8cd98f00b204e9800998ecf8427e"},
        {"name": "toto", "size": 1, "md5": "0cc175b9c0f1b6a831c399e269772661", "renamed": True, "tata": "tutu"},
        {"name": "trololo", "size": 3, "md5": "900150983cd24fb0d6963f7d28e17f72", "ignore": True},
    ]
    # yapf: enable
    assert sorted(files_info, key=lambda a: a["name"]) == expected_files_info
示例#3
0
def test_app_get_template(tmpdir):
    """Tests that the template is used and that is correctly displayed."""
    data = [{
        "name": "toto",
        "image": "tutu",
        "link": "tata",
    }]
    static_folder = os.path.join(tmpdir, "static")
    os.makedirs(static_folder)
    data_path = os.path.join(static_folder, "data.json")
    write_json(data_path, data)

    with mock.patch("movexplo.app.DATA_PATH", data_path), app.test_client() as c:
        response = c.get("/")

    html = response.data.decode("utf-8")
    assert 'href="tata"' in html
    assert 'src="tutu"' in html
示例#4
0
def test_app_post(tmpdir):
    data = [{
        "name": "toto",
        "image": "tutu",
        "link": "tata",
    }]
    static_folder = os.path.join(tmpdir, "static")
    os.makedirs(static_folder)
    data_path = os.path.join(static_folder, "data.json")
    write_json(data_path, data)

    with mock.patch("movexplo.app.DATA_PATH", data_path), app.test_client() as c:
        response = c.post("/", data={"title": "toto"})

    html = response.data.decode("utf-8")
    assert 'href="tata"' in html
    assert 'src="tutu"' in html

    with mock.patch("movexplo.app.DATA_PATH", data_path), app.test_client() as c:
        response = c.post("/", data={"title": "laiuebfzleb"})

    html = response.data.decode("utf-8")
    assert 'href="tata"' not in html
    assert 'src="tutu"' not in html
示例#5
0
def test_get_files_info_no_input_folder(tmpdir):
    file_info_path = os.path.join(tmpdir, "file_info.json")
    expected_files_info = [{"toto": "tata"}]
    write_json(file_info_path, expected_files_info)
    files_info = get_files_info(file_info_path, None)
    assert files_info == expected_files_info
示例#6
0
def test_main(mockingbird, folder, tmpdir, enrich, force_enrich, entrypoint):
    files_info = [
        {
            "a": "b",
            "enriched": True,
            "name": "a",
            "md5": "0cc175b9c0f1b6a831c399e269772661"
        },
        {
            "c": "d",
            "enriched": False,
            "name": "c",
            "md5": "e2fc714c4727ee9395f324cd2e7f331f"
        },
        {
            "e": "f",
            "name": "e",
            "md5": "187ef4436122d1cc2f40dc2b92f0eba0"
        },
    ]
    input_file = os.path.join(tmpdir, "input.json")
    write_json(input_file, files_info)
    output_file = os.path.join(tmpdir, "output.json")

    if entrypoint == "main":
        with pytest.raises(
                FileNotFoundError
        ):  # nor output_file, input_folder and input_file exist
            main(output_file, None, None, enrich, force_enrich)

        open(output_file, "x")
        with pytest.raises(
                FileExistsError
        ):  # output_file exists and is different from input_file
            main(output_file, None, input_file, enrich, force_enrich)
        os.remove(output_file)

        main(output_file, folder, input_file, enrich, force_enrich)
    elif entrypoint == "typer_app":
        params = [output_file, folder, input_file]
        if enrich:
            params.append("--enrich")
        if force_enrich:
            params.append("--force-enrich")
        runner = CliRunner()
        result = runner.invoke(typer_app, params)
        assert result.exit_code == 0
    else:
        raise NotImplementedError(entrypoint)
    if force_enrich:
        expected_files_info = [
            {
                "name": "a",
                "size": 1,
                "md5": "0cc175b9c0f1b6a831c399e269772661",
                "renamed": True,
                "enriched": True,
                "mock_enriched": True,
                "a": "b"
            },
            {
                "name": "c",
                "size": 4,
                "md5": "e2fc714c4727ee9395f324cd2e7f331f",
                "c": "d",
                "enriched": False,
                "mock_enriched": True
            },
            {
                "name": "e",
                "size": 2,
                "md5": "187ef4436122d1cc2f40dc2b92f0eba0",
                "e": "f",
                "mock_enriched": True
            },
            {
                "name": "movie_3",
                "size": 3,
                "md5": "900150983cd24fb0d6963f7d28e17f72",
                "mock_enriched": True
            },
            {
                "name": "movie_4",
                "size": 0,
                "md5": "d41d8cd98f00b204e9800998ecf8427e",
                "mock_enriched": True
            },
        ]
    elif enrich:
        expected_files_info = [
            {
                "name": "a",
                "size": 1,
                "md5": "0cc175b9c0f1b6a831c399e269772661",
                "renamed": True,
                "enriched": True,
                "a": "b"
            },
            {
                "name": "c",
                "size": 4,
                "md5": "e2fc714c4727ee9395f324cd2e7f331f",
                "c": "d",
                "enriched": False,
                "mock_enriched": True
            },
            {
                "name": "e",
                "size": 2,
                "md5": "187ef4436122d1cc2f40dc2b92f0eba0",
                "e": "f",
                "mock_enriched": True
            },
            {
                "name": "movie_3",
                "size": 3,
                "md5": "900150983cd24fb0d6963f7d28e17f72",
                "mock_enriched": True
            },
            {
                "name": "movie_4",
                "size": 0,
                "md5": "d41d8cd98f00b204e9800998ecf8427e",
                "mock_enriched": True
            },
        ]
    else:
        expected_files_info = [
            {
                "name": "a",
                "size": 1,
                "md5": "0cc175b9c0f1b6a831c399e269772661",
                "renamed": True,
                "enriched": True,
                "a": "b"
            },
            {
                "name": "c",
                "size": 4,
                "md5": "e2fc714c4727ee9395f324cd2e7f331f",
                "c": "d",
                "enriched": False,
            },
            {
                "name": "e",
                "size": 2,
                "md5": "187ef4436122d1cc2f40dc2b92f0eba0",
                "e": "f",
            },
            {
                "name": "movie_3",
                "size": 3,
                "md5": "900150983cd24fb0d6963f7d28e17f72",
            },
            {
                "name": "movie_4",
                "size": 0,
                "md5": "d41d8cd98f00b204e9800998ecf8427e",
            },
        ]
    files_info = read_json(output_file)
    assert sorted(files_info, key=lambda a: a["name"]) == expected_files_info