Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3152D200C7F for ; Wed, 24 May 2017 14:11:01 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 2E660160BD0; Wed, 24 May 2017 12:11:01 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 77C85160BB4 for ; Wed, 24 May 2017 14:11:00 +0200 (CEST) Received: (qmail 51614 invoked by uid 500); 24 May 2017 12:10:59 -0000 Mailing-List: contact commits-help@ariatosca.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ariatosca.incubator.apache.org Delivered-To: mailing list commits@ariatosca.incubator.apache.org Received: (qmail 51577 invoked by uid 99); 24 May 2017 12:10:58 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 May 2017 12:10:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id AF1F8E04F2; Wed, 24 May 2017 12:10:58 +0000 (UTC) From: mxmrlv To: commits@ariatosca.apache.org Reply-To: commits@ariatosca.apache.org References: In-Reply-To: Subject: [GitHub] incubator-ariatosca pull request #133: Convert runtime_properties to attribu... Content-Type: text/plain Message-Id: <20170524121058.AF1F8E04F2@git1-us-west.apache.org> Date: Wed, 24 May 2017 12:10:58 +0000 (UTC) archived-at: Wed, 24 May 2017 12:11:01 -0000 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. ---