Passed
Pull Request — master (#1030)
by Konstantin
02:43
created

ocrd_network.cli.client   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 65
dl 0
loc 102
rs 10
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A discovery_cli() 0 6 1
B send_processing_request() 0 50 6
A workflow_cli() 0 6 1
A client_cli() 0 7 1
A processing_cli() 0 6 1
A workspace_cli() 0 6 1
1
import click
2
from typing import Optional
3
4
from ocrd.decorators import (
5
    parameter_option,
6
    parameter_override_option
7
)
8
from ocrd_network import Client
9
10
11
@click.group('client')
12
def client_cli():
13
    """
14
    A client for interacting with the network modules.
15
    The client CLI mimics the WebAPI endpoints
16
    """
17
    pass
18
19
20
@client_cli.group('discovery')
21
def discovery_cli():
22
    """
23
    The discovery endpoint of the WebAPI
24
    """
25
    pass
26
27
28
@client_cli.group('processing')
29
def processing_cli():
30
    """
31
    The processing endpoint of the WebAPI
32
    """
33
    pass
34
35
36
@processing_cli.command('processor')
37
@click.argument('processor_name', required=True, type=click.STRING)
38
@click.option('--address')
39
@click.option('-m', '--mets', required=True, default="mets.xml")
40
@click.option('-I', '--input-file-grp', default='OCR-D-INPUT')
41
@click.option('-O', '--output-file-grp', default='OCR-D-OUTPUT')
42
@click.option('-g', '--page-id')
43
@parameter_option
44
@click.option('--result-queue-name')
45
@click.option('--callback-url')
46
@click.option('--agent-type', default='worker')
47
def send_processing_request(
48
        address: Optional[str],
49
        processor_name: str,
50
        mets: str,
51
        input_file_grp: str,
52
        output_file_grp: Optional[str],
53
        page_id: Optional[str],
54
        parameter: Optional[dict],
55
        result_queue_name: Optional[str],
56
        callback_url: Optional[str],
57
        # TODO: This is temporally available to toggle
58
        #  between the ProcessingWorker/ProcessorServer
59
        agent_type: Optional[str]
60
):
61
    req_params = {
62
        "path_to_mets": mets,
63
        "description": "OCR-D Network client request",
64
        "input_file_grps": input_file_grp.split(','),
65
        "parameters": parameter if parameter else {},
66
        "agent_type": agent_type,
67
    }
68
    if output_file_grp:
69
        req_params["output_file_grps"] = output_file_grp.split(',')
70
    if page_id:
71
        req_params["page_id"] = page_id
72
    if result_queue_name:
73
        req_params["result_queue_name"] = result_queue_name
74
    if callback_url:
75
        req_params["callback_url"] = callback_url
76
77
    client = Client(
78
        server_addr_processing=address
79
    )
80
    response = client.send_processing_request(
81
        processor_name=processor_name,
82
        req_params=req_params
83
    )
84
    processing_job_id = response.get('job_id', None)
85
    print(f"Processing job id: {processing_job_id}")
86
87
88
@client_cli.group('workflow')
89
def workflow_cli():
90
    """
91
    The workflow endpoint of the WebAPI
92
    """
93
    pass
94
95
96
@client_cli.group('workspace')
97
def workspace_cli():
98
    """
99
    The workspace endpoint of the WebAPI
100
    """
101
    pass
102