From 324ade3638484f5dec9c4afb9b7796d52989de6b Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Wed, 19 Aug 2020 23:24:19 +0000 Subject: [PATCH] Pull out parsing functions --- .../interop/xds_interop_client.py | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/python/grpcio_tests/tests_py3_only/interop/xds_interop_client.py b/src/python/grpcio_tests/tests_py3_only/interop/xds_interop_client.py index 0c4d7ee387b..8919ac03a31 100644 --- a/src/python/grpcio_tests/tests_py3_only/interop/xds_interop_client.py +++ b/src/python/grpcio_tests/tests_py3_only/interop/xds_interop_client.py @@ -271,6 +271,28 @@ def _run(args: argparse.Namespace, methods: Sequence[str], method_handle.stop() +def parse_metadata_arg(metadata_arg: str) -> PerMethodMetadataType: + metadata = metadata_arg.split(",") if args.metadata else [] + per_method_metadata = collections.defaultdict(list) + for metadatum in metadata: + elems = metadatum.split(":") + if len(elems) != 3: + raise ValueError( + f"'{metadatum}' was not in the form 'METHOD:KEY:VALUE'") + if elems[0] not in _SUPPORTED_METHODS: + raise ValueError(f"Unrecognized method '{elems[0]}'") + per_method_metadata[elems[0]].append((elems[1], elems[2])) + return per_method_metadata + + +def parse_rpc_arg(rpc_arg: str) -> Sequence[str]: + methods = rpc_arg.split(",") + if set(methods) - set(_SUPPORTED_METHODS): + raise ValueError("--rpc supported methods: {}".format( + ", ".join(_SUPPORTED_METHODS))) + return methods + + if __name__ == "__main__": parser = argparse.ArgumentParser( description='Run Python XDS interop client.') @@ -325,18 +347,4 @@ if __name__ == "__main__": file_handler = logging.FileHandler(args.log_file, mode='a') file_handler.setFormatter(formatter) logger.addHandler(file_handler) - methods = args.rpc.split(",") - if set(methods) - set(_SUPPORTED_METHODS): - raise ValueError("--rpc supported methods: {}".format( - ", ".join(_SUPPORTED_METHODS))) - per_method_metadata = collections.defaultdict(list) - metadata = args.metadata.split(",") if args.metadata else [] - for metadatum in metadata: - elems = metadatum.split(":") - if len(elems) != 3: - raise ValueError( - f"'{metadatum}' was not in the form 'METHOD:KEY:VALUE'") - if elems[0] not in _SUPPORTED_METHODS: - raise ValueError(f"Unrecognized method '{elems[0]}'") - per_method_metadata[elems[0]].append((elems[1], elems[2])) - _run(args, methods, per_method_metadata) + _run(args, parse_rpc_arg(args.rpc), parse_metadata_arg(args.metadata))