| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 4 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
"""cylc client [OPTIONS] ARGS |
| 20 |
|
| 21 |
(This command is for internal use.) |
| 22 |
|
| 23 |
Invoke workflow runtime client, expect JSON from STDIN for keyword arguments. |
| 24 |
Use the -n option if client function requires no keyword arguments. |
| 25 |
""" |
| 26 |
|
| 27 |
from google.protobuf.json_format import MessageToDict |
| 28 |
import json |
| 29 |
import sys |
| 30 |
from typing import TYPE_CHECKING, cast |
| 31 |
|
| 32 |
from cylc.flow.id_cli import parse_id |
| 33 |
from cylc.flow.option_parsers import ( |
| 34 |
WORKFLOW_ID_ARG_DOC, |
| 35 |
CylcOptionParser as COP, |
| 36 |
) |
| 37 |
from cylc.flow.network.client import WorkflowRuntimeClient |
| 38 |
from cylc.flow.network.server import PB_METHOD_MAP |
| 39 |
from cylc.flow.terminal import cli_function |
| 40 |
|
| 41 |
if TYPE_CHECKING: |
| 42 |
from optparse import Values |
| 43 |
from google.protobuf.message import Message |
| 44 |
|
| 45 |
|
| 46 |
INTERNAL = True |
| 47 |
|
| 48 |
|
| 49 |
def get_option_parser(): |
| 50 |
parser = COP( |
| 51 |
__doc__, comms=True, |
| 52 |
argdoc=[ |
| 53 |
WORKFLOW_ID_ARG_DOC, |
| 54 |
('METHOD', 'Network API function name') |
| 55 |
] |
| 56 |
) |
| 57 |
|
| 58 |
parser.add_option( |
| 59 |
'-n', '--no-input', |
| 60 |
help='Do not read from STDIN, assume null input', |
| 61 |
action='store_true', dest='no_input') |
| 62 |
|
| 63 |
return parser |
| 64 |
|
| 65 |
|
| 66 |
@cli_function(get_option_parser) |
| 67 |
def main(_, options: 'Values', workflow_id: str, func: str) -> None: |
| 68 |
workflow_id, *_ = parse_id( |
| 69 |
workflow_id, |
| 70 |
constraint='workflows', |
| 71 |
) |
| 72 |
pclient = WorkflowRuntimeClient(workflow_id, timeout=options.comms_timeout) |
| 73 |
if options.no_input: |
| 74 |
kwargs = {} |
| 75 |
else: |
| 76 |
kwargs = json.load(sys.stdin) |
| 77 |
sys.stdin.close() |
| 78 |
res = pclient(func, kwargs) |
| 79 |
if func in PB_METHOD_MAP: |
| 80 |
pb_msg: Message |
| 81 |
if 'element_type' in kwargs: |
| 82 |
pb_msg = PB_METHOD_MAP[func][kwargs['element_type']]() |
| 83 |
else: |
| 84 |
pb_msg = PB_METHOD_MAP[func]() |
| 85 |
pb_msg.ParseFromString(cast('bytes', res)) |
| 86 |
res_msg: object = MessageToDict(pb_msg) |
| 87 |
else: |
| 88 |
res_msg = res |
| 89 |
sys.stdout.write(json.dumps(res_msg, indent=4) + '\n') |