Refactored object storage account and container list models
Change-Id: Ib3a7a964f64d90289355eec17ae3c8ad39cde50d
This commit is contained in:
@@ -15,85 +15,80 @@ limitations under the License.
|
||||
"""
|
||||
import json
|
||||
from xml.etree import ElementTree
|
||||
from cafe.engine.models.base import AutoMarshallingModel
|
||||
from cafe.engine.models.base import AutoMarshallingListModel
|
||||
|
||||
|
||||
class AccountContainersList(AutoMarshallingModel):
|
||||
pass
|
||||
class StorageObject(object):
|
||||
def __init__(self, name, bytes_, hash_, last_modified, content_type):
|
||||
self.name = name
|
||||
self.bytes_ = bytes_
|
||||
self.hash_ = hash_
|
||||
self.last_modified = last_modified
|
||||
self.content_type = content_type
|
||||
|
||||
class _Container(object):
|
||||
def __init__(self, name=None, count=None, bytes=None):
|
||||
self.name = None
|
||||
self.count = None
|
||||
self.bytes = None
|
||||
|
||||
def __init__(self):
|
||||
'''This is a deserializing object only'''
|
||||
pass
|
||||
class Container(object):
|
||||
def __init__(self, name=None, count=None, bytes_=None):
|
||||
self.name = name
|
||||
self.count = count
|
||||
self.bytes_ = bytes_
|
||||
|
||||
|
||||
class AccountContainersList(AutoMarshallingListModel):
|
||||
|
||||
@classmethod
|
||||
def _xml_to_obj(cls, serialized_str):
|
||||
ret = []
|
||||
root = ElementTree.fromstring(serialized_str)
|
||||
setattr(cls, 'name', root.attrib['name'])
|
||||
data = []
|
||||
for child in root:
|
||||
container_dict = {}
|
||||
account_container_dict = {}
|
||||
for sub_child in child:
|
||||
container_dict[sub_child.tag] = sub_child.text
|
||||
ret.append(cls._StorageObject(**container_dict))
|
||||
return ret
|
||||
account_container_dict[sub_child.tag] = sub_child.text
|
||||
data.append(account_container_dict)
|
||||
return cls._list_to_obj(data)
|
||||
|
||||
@classmethod
|
||||
def _json_to_obj(cls, serialized_str):
|
||||
ret = []
|
||||
data = json.loads(serialized_str)
|
||||
for container in data:
|
||||
ret.append(
|
||||
cls._Container(
|
||||
name=container.get('name'),
|
||||
bytes=container.get('bytes'),
|
||||
count=container.get('count')))
|
||||
return ret
|
||||
return cls._list_to_obj(data)
|
||||
|
||||
def _list_to_obj(cls, data):
|
||||
account_containers_list = AccountContainersList()
|
||||
for obj in data:
|
||||
container = Container(
|
||||
name=obj.get('name'),
|
||||
bytes_=obj.get('bytes'),
|
||||
content_type=obj.get('count'))
|
||||
account_containers_list.append(container)
|
||||
return account_containers_list
|
||||
|
||||
|
||||
class ContainerObjectsList(AutoMarshallingModel):
|
||||
#TODO: make this not use *args and **kwargs
|
||||
class _StorageObject(dict):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.name = kwargs.get('name', None)
|
||||
self.bytes = kwargs.get('bytes', None)
|
||||
self.hash = kwargs.get('hash', None)
|
||||
self.last_modified = kwargs.get('last_modified', None)
|
||||
self.content_type = kwargs.get('content_type', None)
|
||||
|
||||
def __init__(self):
|
||||
'''This is a deserializing object only'''
|
||||
pass
|
||||
class ContainerObjectsList(AutoMarshallingListModel):
|
||||
|
||||
@classmethod
|
||||
def _xml_to_obj(cls, serialized_str):
|
||||
ret = []
|
||||
root = ElementTree.fromstring(serialized_str)
|
||||
setattr(cls, 'name', root.attrib['name'])
|
||||
data = []
|
||||
for child in root:
|
||||
storage_object_dict = {}
|
||||
for sub_child in child:
|
||||
storage_object_dict[sub_child.tag] = sub_child.text
|
||||
ret.append(cls._StorageObject(**storage_object_dict))
|
||||
return ret
|
||||
data.append(storage_object_dict)
|
||||
return cls._list_to_obj(data)
|
||||
|
||||
@classmethod
|
||||
def _json_to_obj(cls, serialized_str):
|
||||
ret = []
|
||||
data = json.loads(serialized_str)
|
||||
for storage_object in data:
|
||||
storage_obj = cls._StorageObject(
|
||||
name=storage_object.get('name'),
|
||||
bytes=storage_object.get('bytes'),
|
||||
hash=storage_object.get('hash'))
|
||||
last_modified = storage_object.get('last_modified')
|
||||
content_type = storage_object.get('content_type')
|
||||
ret.append(storage_obj)
|
||||
ret.append(last_modified)
|
||||
ret.append(content_type)
|
||||
return ret
|
||||
return cls._list_to_obj(data)
|
||||
|
||||
def _list_to_obj(cls, data):
|
||||
container_objects_list = ContainerObjectsList()
|
||||
for obj in data:
|
||||
storage_object = StorageObject(
|
||||
name=obj.get('name'),
|
||||
bytes_=obj.get('bytes'),
|
||||
hash_=obj.get('hash'),
|
||||
last_modified=obj.get('last_modified'),
|
||||
content_type=obj.get('content_type'))
|
||||
container_objects_list.append(storage_object)
|
||||
return container_objects_list
|
||||
|
||||
Reference in New Issue
Block a user