File size: 2,620 Bytes
b400ace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import json
import os
from copy import deepcopy

from freezegun import freeze_time

from main import entry_point_for_args

FROZEN_TIMESTAMP = "1970-01-01"


def setup_mocker_patches(mocker):
    mock_imshow = mocker.patch("cv2.imshow")
    mock_imshow.return_value = True

    mock_destroy_all_windows = mocker.patch("cv2.destroyAllWindows")
    mock_destroy_all_windows.return_value = True

    mock_wait_key = mocker.patch("cv2.waitKey")
    mock_wait_key.return_value = ord("q")


def run_entry_point(input_path, output_dir):
    args = {
        "autoAlign": False,
        "debug": False,
        "input_paths": [input_path],
        "output_dir": output_dir,
        "setLayout": False,
        "silent": True,
    }
    with freeze_time(FROZEN_TIMESTAMP):
        entry_point_for_args(args)


def write_modified(modify_content, boilerplate, sample_json_path):
    if boilerplate is None:
        return

    content = deepcopy(boilerplate)

    if modify_content is not None:
        returned_value = modify_content(content)
        if returned_value is not None:
            content = returned_value

    with open(sample_json_path, "w") as f:
        json.dump(content, f)


def remove_file(path):
    if os.path.exists(path):
        os.remove(path)


def generate_write_jsons_and_run(
    run_sample,
    sample_path,
    template_boilerplate=None,
    config_boilerplate=None,
    evaluation_boilerplate=None,
):
    if (template_boilerplate or config_boilerplate or evaluation_boilerplate) is None:
        raise Exception(
            f"No boilerplates found. Provide atleast one boilerplate to write json."
        )

    def write_jsons_and_run(
        mocker,
        modify_template=None,
        modify_config=None,
        modify_evaluation=None,
    ):
        sample_template_path, sample_config_path, sample_evaluation_path = (
            sample_path.joinpath("template.json"),
            sample_path.joinpath("config.json"),
            sample_path.joinpath("evaluation.json"),
        )
        write_modified(modify_template, template_boilerplate, sample_template_path)
        write_modified(modify_config, config_boilerplate, sample_config_path)
        write_modified(
            modify_evaluation, evaluation_boilerplate, sample_evaluation_path
        )

        exception = "No Error"
        try:
            run_sample(mocker, sample_path)
        except Exception as e:
            exception = e

        remove_file(sample_template_path)
        remove_file(sample_config_path)
        remove_file(sample_evaluation_path)

        return exception

    return write_jsons_and_run