summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/resources/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'testapi/opnfv_testapi/resources/models.py')
-rw-r--r--testapi/opnfv_testapi/resources/models.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/testapi/opnfv_testapi/resources/models.py b/testapi/opnfv_testapi/resources/models.py
index e8fc532..6f04cc2 100644
--- a/testapi/opnfv_testapi/resources/models.py
+++ b/testapi/opnfv_testapi/resources/models.py
@@ -48,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(v)
+ elif isinstance(v, list) and k in attr_parser:
+ value = []
+ for item in v:
+ value.append(attr_parser[k].from_dict(item))
+
+ t.__setattr__(k, value)
+
+ return t
+
@staticmethod
def attr_parser():
return {}