Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-58-Storage-model-to_dict-creates-symbolic-links [created] f35bf15de
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/f35bf15d Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/f35bf15d Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/f35bf15d Branch: refs/heads/ARIA-58-Storage-model-to_dict-creates-symbolic-links Commit: f35bf15de03940b08023666c210c69d87b2739d8 Parents: 860d69b Author: mxmrlv <mxm...@gmail.com> Authored: Thu Jan 5 11:30:30 2017 +0200 Committer: mxmrlv <mxm...@gmail.com> Committed: Thu Jan 5 11:30:30 2017 +0200 ---------------------------------------------------------------------- aria/storage/structure.py | 30 +++++++++++++---------- aria/storage/type.py | 54 ++++++++---------------------------------- 2 files changed, 28 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/f35bf15d/aria/storage/structure.py ---------------------------------------------------------------------- diff --git a/aria/storage/structure.py b/aria/storage/structure.py index d222c94..9bf1286 100644 --- a/aria/storage/structure.py +++ b/aria/storage/structure.py @@ -124,25 +124,31 @@ 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: + # Can't simply call here `self.to_response()` because inheriting + # class might override it, but we always need the same code here + 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/f35bf15d/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)