ariatosca-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mxmrlv <...@git.apache.org>
Subject [GitHub] incubator-ariatosca pull request #133: Convert runtime_properties to attribu...
Date Wed, 24 May 2017 12:10:58 GMT
Github user mxmrlv commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/133#discussion_r118228547
  
    --- Diff: aria/orchestrator/context/common.py ---
    @@ -194,3 +194,242 @@ def _render_resource(self, resource_content, variables):
             variables.setdefault('ctx', self)
             resource_template = jinja2.Template(resource_content)
             return resource_template.render(variables)
    +
    +    def _teardown_db_resources(self):
    +        self.model.log._session.close()
    +        self.model.log._engine.dispose()
    +
    +
    +class _InstrumentedCollection(object):
    +
    +    def __init__(self,
    +                 model,
    +                 parent,
    +                 field_name=None,
    +                 item_cls=None,
    +                 seq=None,
    +                 is_top_level=True,
    +                 **kwargs):
    +        self._model = model
    +        self._parent = parent
    +        self._field_name = field_name
    +        self._item_cls = item_cls
    +        self._is_top_level = is_top_level
    +        self._load(seq, **kwargs)
    +
    +    @property
    +    def _raw(self):
    +        raise NotImplementedError
    +
    +    def _load(self, seq, **kwargs):
    +        """
    +        Instantiates the object from existing seq.
    +
    +        :param seq: the original sequence to load from
    +        :return:
    +        """
    +        raise NotImplementedError
    +
    +    def _set(self, key, value):
    +        """
    +        set the changes for the current object (not in the db)
    +
    +        :param key:
    +        :param value:
    +        :return:
    +        """
    +        raise NotImplementedError
    +
    +    def _del(self, collection, key):
    +        raise NotImplementedError
    +
    +    def _instrument(self, key, value):
    +        """
    +        Instruments any collection to track changes (and ease of access)
    +        :param key:
    +        :param value:
    +        :return:
    +        """
    +        if isinstance(value, _InstrumentedCollection):
    +            return value
    +        elif isinstance(value, dict):
    +            instrumentation_cls = _InstrumentedDict
    +        elif isinstance(value, list):
    +            instrumentation_cls = _InstrumentedList
    +        else:
    +            return value
    +
    +        return instrumentation_cls(self._model, self, key, self._item_cls, value, False)
    +
    +    def _raw_value(self, value):
    +        """
    +        Get the raw value.
    +        :param value:
    +        :return:
    +        """
    +        if self._is_top_level and isinstance(value, self._item_cls):
    +            return value.value
    +        return value
    +
    +    def _encapsulate_value(self, key, value):
    +        """
    +        Create a new item cls if needed.
    +        :param key:
    +        :param value:
    +        :return:
    +        """
    +        if isinstance(value, self._item_cls):
    +            return value
    +        # If it is not wrapped
    +        return self._item_cls.wrap(key, value)
    +
    +    def __setitem__(self, key, value):
    +        """
    +        Update the values in both the local and the db locations.
    +        :param key:
    +        :param value:
    +        :return:
    +        """
    +        self._set(key, value)
    +        if self._is_top_level:
    +            # We are at the top level
    +            field = getattr(self._parent, self._field_name)
    +            mapi = getattr(self._model, self._item_cls.__modelname__)
    +            value = self._set_field(field,
    +                                    key,
    +                                    value if key in field else self._encapsulate_value(key,
value))
    +            mapi.update(value)
    +        else:
    +            # We are not at the top level
    +            self._set_field(self._parent, self._field_name, self)
    +
    +    def _set_field(self, collection, key, value):
    +        """
    +        enables updating the current change in the ancestors
    +        :param collection: the collection to change
    +        :param key: the key for the specific field
    +        :param value: the new value
    +        :return:
    +        """
    +        if isinstance(value, _InstrumentedCollection):
    +            value = value._raw
    +        if key in collection and isinstance(collection[key], self._item_cls):
    +            if isinstance(collection[key], self.PYTHON_TYPE):
    --- End diff --
    
    maybe delete always if key is in collection?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message