X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=utils%2Ftest%2Ftestapi%2Fopnfv_testapi%2Fresources%2Fmodels.py;h=e70a6ed23fbd5d85f1587236d434befcbfde31ea;hb=c83ab03b1b81623a8bedb9763b32de01ec492233;hp=0ea482fd2e3033b11e51bf4438b228be294a9c5d;hpb=f2ee1d5e8d258da2c2ffdf8bf33f0a8f9fd8dcb0;p=releng.git diff --git a/utils/test/testapi/opnfv_testapi/resources/models.py b/utils/test/testapi/opnfv_testapi/resources/models.py index 0ea482fd2..e70a6ed23 100644 --- a/utils/test/testapi/opnfv_testapi/resources/models.py +++ b/utils/test/testapi/opnfv_testapi/resources/models.py @@ -14,9 +14,8 @@ # feng.xiaowei@zte.com.cn mv TestResut to result_models.py 5-23-2016 # feng.xiaowei@zte.com.cn add ModelBase 12-20-2016 ############################################################################## -import copy import ast - +import copy from opnfv_testapi.tornado_swagger import swagger @@ -49,6 +48,29 @@ class ModelBase(object): return t + @classmethod + def from_dict_with_raise(cls, a_dict): + if a_dict is None: + return None + + attr_parser = cls.attr_parser() + t = cls() + for k, v in a_dict.iteritems(): + if k not in t.__dict__: + raise AttributeError( + '{} has no attribute {}'.format(cls.__name__, k)) + value = v + if isinstance(v, dict) and k in attr_parser: + value = attr_parser[k].from_dict_with_raise(v) + elif isinstance(v, list) and k in attr_parser: + value = [] + for item in v: + value.append(attr_parser[k].from_dict_with_raise(item)) + + t.__setattr__(k, value) + + return t + @staticmethod def attr_parser(): return {}