Skip to content

Commit 0d71253

Browse files
committed
Extract method for parsing/validating options.params/option.json_file
Signed-off-by: Bernd Hufmann <[email protected]>
1 parent 92b841f commit 0d71253

File tree

1 file changed

+27
-51
lines changed

1 file changed

+27
-51
lines changed

tsp_cli_client

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ def __parse_params (parameters_string):
108108

109109
return _params
110110

111+
def __get_parameters(options):
112+
if not options.params and not options.json_file:
113+
print("No parameters provided with --params or --json-file")
114+
sys.exit(1)
115+
116+
_params = {}
117+
if options.params is not None:
118+
_params = __parse_params(options.params)
119+
120+
if options.json_file is not None:
121+
with open(options.json_file, 'r') as file:
122+
_params = json.loads(file.read())
123+
124+
if not _params:
125+
print("Invalid params provided")
126+
sys.exit(1)
127+
return _params
128+
111129

112130
DESCRIPTION = """CLI client to send Trace Server Protocol commands to a Trace Server."""
113131

@@ -523,21 +541,7 @@ if __name__ == "__main__":
523541
print("No type-id for loading of configuration provided")
524542
sys.exit(1)
525543

526-
if not options.params and not options.json_file:
527-
print("No parameters provided with --params or --json-file")
528-
sys.exit(1)
529-
530-
params = {}
531-
if options.params is not None:
532-
params = __parse_params(options.params)
533-
534-
if options.json_file is not None:
535-
with open(options.json_file, 'r') as file:
536-
params = json.loads(file.read())
537-
538-
if not params:
539-
print("Invalid params provided")
540-
sys.exit(1)
544+
params = __get_parameters(options)
541545

542546
response = tsp_client.post_configuration(options.type_id, params)
543547
if response.status_code == 200:
@@ -557,21 +561,7 @@ if __name__ == "__main__":
557561
print("No config-id of configuration provided")
558562
sys.exit(1)
559563

560-
if not options.params and not options.json_file:
561-
print("No parameters provided with --params or --json-file")
562-
sys.exit(1)
563-
564-
params = {}
565-
if options.params is not None:
566-
params = __parse_params(options.params)
567-
568-
if options.json_file is not None:
569-
with open(options.json_file, 'r') as file:
570-
params = json.loads(file.read())
571-
572-
if not params:
573-
print("Invalid params provided")
574-
sys.exit(1)
564+
params = __get_parameters(options)
575565

576566
response = tsp_client.put_configuration(options.type_id, options.config_id, params)
577567
if response.status_code == 200:
@@ -640,29 +630,15 @@ if __name__ == "__main__":
640630
print(TRACE_MISSING)
641631
sys.exit(1)
642632

643-
if not options.params and not options.json_file:
644-
print("No parameters provided with --params or --json-file")
645-
sys.exit(1)
646-
647-
params = {}
648-
if options.params is not None:
649-
params = __parse_params(options.params)
650-
651-
if options.json_file is not None:
652-
with open(options.json_file, 'r') as file:
653-
params = json.loads(file.read())
633+
params = __get_parameters(options)
654634

655-
if (params is not None):
656-
response = tsp_client.create_derived_output(options.uuid, options.create_output, params)
657-
if response.status_code == 200:
658-
print('Successfully created derived output')
659-
print('-----------------------------------')
660-
print(response.model.to_json())
661-
sys.exit(0)
662-
else:
663-
sys.exit(1)
635+
response = tsp_client.create_derived_output(options.uuid, options.create_output, params)
636+
if response.status_code == 200:
637+
print('Successfully created derived output')
638+
print('-----------------------------------')
639+
print(response.model.to_json())
640+
sys.exit(0)
664641
else:
665-
print("Invalid params provided")
666642
sys.exit(1)
667643

668644
if options.delete_output:

0 commit comments

Comments
 (0)