simplifying instrumenteddict wip

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

Branch: refs/heads/ARIA-258-Convert-runtime-properties-to-attributes
Commit: e2f5e25a864b6f2befc6a515511503de3aa31431
Parents: b8b7c70
Author: max-orlov <ma...@gigaspaces.com>
Authored: Thu May 18 20:06:45 2017 +0300
Committer: max-orlov <ma...@gigaspaces.com>
Committed: Thu May 18 20:06:45 2017 +0300

----------------------------------------------------------------------
 aria/orchestrator/context/common.py          | 96 ++++++++++++++---------
 tests/orchestrator/context/test_operation.py | 49 ++++++------
 2 files changed, 83 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/e2f5e25a/aria/orchestrator/context/common.py
----------------------------------------------------------------------
diff --git a/aria/orchestrator/context/common.py 
b/aria/orchestrator/context/common.py
index 9f41071..46e315f 100644
--- a/aria/orchestrator/context/common.py
+++ b/aria/orchestrator/context/common.py
@@ -203,52 +203,63 @@ class BaseContext(object):
 
 
 class _InstrumentedCollection(object):
-    def __init__(self, parent, model, nested=None):
-        super(_InstrumentedCollection, self).__init__()
+    def __init__(self, parent, model, nested_key=None, **kwargs):
+        super(_InstrumentedCollection, self).__init__(**kwargs)
         self._parent = parent
         self._model = model
         self._attr_cls = self._model.parameter.model_cls
-        self._nested = nested or None
+        self._nested_key = nested_key or None
 
     def __getitem__(self, key):
-        if isinstance(self._parent.get(key), self._attr_cls):
+        if self._nested_key is None:
             value = self._parent[key].value
+        elif isinstance(self, dict):
+            value = dict.__getitem__(self, key)
+        elif isinstance(self, list):
+            value = list.__getitem__(self, key)
         else:
-            value = self._parent[key]
+            raise BaseException()
 
         if isinstance(value, list):
-            return _InstrumentedList(self, self._model, key, _dict=value)
+            return _InstrumentedList(self, self._model, key, _list=value)
         elif isinstance(value, dict):
-            return _InstrumentedDict(self, self._model, key, _dict=value)
+            return _InstrumentedDict(self, self._model, key, **value)
         else:
             return value
 
 
 class _InstrumentedDict(_InstrumentedCollection, dict):
 
-    def __init__(self, *args, **kwargs):
-        dict_ = kwargs.pop('_dict', False)
-        super(_InstrumentedDict, self).__init__(*args, **kwargs)
-        if dict_:
-            dict.__init__(self, dict_ or {})
-
-    def _set(self, key, value):
-        dict.__setitem__(self, key, value.value if isinstance(value, 
self._attr_cls) else value)
-
-    def __setitem__(self, key, value):
-        self._set(key, value)
-        if self._nested is not None:
-            self._update_parent(self._nested, key, value)
+    def __setitem__(self, i, y):
+        super(_InstrumentedDict, self).__setitem__(i,
+                                                   y.value if isinstance(y, 
self._attr_cls) else y)
+        if self._nested_key is None:
+            nested_key = i
+            if not isinstance(y, self._attr_cls):
+                y = self._attr_cls.wrap(i, y)
+            self._model.parameter.put(y)
         else:
-            if isinstance(value, self._attr_cls):
-                attr = value
-            else:
-                attr = self._attr_cls.wrap(key, value)
-            self._update_parent(key, key, attr)
-            self._model.parameter.put(attr)
+            nested_key = self._nested_key
+
+        self._update_parent(nested_key, i, y)
+
+    def items(self):
+        items = super(_InstrumentedDict, self).items()
+        return ((key, value.value if isinstance(value, self._attr_cls) else 
value)
+                for key, value in items)
+
+    def values(self):
+        for _, value in self.items():
+            yield value
+
+    def update(self, E=None, **F):
+        dict_ = E or {}
+        dict_.update(F.copy())
+        for key, value in dict_.items():
+            self[key] = value
 
     def _update_parent(self, nested_key, key, value):
-        if isinstance(self._parent.get(self._nested), self._attr_cls):
+        if isinstance(self._parent.get(self._nested_key), self._attr_cls):
             self._parent[nested_key].value[key] = value
         else:
             if nested_key == key:
@@ -257,20 +268,29 @@ class _InstrumentedDict(_InstrumentedCollection, dict):
                 self._parent[nested_key] = {key: value}
 
     def clear(self):
-        self._parent.clear()
+        self._parent.get(self._nested_key, {}).clear()
+        dict.clear(self)
+
 
+class _InstrumentedList(_InstrumentedCollection, list):
+
+    def __init__(self, *args, **kwargs):
+        super(_InstrumentedList, self).__init__(*args, **kwargs)
+        list.__init__(self, kwargs.pop('_list', []))
 
-class _InstrumentedList(_InstrumentedCollection, collections.MutableSequence):
+    def append(self, p_object):
+        self.insert(len(self), p_object)
 
-    def insert(self, index, value):
-        if self._nested is not None:
-            attribute = self._update_attr(index, value)
+    def insert(self, index, p_object):
+        list.insert(self, index, p_object)
+        if self._nested_key is not None:
+            attribute = self._update_attr(index, p_object)
             self._model.parameter.update(attribute)
         elif len(self._parent) > index:
-            self._parent[index].value = value
+            self._parent[index].value = p_object
             self._model.parameter.update(self._parent[index])
         else:
-            attr = value if isinstance(value, self._attr_cls) else 
self._attr_cls.wrap(index, value)
+            attr = p_object if isinstance(p_object, self._attr_cls) else 
self._attr_cls.wrap(index, p_object)
             self._parent.insert(index, attr)
             self._model.parameter.put(self._parent)
 
@@ -278,8 +298,8 @@ class _InstrumentedList(_InstrumentedCollection, 
collections.MutableSequence):
         return self.insert(index, value)
 
     def _update_attr(self, index, value):
-        attribute = current = self._parent[self._nested[0]]
-        for i in self._nested[1:]:
+        attribute = current = self._parent[self._nested_key[0]]
+        for i in self._nested_key[1:]:
             current = current[i]
         if isinstance(current, self._attr_cls):
             if isinstance(current.value, list):
@@ -324,8 +344,8 @@ class InstrumentCollection(object):
             self._actor = func(func_self, *args, **kwargs)
             field = getattr(self._actor, self._field_name)
             if isinstance(field, dict):
-                setattr(self, self._field_name, _InstrumentedDict(self._actor, 
field, func_self.model))
+                setattr(self, self._field_name, _InstrumentedDict(field, 
func_self.model))
             elif isinstance(field, list):
-                setattr(self, self._field_name, _InstrumentedList(self._actor, 
field, func_self.model))
+                setattr(self, self._field_name, _InstrumentedList(field, 
func_self.model))
             return self
         return _wrapper

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/e2f5e25a/tests/orchestrator/context/test_operation.py
----------------------------------------------------------------------
diff --git a/tests/orchestrator/context/test_operation.py 
b/tests/orchestrator/context/test_operation.py
index a7656e5..669c2d5 100644
--- a/tests/orchestrator/context/test_operation.py
+++ b/tests/orchestrator/context/test_operation.py
@@ -490,6 +490,7 @@ def _test_plugin_workdir(ctx, filename, content):
 
 @operation
 def attribute_altering_operation(ctx, attributes_dict, **_):
+    import pydevd; pydevd.settrace('localhost', suspend=False)
     ctx.node.attributes.update(attributes_dict)
 
 
@@ -525,8 +526,8 @@ class TestDict(object):
         return MockModel()
 
     def test_keys(self, model, actor):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
-        actor.attributes_dict.update(
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
+        dict_.update(
             {
                 'key1': Parameter.wrap('key1', 'value1'),
                 'key2': Parameter.wrap('key1', 'value2')
@@ -535,33 +536,33 @@ class TestDict(object):
         assert sorted(dict_.keys()) == sorted(['key1', 'key2'])
 
     def test_values(self, model, actor):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
-        actor.attributes_dict.update({
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
+        dict_.update({
             'key1': Parameter.wrap('key1', 'value1'),
             'key2': Parameter.wrap('key1', 'value2')
         })
         assert sorted(dict_.values()) == sorted(['value1', 'value2'])
 
     def test_items(self, actor, model):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
-        actor.attributes_dict.update({
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
+        dict_.update({
             'key1': Parameter.wrap('key1', 'value1'),
             'key2': Parameter.wrap('key1', 'value2')
         })
         assert sorted(dict_.items()) == sorted([('key1', 'value1'), ('key2', 
'value2')])
 
     def test_iter(self, actor, model):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
-        actor.attributes_dict.update({
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
+        dict_.update({
             'key1': Parameter.wrap('key1', 'value1'),
             'key2': Parameter.wrap('key1', 'value2')
         })
         assert sorted(list(dict_)) == sorted(['key1', 'key2'])
 
     def test_bool(self, actor, model):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
         assert not dict_
-        actor.attributes_dict.update({
+        dict_.update({
             'key1': Parameter.wrap('key1', 'value1'),
             'key2': Parameter.wrap('key1', 'value2')
         })
@@ -583,27 +584,27 @@ class TestDict(object):
         assert dict_['key1']['inner_key'] == 'value2'
 
     def test_get_item(self, actor, model):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
         dict_['key1'] = Parameter.wrap('key1', 'value1')
 
-        assert isinstance(dict_._attributes['key1'], Parameter)
+        assert isinstance(dict_._parent['key1'], Parameter)
 
     def test_update(self, actor, model):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
         dict_['key1'] = 'value1'
 
         new_dict = {'key2': 'value2'}
         dict_.update(new_dict)
         assert len(dict_) == 2
         assert dict_['key2'] == 'value2'
-        assert isinstance(dict_._attributes['key2'], Parameter)
+        assert isinstance(dict_._parent['key2'], Parameter)
 
         new_dict = {}
         new_dict.update(dict_)
         assert new_dict['key1'] == dict_['key1']
 
     def test_copy(self, actor, model):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
         dict_['key1'] = 'value1'
 
         new_dict = dict_.copy()
@@ -615,7 +616,7 @@ class TestDict(object):
         assert dict_['key1'] == 'value2'
 
     def test_clear(self, actor, model):
-        dict_ = common._InstrumentedDict(actor, actor.attributes_dict, model)
+        dict_ = common._InstrumentedDict(actor.attributes_dict, model)
         dict_['key1'] = 'value1'
         dict_.clear()
 
@@ -632,35 +633,35 @@ class TestList(object):
         return MockModel()
 
     def test_insert(self, model, actor):
-        list_ = common._InstrumentedList(actor, actor.attributes_list, model)
+        list_ = common._InstrumentedList(actor.attributes_list, model)
         list_.append(Parameter.wrap('name', 'value1'))
         list_.append('value2')
 
         assert len(list_) == 2
-        assert isinstance(list_._attributes[0], Parameter)
+        assert isinstance(list_._parent[0], Parameter)
         assert list_[0] == 'value1'
 
-        assert isinstance(list_._attributes[1], Parameter)
+        assert isinstance(list_._parent[1], Parameter)
         assert list_[1] == 'value2'
 
         list_[0] = 'new_value1'
         list_[1] = 'new_value2'
-        assert isinstance(list_._attributes[1], Parameter)
-        assert isinstance(list_._attributes[1], Parameter)
+        assert isinstance(list_._parent[1], Parameter)
+        assert isinstance(list_._parent[1], Parameter)
         assert list_[0] == 'new_value1'
         assert list_[1] == 'new_value2'
 
     def test_insert_into_nested(self, model, actor):
-        list_ = common._InstrumentedList(actor, actor.attributes_list, model)
+        list_ = common._InstrumentedList(actor.attributes_list, model)
         list_.append([])
 
         list_[0].append('inner_item')
-        assert isinstance(list_._attributes[0], Parameter)
+        assert isinstance(list_._parent[0], Parameter)
         assert len(list_) == 1
         assert list_[0][0] == 'inner_item'
 
         list_[0].append('new_item')
-        assert isinstance(list_._attributes[0], Parameter)
+        assert isinstance(list_._parent[0], Parameter)
         assert len(list_) == 1
         assert list_[0][1] == 'new_item'
 

Reply via email to