Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-258-Convert-runtime-properties-to-attributes b597b94db -> 
493816b06


moved the decoration to a function and return custom class instnace


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/493816b0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/493816b0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/493816b0

Branch: refs/heads/ARIA-258-Convert-runtime-properties-to-attributes
Commit: 493816b06e01a246c8257f0bf7893f9c8e3a877d
Parents: b597b94
Author: max-orlov <[email protected]>
Authored: Thu May 25 14:13:14 2017 +0300
Committer: max-orlov <[email protected]>
Committed: Thu May 25 14:13:14 2017 +0300

----------------------------------------------------------------------
 aria/orchestrator/context/common.py    | 60 ++++++++++++++++-------------
 aria/orchestrator/context/operation.py | 12 +++---
 2 files changed, 40 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/493816b0/aria/orchestrator/context/common.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/context/common.py 
b/aria/orchestrator/context/common.py
index fad50ea..2feea14 100644
--- a/aria/orchestrator/context/common.py
+++ b/aria/orchestrator/context/common.py
@@ -381,33 +381,41 @@ class _InstrumentedList(_InstrumentedCollection, list):
         return list(self)
 
 
-class InstrumentCollection(object):
+class _InstrumentedModel(object):
 
-    def __init__(self, field_name):
-        super(InstrumentCollection, self).__init__()
+    def __init__(self, field_name, original_model, model_storage):
+        super(_InstrumentedModel, self).__init__()
         self._field_name = field_name
-        self._actor = None
-
-    @property
-    def actor(self):
-        return self._actor
+        self._model_storage = model_storage
+        self._original_model = original_model
+        self._apply_instrumentation()
 
     def __getattr__(self, item):
-        return getattr(self._actor, item)
-
-    def __call__(self, func, *args, **kwargs):
-        def _wrapper(func_self, *args, **kwargs):
-            self._actor = func(func_self, *args, **kwargs)
-            field = getattr(self._actor, self._field_name)
-
-            # Preserve the original value. e.g. original attributes would be 
located under
-            # _attributes
-            setattr(self, '_{0}'.format(self._field_name), field)
-
-            # set instrumented value
-            setattr(self, self._field_name, _InstrumentedDict(func_self.model,
-                                                              self._actor,
-                                                              self._field_name,
-                                                              field))
-            return self
-        return _wrapper
+        return getattr(self._original_model, item)
+
+    def _apply_instrumentation(self):
+
+        field = getattr(self._original_model, self._field_name)
+
+        # Preserve the original value. e.g. original attributes would be 
located under
+        # _attributes
+        setattr(self, '_{0}'.format(self._field_name), field)
+
+        # set instrumented value
+        setattr(self, self._field_name, _InstrumentedDict(self._model_storage,
+                                                          self._original_model,
+                                                          self._field_name,
+                                                          field))
+
+
+def instrument_collection(field_name, func=None):
+    if func is None:
+        return partial(instrument_collection, field_name)
+
+    def _wrapper(*args, **kwargs):
+        original_model = func(*args, **kwargs)
+        return 
type('Instrumented{0}'.format(original_model.__class__.__name__),
+                    (_InstrumentedModel, ),
+                    {})(field_name, original_model, args[0].model)
+
+    return _wrapper

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/493816b0/aria/orchestrator/context/operation.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/context/operation.py 
b/aria/orchestrator/context/operation.py
index 1563e22..269251f 100644
--- a/aria/orchestrator/context/operation.py
+++ b/aria/orchestrator/context/operation.py
@@ -114,7 +114,7 @@ class NodeOperationContext(BaseOperationContext):
     """
 
     @property
-    @common.InstrumentCollection('attributes')
+    @common.instrument_collection('attributes')
     def node_template(self):
         """
         the node of the current operation
@@ -123,7 +123,7 @@ class NodeOperationContext(BaseOperationContext):
         return self.node.node_template
 
     @property
-    @common.InstrumentCollection('attributes')
+    @common.instrument_collection('attributes')
     def node(self):
         """
         The node instance of the current operation
@@ -138,7 +138,7 @@ class RelationshipOperationContext(BaseOperationContext):
     """
 
     @property
-    @common.InstrumentCollection('attributes')
+    @common.instrument_collection('attributes')
     def source_node_template(self):
         """
         The source node
@@ -147,7 +147,7 @@ class RelationshipOperationContext(BaseOperationContext):
         return self.source_node.node_template
 
     @property
-    @common.InstrumentCollection('attributes')
+    @common.instrument_collection('attributes')
     def source_node(self):
         """
         The source node instance
@@ -156,7 +156,7 @@ class RelationshipOperationContext(BaseOperationContext):
         return self.relationship.source_node
 
     @property
-    @common.InstrumentCollection('attributes')
+    @common.instrument_collection('attributes')
     def target_node_template(self):
         """
         The target node
@@ -165,7 +165,7 @@ class RelationshipOperationContext(BaseOperationContext):
         return self.target_node.node_template
 
     @property
-    @common.InstrumentCollection('attributes')
+    @common.instrument_collection('attributes')
     def target_node(self):
         """
         The target node instance

Reply via email to