Repository: incubator-ariatosca Updated Branches: refs/heads/Misc_fixes 3eeb4a468 -> ba4595e23 (forced update)
Misc_fixes Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/ba4595e2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/ba4595e2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/ba4595e2 Branch: refs/heads/Misc_fixes Commit: ba4595e23b33de3f4f86b382781dc464bf3e5e66 Parents: 860d69b Author: mxmrlv <mxm...@gmail.com> Authored: Wed Jan 4 23:33:12 2017 +0200 Committer: mxmrlv <mxm...@gmail.com> Committed: Thu Jan 5 00:30:50 2017 +0200 ---------------------------------------------------------------------- aria/storage/base_model.py | 4 +-- aria/storage/structure.py | 30 ++++++++++++++--------- aria/storage/type.py | 54 ++++++++--------------------------------- 3 files changed, 30 insertions(+), 58 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ba4595e2/aria/storage/base_model.py ---------------------------------------------------------------------- diff --git a/aria/storage/base_model.py b/aria/storage/base_model.py index c7eb27c..d1aebf2 100644 --- a/aria/storage/base_model.py +++ b/aria/storage/base_model.py @@ -146,7 +146,7 @@ class ExecutionBase(ModelMixin): VALID_TRANSITIONS = { PENDING: [STARTED, CANCELLED], STARTED: END_STATES + [CANCELLING], - CANCELLING: END_STATES + CANCELLING: END_STATES + [FORCE_CANCELLING] } @orm.validates('status') @@ -156,7 +156,7 @@ class ExecutionBase(ModelMixin): current_status = getattr(self, key) except AttributeError: return - valid_transitions = ExecutionBase.VALID_TRANSITIONS.get(current_status, []) + valid_transitions = self.VALID_TRANSITIONS.get(current_status, []) if all([current_status is not None, current_status != value, value not in valid_transitions]): http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/ba4595e2/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/ba4595e2/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)