diff --git a/mistralclient/commands/v2/action_executions.py b/mistralclient/commands/v2/action_executions.py index 8dc72b9e..cdef79d5 100644 --- a/mistralclient/commands/v2/action_executions.py +++ b/mistralclient/commands/v2/action_executions.py @@ -151,10 +151,7 @@ class Create(command.ShowOne): action_input = None if parsed_args.input: - try: - action_input = json.loads(parsed_args.input) - except Exception: - action_input = json.load(open(parsed_args.input)) + action_input = utils.load_json(parsed_args.input) mistral_client = self.app.client_manager.workflow_engine action_ex = mistral_client.action_executions.create( @@ -240,10 +237,7 @@ class Update(command.ShowOne): def take_action(self, parsed_args): output = None if parsed_args.output: - try: - output = json.loads(parsed_args.output) - except Exception: - output = json.load(open(parsed_args.output)) + output = utils.load_json(parsed_args.output) mistral_client = self.app.client_manager.workflow_engine execution = mistral_client.action_executions.update( diff --git a/mistralclient/commands/v2/cron_triggers.py b/mistralclient/commands/v2/cron_triggers.py index 10379eb7..a48c7d68 100644 --- a/mistralclient/commands/v2/cron_triggers.py +++ b/mistralclient/commands/v2/cron_triggers.py @@ -14,8 +14,6 @@ # under the License. # -import json - from osc_lib.command import command from mistralclient.commands.v2 import base @@ -140,10 +138,7 @@ class Create(command.ShowOne): @staticmethod def _get_file_content_or_dict(string): if string: - try: - return json.loads(string) - except Exception: - return json.load(open(string)) + return utils.load_json(string) else: return {} diff --git a/mistralclient/commands/v2/executions.py b/mistralclient/commands/v2/executions.py index c49bd925..93068de8 100644 --- a/mistralclient/commands/v2/executions.py +++ b/mistralclient/commands/v2/executions.py @@ -170,18 +170,12 @@ class Create(command.ShowOne): def take_action(self, parsed_args): if parsed_args.workflow_input: - try: - wf_input = json.loads(parsed_args.workflow_input) - except Exception: - wf_input = json.load(open(parsed_args.workflow_input)) + wf_input = utils.load_json(parsed_args.workflow_input) else: wf_input = {} if parsed_args.params: - try: - params = json.loads(parsed_args.params) - except Exception: - params = json.load(open(parsed_args.params)) + params = utils.load_json(parsed_args.params) else: params = {} diff --git a/mistralclient/tests/unit/test_utils.py b/mistralclient/tests/unit/test_utils.py index e52ad5eb..51343d6d 100644 --- a/mistralclient/tests/unit/test_utils.py +++ b/mistralclient/tests/unit/test_utils.py @@ -55,3 +55,11 @@ class UtilityTest(base.BaseTestCase): file_path = os.path.abspath(f.name) self.assertDictEqual(ENV_DICT, utils.load_file(file_path)) + + def test_load_json(self): + with tempfile.NamedTemporaryFile() as f: + f.write(ENV_STR.encode('utf-8')) + f.flush() + self.assertDictEqual(ENV_DICT, utils.load_json(f.name)) + + self.assertDictEqual(ENV_DICT, utils.load_json(ENV_STR)) diff --git a/mistralclient/utils.py b/mistralclient/utils.py index cdc73e2b..4459765e 100644 --- a/mistralclient/utils.py +++ b/mistralclient/utils.py @@ -79,3 +79,11 @@ def get_contents_if_file(contents_or_file_name): return request.urlopen(definition_url).read().decode('utf8') except Exception: return contents_or_file_name + + +def load_json(input_string): + try: + with open(input_string) as fh: + return json.load(fh) + except IOError: + return json.loads(input_string)