Move json.loads() method to utils.py, and use "with" to deal with file objects

Change-Id: I030027a9545a7344a2364c86ca438803dd27ac87
This commit is contained in:
fengchaoyang 2016-11-30 00:35:13 +08:00
parent 316db62e9c
commit 2e55d3a89f
5 changed files with 21 additions and 22 deletions

View File

@ -151,10 +151,7 @@ class Create(command.ShowOne):
action_input = None action_input = None
if parsed_args.input: if parsed_args.input:
try: action_input = utils.load_json(parsed_args.input)
action_input = json.loads(parsed_args.input)
except Exception:
action_input = json.load(open(parsed_args.input))
mistral_client = self.app.client_manager.workflow_engine mistral_client = self.app.client_manager.workflow_engine
action_ex = mistral_client.action_executions.create( action_ex = mistral_client.action_executions.create(
@ -240,10 +237,7 @@ class Update(command.ShowOne):
def take_action(self, parsed_args): def take_action(self, parsed_args):
output = None output = None
if parsed_args.output: if parsed_args.output:
try: output = utils.load_json(parsed_args.output)
output = json.loads(parsed_args.output)
except Exception:
output = json.load(open(parsed_args.output))
mistral_client = self.app.client_manager.workflow_engine mistral_client = self.app.client_manager.workflow_engine
execution = mistral_client.action_executions.update( execution = mistral_client.action_executions.update(

View File

@ -14,8 +14,6 @@
# under the License. # under the License.
# #
import json
from osc_lib.command import command from osc_lib.command import command
from mistralclient.commands.v2 import base from mistralclient.commands.v2 import base
@ -140,10 +138,7 @@ class Create(command.ShowOne):
@staticmethod @staticmethod
def _get_file_content_or_dict(string): def _get_file_content_or_dict(string):
if string: if string:
try: return utils.load_json(string)
return json.loads(string)
except Exception:
return json.load(open(string))
else: else:
return {} return {}

View File

@ -170,18 +170,12 @@ class Create(command.ShowOne):
def take_action(self, parsed_args): def take_action(self, parsed_args):
if parsed_args.workflow_input: if parsed_args.workflow_input:
try: wf_input = utils.load_json(parsed_args.workflow_input)
wf_input = json.loads(parsed_args.workflow_input)
except Exception:
wf_input = json.load(open(parsed_args.workflow_input))
else: else:
wf_input = {} wf_input = {}
if parsed_args.params: if parsed_args.params:
try: params = utils.load_json(parsed_args.params)
params = json.loads(parsed_args.params)
except Exception:
params = json.load(open(parsed_args.params))
else: else:
params = {} params = {}

View File

@ -55,3 +55,11 @@ class UtilityTest(base.BaseTestCase):
file_path = os.path.abspath(f.name) file_path = os.path.abspath(f.name)
self.assertDictEqual(ENV_DICT, utils.load_file(file_path)) 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))

View File

@ -79,3 +79,11 @@ def get_contents_if_file(contents_or_file_name):
return request.urlopen(definition_url).read().decode('utf8') return request.urlopen(definition_url).read().decode('utf8')
except Exception: except Exception:
return contents_or_file_name 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)