You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ariatosca.apache.org by mx...@apache.org on 2017/01/05 09:35:32 UTC
incubator-ariatosca git commit:
ARIA-58-Storage-model-to_dict-creates-symbolic-links [Forced Update!]
Repository: incubator-ariatosca
Updated Branches:
refs/heads/ARIA-58-Storage-model-to_dict-creates-symbolic-links f35bf15de -> 2c7d326ce (forced update)
ARIA-58-Storage-model-to_dict-creates-symbolic-links
As part of this commits, ARIA will use the builtin MutableDict and List
Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/2c7d326c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/2c7d326c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/2c7d326c
Branch: refs/heads/ARIA-58-Storage-model-to_dict-creates-symbolic-links
Commit: 2c7d326ce4ea65333efe5a242bdd8369fbb8e0d9
Parents: 860d69b
Author: mxmrlv <mx...@gmail.com>
Authored: Thu Jan 5 11:30:30 2017 +0200
Committer: mxmrlv <mx...@gmail.com>
Committed: Thu Jan 5 11:35:22 2017 +0200
----------------------------------------------------------------------
aria/storage/structure.py | 28 ++++++++++++----------
aria/storage/type.py | 54 ++++++++----------------------------------
2 files changed, 26 insertions(+), 56 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/2c7d326c/aria/storage/structure.py
----------------------------------------------------------------------
diff --git a/aria/storage/structure.py b/aria/storage/structure.py
index d222c94..431633b 100644
--- a/aria/storage/structure.py
+++ b/aria/storage/structure.py
@@ -124,25 +124,29 @@ class ModelMixin(object):
remote_side=remote_side_str,
post_update=True)
- def to_dict(self, suppress_error=False):
+ def to_dict(self, fields=None, suppress_error=False):
"""Return a dict representation of the model
:param suppress_error: If set to True, sets `None` to attributes that
it's unable to retrieve (e.g., if a relationship wasn't established
yet, and so it's impossible to access a property through it)
"""
- if suppress_error:
- res = dict()
- for field in self.fields():
- try:
- field_value = getattr(self, field)
- except AttributeError:
+ res = dict()
+ fields = fields or self.fields()
+ for field in fields:
+ try:
+ field_value = getattr(self, field)
+ except AttributeError:
+ if suppress_error:
field_value = None
- res[field] = field_value
- else:
- # Can't simply call here `self.to_response()` because inheriting
- # class might override it, but we always need the same code here
- res = dict((f, getattr(self, f)) for f in self.fields())
+ else:
+ raise
+ if isinstance(field_value, list):
+ field_value = list(field_value)
+ elif isinstance(field_value, dict):
+ field_value = dict(field_value)
+ res[field] = field_value
+
return res
@classmethod
http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/2c7d326c/aria/storage/type.py
----------------------------------------------------------------------
diff --git a/aria/storage/type.py b/aria/storage/type.py
index 84fd8dc..ab50b0f 100644
--- a/aria/storage/type.py
+++ b/aria/storage/type.py
@@ -60,63 +60,29 @@ class List(_MutableType):
return list
-class _MutableDict(mutable.Mutable, dict):
+class _MutableDict(mutable.MutableDict):
"""
Enables tracking for dict values.
"""
@classmethod
def coerce(cls, key, value):
"Convert plain dictionaries to MutableDict."
+ try:
+ return mutable.MutableDict.coerce(key, value)
+ except ValueError as e:
+ raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e)))
- if not isinstance(value, cls):
- if isinstance(value, dict):
- return cls(value)
- # this call will raise ValueError
- try:
- return mutable.Mutable.coerce(key, value)
- except ValueError as e:
- raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e)))
- else:
- return value
-
- def __setitem__(self, key, value):
- "Detect dictionary set events and emit change events."
-
- dict.__setitem__(self, key, value)
- self.changed()
-
- def __delitem__(self, key):
- "Detect dictionary del events and emit change events."
-
- dict.__delitem__(self, key)
- self.changed()
-
-
-class _MutableList(mutable.Mutable, list):
+class _MutableList(mutable.MutableList):
@classmethod
def coerce(cls, key, value):
"Convert plain dictionaries to MutableDict."
+ try:
+ return mutable.MutableList.coerce(key, value)
+ except ValueError as e:
+ raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e)))
- if not isinstance(value, cls):
- if isinstance(value, list):
- return cls(value)
-
- # this call will raise ValueError
- try:
- return mutable.Mutable.coerce(key, value)
- except ValueError as e:
- raise exceptions.StorageError('SQL Storage error: {0}'.format(str(e)))
- else:
- return value
-
- def __setitem__(self, key, value):
- list.__setitem__(self, key, value)
- self.changed()
-
- def __delitem__(self, key):
- list.__delitem__(self, key)
_MutableList.associate_with(List)
_MutableDict.associate_with(Dict)