Skip to content

Commit

Permalink
#451 revert ovewritten files
Browse files Browse the repository at this point in the history
  • Loading branch information
filippomc committed Mar 4, 2022
1 parent eb72df1 commit f6b7ca3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
4 changes: 2 additions & 2 deletions libraries/models/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ setup.py
*/__init__.py
encoder.py
*.sh
util.py
base_model.py
*/util.py
**/base_model.py
27 changes: 23 additions & 4 deletions libraries/models/cloudharness_model/models/base_model_.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,30 @@ class Model(object):
# value is json key in definition.
attribute_map = {}

def __init__(self):
self._raw_dict = {}

@classmethod
def from_dict(cls: typing.Type[T], dikt) -> T:
"""Returns the dict as a model"""
return util.deserialize_model(dikt, cls)
obj = util.deserialize_model(dikt, cls)
return obj

def __getitem__(self, key):
if hasattr(self, key):
return getattr(self, key)
return self._raw_dict[key]


def __contains__(self, key):
if key in self.attribute_map:
return True
elif hasattr(self, "_raw_dict"):
return key in self._raw_dict
return False

def to_dict(self):
"""Returns the model properties as a dict
:rtype: dict
"""
result = {}
Expand All @@ -47,11 +63,14 @@ def to_dict(self):
else:
result[attr] = value

if hasattr(self, "raw_dict"):
merged = dict(self.raw_dict)
merged.update(result)
return merged
return result

def to_str(self):
"""Returns the string representation of the model
:rtype: str
"""
return pprint.pformat(self.to_dict())
Expand All @@ -66,4 +85,4 @@ def __eq__(self, other):

def __ne__(self, other):
"""Returns true if both objects are not equal"""
return not self == other
return not self == other
46 changes: 21 additions & 25 deletions libraries/models/cloudharness_model/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

def _deserialize(data, klass):
"""Deserializes dict, list, str into an object.
:param data: dict, list or str.
:param klass: class literal, or string of class name.
:return: object.
"""
if data is None:
Expand All @@ -35,10 +33,8 @@ def _deserialize(data, klass):

def _deserialize_primitive(data, klass):
"""Deserializes to primitive type.
:param data: data to deserialize.
:param klass: class literal.
:return: int, long, float, str, bool.
:rtype: int | long | float | str | bool
"""
Expand All @@ -53,23 +49,21 @@ def _deserialize_primitive(data, klass):

def _deserialize_object(value):
"""Return an original value.
:return: object.
"""
return value


def deserialize_date(string):
"""Deserializes string to date.
:param string: str.
:type string: str
:return: date.
:rtype: date
"""
if string is None:
return None
return None

try:
from dateutil.parser import parse
return parse(string).date()
Expand All @@ -79,58 +73,62 @@ def deserialize_date(string):

def deserialize_datetime(string):
"""Deserializes string to datetime.
The string should be in iso8601 datetime format.
:param string: str.
:type string: str
:return: datetime.
:rtype: datetime
"""
if string is None:
return None
return None

try:
from dateutil.parser import parse
return parse(string)
except ImportError:
return string

class DeserializationException(Exception): pass



def deserialize_model(data, klass):
"""Deserializes list or dict to model.
:param data: dict, list.
:type data: dict | list
:param klass: class literal.
:return: model object.
"""
instance = klass()
instance._raw_dict = data

if not instance.openapi_types:
return data
try:
if data is None:
return instance

if isinstance(data, list):
for attr, attr_type in six.iteritems(instance.openapi_types):
if data is not None \
and instance.attribute_map[attr] in data \
and isinstance(data, (list, dict)):
if instance.attribute_map[attr] in data:
value = data[instance.attribute_map[attr]]
setattr(instance, attr, _deserialize(value, attr_type))
except Exception as e:
raise DeserializationException from e

elif isinstance(data, dict):

for attr in data:
value = data[attr]
if attr in instance.attribute_map:
setattr(instance, attr, _deserialize(value, instance.openapi_types[attr]))
else:
setattr(instance, attr, value)

return instance


def _deserialize_list(data, boxed_type):
"""Deserializes a list and its elements.
:param data: list to deserialize.
:type data: list
:param boxed_type: class literal.
:return: deserialized list.
:rtype: list
"""
Expand All @@ -140,13 +138,11 @@ def _deserialize_list(data, boxed_type):

def _deserialize_dict(data, boxed_type):
"""Deserializes a dict and its elements.
:param data: dict to deserialize.
:type data: dict
:param boxed_type: class literal.
:return: deserialized dict.
:rtype: dict
"""
return {k: _deserialize(v, boxed_type)
for k, v in six.iteritems(data)}
for k, v in six.iteritems(data)}

0 comments on commit f6b7ca3

Please sign in to comment.