Passed
Push — master ( 670862...a75791 )
by Konstantin
02:31
created

ocrd_network.process_helpers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 40
dl 0
loc 48
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B invoke_processor() 0 39 6
1
import json
2
from typing import List
3
4
from ocrd import Resolver
5
from ocrd.processor.helpers import run_cli, run_processor
6
7
8
# A wrapper for run_processor() and run_cli()
9
def invoke_processor(
10
        processor_class,
11
        executable: str,
12
        abs_path_to_mets: str,
13
        input_file_grps: List[str],
14
        output_file_grps: List[str],
15
        page_id: str,
16
        parameters: dict,
17
) -> None:
18
    if not (processor_class or executable):
19
        raise ValueError(f'Missing processor class and executable')
20
    input_file_grps_str = ','.join(input_file_grps)
21
    output_file_grps_str = ','.join(output_file_grps)
22
    workspace = Resolver().workspace_from_url(abs_path_to_mets)
23
    if processor_class:
24
        try:
25
            run_processor(
26
                processorClass=processor_class,
27
                workspace=workspace,
28
                input_file_grp=input_file_grps_str,
29
                output_file_grp=output_file_grps_str,
30
                page_id=page_id,
31
                parameter=parameters,
32
                instance_caching=True
33
            )
34
        except Exception as e:
35
            raise RuntimeError(f"Python executable '{executable}' exited with: {e}")
36
    else:
37
        return_code = run_cli(
38
            executable=executable,
39
            workspace=workspace,
40
            mets_url=abs_path_to_mets,
41
            input_file_grp=input_file_grps_str,
42
            output_file_grp=output_file_grps_str,
43
            page_id=page_id,
44
            parameter=json.dumps(parameters)
45
        )
46
        if return_code != 0:
47
            raise RuntimeError(f"CLI executable '{executable}' exited with: {return_code}")
48