Change-Id: Ib4e9a43381af29b0ca1e2eee05de4a4976dd8794
This commit is contained in:
Lisa Zangrando 2016-08-31 15:22:36 +02:00
parent 3c33d51dd4
commit 4771a6e490
6 changed files with 16 additions and 38 deletions

View File

@ -1,7 +1,7 @@
import json import json
import requests import requests
from synergy.common import utils from synergy.common.utils import objectHookHandler
from tabulate import tabulate from tabulate import tabulate
@ -35,32 +35,16 @@ class HTTPCommand(object):
def configureParser(self, subparser): def configureParser(self, subparser):
raise NotImplementedError("not implemented!") raise NotImplementedError("not implemented!")
def objectHookHandler(self, parsed_dict):
if "synergy_object" in parsed_dict:
synergy_object = parsed_dict["synergy_object"]
try:
objClass = utils.import_class(synergy_object["name"])
objInstance = objClass()
return objInstance.deserialize(parsed_dict)
except Exception as ex:
print(ex)
raise ex
else:
return parsed_dict
def execute(self, synergy_url, payload=None): def execute(self, synergy_url, payload=None):
request = requests.get(synergy_url, params=payload) request = requests.get(synergy_url, params=payload)
if request.status_code != requests.codes.ok: if request.status_code != requests.codes.ok:
# print(request.reason)
# print(request.status_code)
request.raise_for_status() request.raise_for_status()
self.results = request.json() self.results = request.json()
try: try:
return json.loads(request.text, object_hook=self.objectHookHandler) return json.loads(request.text, object_hook=objectHookHandler)
except Exception: except Exception:
return request.json() return request.json()

View File

@ -176,7 +176,6 @@ def main():
print("command %r not found!" % command_name) print("command %r not found!" % command_name)
commands[command_name].execute(synergy_url, args) commands[command_name].execute(synergy_url, args)
# commands[command_name].log()
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
print("Shutting down synergyclient") print("Shutting down synergyclient")
sys.exit(1) sys.exit(1)

View File

@ -92,11 +92,9 @@ class SynergyObject(object):
try: try:
objName = synergy_object["name"] objName = synergy_object["name"]
# objVer = synergy_object['version']
objClass = utils.import_class(objName) objClass = utils.import_class(objName)
objInstance = objClass() objInstance = objClass()
except Exception as ex: except Exception as ex:
print(ex)
raise Exception("error on deserializing the object %r: %s" raise Exception("error on deserializing the object %r: %s"
% (objName, ex)) % (objName, ex))

View File

@ -31,3 +31,16 @@ def import_class(import_str):
raise ImportError( raise ImportError(
'Class %s cannot be found (%s)' % 'Class %s cannot be found (%s)' %
(class_str, traceback.format_exception(*sys.exc_info()))) (class_str, traceback.format_exception(*sys.exc_info())))
def objectHookHandler(parsed_dict):
if "synergy_object" in parsed_dict:
synergy_object = parsed_dict["synergy_object"]
try:
objClass = import_class(synergy_object["name"])
objInstance = objClass()
return objInstance.deserialize(parsed_dict)
except Exception as ex:
raise ex
else:
return parsed_dict

View File

@ -23,7 +23,7 @@ import sys
import time import time
from mock import Mock from mock import Mock
from synergy.common import utils from synergy.common.utils import objectHookHandler
from synergy.service import Synergy from synergy.service import Synergy
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
@ -48,21 +48,6 @@ def getLogger(name):
return logger return logger
def objectHookHandler(parsed_dict):
if "synergy_object" in parsed_dict:
synergy_object = parsed_dict["synergy_object"]
try:
objClass = utils.import_class(synergy_object["name"])
objInstance = objClass()
return objInstance.deserialize(parsed_dict)
except Exception as ex:
print(ex)
raise ex
else:
return parsed_dict
class SynergyTests(unittest.TestCase): class SynergyTests(unittest.TestCase):
@mock.patch('synergy.service.LOG', LOG) @mock.patch('synergy.service.LOG', LOG)

View File

@ -13,5 +13,4 @@ testrepository>=0.0.18
testscenarios>=0.4 testscenarios>=0.4
testtools>=1.4.0 testtools>=1.4.0
mock==2.0.0 mock==2.0.0
tabulate>=0.7.2,<0.8.0