From 9ff7313fab6a3957d6217b522c0c52ab1df163f3 Mon Sep 17 00:00:00 2001 From: Karol Latecki Date: Tue, 17 Jul 2018 12:00:20 +0200 Subject: [PATCH] scripts/rpc.py: pass named arguments in json rpc functions Add docstrings while at it. Change-Id: I2b723ae1f00d0a840a7498f5cabb6c33e60c652a Signed-off-by: Karol Latecki Reviewed-on: https://review.gerrithub.io/419506 Reviewed-by: Pawel Wodkowski Reviewed-by: Ben Walker Reviewed-by: Shuhei Matsumoto Tested-by: SPDK CI Jenkins Chandler-Test-Pool: SPDK Automated Test System --- scripts/rpc.py | 18 +++++++++---- scripts/rpc/__init__.py | 57 +++++++++++++++++++++++++++++++---------- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/scripts/rpc.py b/scripts/rpc.py index 2f1bb617b..2c952d440 100755 --- a/scripts/rpc.py +++ b/scripts/rpc.py @@ -49,7 +49,8 @@ if __name__ == "__main__": @call_cmd def get_rpc_methods(args): - print_dict(rpc.get_rpc_methods(args.client, args)) + print_dict(rpc.get_rpc_methods(args.client, + current=args.current)) p = subparsers.add_parser('get_rpc_methods', help='Get list of supported RPC methods') p.add_argument('-c', '--current', help='Get list of RPC methods only callable in the current state.', action='store_true') @@ -57,7 +58,9 @@ if __name__ == "__main__": @call_cmd def save_config(args): - rpc.save_config(args.client, args) + rpc.save_config(args.client, + filename=args.filename, + indent=args.indent) p = subparsers.add_parser('save_config', help="""Write current (live) configuration of SPDK subsystems and targets. If no filename is given write configuration to stdout.""") @@ -68,7 +71,8 @@ if __name__ == "__main__": @call_cmd def load_config(args): - rpc.load_config(args.client, args) + rpc.load_config(args.client, + filename=args.filename) p = subparsers.add_parser('load_config', help="""Configure SPDK subsystems and tagets using JSON RPC. If no file is provided or file is '-' read configuration from stdin.""") @@ -77,7 +81,10 @@ if __name__ == "__main__": @call_cmd def save_subsystem_config(args): - rpc.save_subsystem_config(args.client, args) + rpc.save_subsystem_config(args.client, + filename=args.filename, + indent=args.indent, + name=args.name) p = subparsers.add_parser('save_subsystem_config', help="""Write current (live) configuration of SPDK subsystem. If no filename is given write configuration to stdout.""") @@ -89,7 +96,8 @@ if __name__ == "__main__": @call_cmd def load_subsystem_config(args): - rpc.load_subsystem_config(args.client, args) + rpc.load_subsystem_config(args.client, + filename=args.filename) p = subparsers.add_parser('load_subsystem_config', help="""Configure SPDK subsystem using JSON RPC. If no file is provided or file is '-' read configuration from stdin.""") diff --git a/scripts/rpc/__init__.py b/scripts/rpc/__init__.py index 4f004aa01..4899eb620 100755 --- a/scripts/rpc/__init__.py +++ b/scripts/rpc/__init__.py @@ -17,14 +17,19 @@ from . import client as rpc_client def start_subsystem_init(client): + """Start initialization of subsystems""" return client.call('start_subsystem_init') -def get_rpc_methods(client, args): +def get_rpc_methods(client, current=None): + """Get list of supported RPC methods. + Args: + current: Get list of RPC methods only callable in the current state. + """ params = {} - if args.current: - params['current'] = args.current + if current: + params['current'] = current return client.call('get_rpc_methods', params) @@ -54,7 +59,15 @@ def _json_load(filename): return json.load(file) -def save_config(client, args): +def save_config(client, filename=None, indent=2): + """Write current (live) configuration of SPDK subsystems and targets. + Args: + filename: File where to save JSON configuration to. + Print to stdout if not provided. + indent: Indent level. Value less than 0 mean compact mode. + If filename is not given default then indent level is 2. + If writing to file of filename is '-' then default is compact mode. + """ config = { 'subsystems': [] } @@ -66,11 +79,16 @@ def save_config(client, args): } config['subsystems'].append(cfg) - _json_dump(config, args.filename, args.indent) + _json_dump(config, filename, indent) -def load_config(client, args): - json_config = _json_load(args.filename) +def load_config(client, filename=None): + """Configure SPDK subsystems and tagets using JSON RPC. + Args: + filename: JSON Configuration file location. + If no file path is provided or file is '-' then read configuration from stdin. + """ + json_config = _json_load(filename) # remove subsystems with no config subsystems = json_config['subsystems'] @@ -114,17 +132,30 @@ def load_config(client, args): print("Some configs were skipped because the RPC state that can call them passed over.") -def save_subsystem_config(client, args): +def save_subsystem_config(client, filename=None, indent=2, name=None): + """Write current (live) configuration of SPDK subsystem. + Args: + filename: File where to save JSON configuration to. + Print to stdout if not provided. + indent: Indent level. Value less than 0 mean compact mode. + If filename is not given default then indent level is 2. + If writing to file of filename is '-' then default is compact mode. + """ cfg = { - 'subsystem': args.name, - 'config': client.call('get_subsystem_config', {"name": args.name}) + 'subsystem': name, + 'config': client.call('get_subsystem_config', {"name": name}) } - _json_dump(cfg, args.filename, args.indent) + _json_dump(cfg, filename, indent) -def load_subsystem_config(client, args): - subsystem = _json_load(args.filename) +def load_subsystem_config(client, filename=None): + """Configure SPDK subsystem using JSON RPC. + Args: + filename: JSON Configuration file location. + If no file path is provided or file is '-' then read configuration from stdin. + """ + subsystem = _json_load(filename) if not subsystem['config']: return