zun-tempest-plugin/zun_tempest_plugin/tests/tempest/api/common/base_model.py
Kien Nguyen 107c4a1805 Support python 3.5
Port python 2 code to python 3 depend on [1]:

- Replace unicode with six.text_type
- Replace json.dumps(obj) with oslo_serialization.jsonutils.dump_as_bytes(obj).
- Replace json.loads(obj) with oslo_serialization.jsonutils.loads(obj).

Change-Id: Id36e723f86427756c7092ae30de50fec9afee735
Implements: blueprint support-python-35
2017-07-05 10:43:46 +07:00

71 lines
2.2 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_serialization import jsonutils as json
class BaseModel(object):
"""Superclass responsible for converting json data to/from model"""
@classmethod
def from_json(cls, json_str):
return cls.from_dict(json.loads(json_str))
def to_json(self):
return json.dump_as_bytes(self.to_dict())
@classmethod
def from_dict(cls, data):
model = cls()
for key in data:
setattr(model, key, data.get(key))
return model
def to_dict(self):
result = {}
for key in self.__dict__:
result[key] = getattr(self, key)
if isinstance(result[key], BaseModel):
result[key] = result[key].to_dict()
return result
def __str__(self):
return "%s" % self.to_dict()
class EntityModel(BaseModel):
"""Superclass resposible from converting dict to instance of model"""
@classmethod
def from_dict(cls, data):
model = super(EntityModel, cls).from_dict(data)
if hasattr(model, cls.ENTITY_NAME):
val = getattr(model, cls.ENTITY_NAME)
setattr(model, cls.ENTITY_NAME, cls.MODEL_TYPE.from_dict(val))
return model
class CollectionModel(BaseModel):
"""Superclass resposible from converting dict to list of models"""
@classmethod
def from_dict(cls, data):
model = super(CollectionModel, cls).from_dict(data)
collection = []
if hasattr(model, cls.COLLECTION_NAME):
for d in getattr(model, cls.COLLECTION_NAME):
collection.append(cls.MODEL_TYPE.from_dict(d))
setattr(model, cls.COLLECTION_NAME, collection)
return model