ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From emblempar...@apache.org
Subject [1/2] incubator-ariatosca git commit: Remove dependency on context, modeling exceptions, some refactoring
Date Sun, 12 Mar 2017 21:49:13 GMT
Repository: incubator-ariatosca
Updated Branches:
  refs/heads/ARIA-105-integrate-modeling dd5bfa930 -> 16a945383


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/16a94538/aria/parser/consumption/modeling.py
----------------------------------------------------------------------
diff --git a/aria/parser/consumption/modeling.py b/aria/parser/consumption/modeling.py
index f4e71a5..4847ba7 100644
--- a/aria/parser/consumption/modeling.py
+++ b/aria/parser/consumption/modeling.py
@@ -17,49 +17,52 @@ from ...utils.formatting import json_dumps, yaml_dumps
 from .consumer import Consumer, ConsumerChain
 
 
-class Derive(Consumer):
+class DeriveServiceTemplate(Consumer):
     """
-    Derives the service model.
+    Derives the service template from the presenter.
     """
 
     def consume(self):
         if self.context.presentation.presenter is None:
-            self.context.validation.report('Derive consumer: missing presenter')
+            self.context.validation.report('DeriveServiceTemplate consumer: missing presenter')
             return
 
         if not hasattr(self.context.presentation.presenter, '_get_model'):
-            self.context.validation.report('Derive consumer: presenter does not support '
-                                           '"_get_model"')
+            self.context.validation.report('DeriveServiceTemplate consumer: presenter does
not'
+                                           ' support "_get_model"')
             return
 
         self.context.modeling.template = \
             self.context.presentation.presenter._get_model(self.context)
 
 
-class CoerceModelValues(Consumer):
+class CoerceServiceTemplateValues(Consumer):
     """
-    Coerces values in the service model.
+    Coerces values in the service template.
     """
 
     def consume(self):
-        self.context.modeling.template.coerce_values(self.context, None, True)
+        self.context.modeling.template.coerce_values(None, True)
 
 
-class ValidateModel(Consumer):
+class ValidateServiceTemplate(Consumer):
     """
-    Validates the service model.
+    Validates the service template.
     """
 
     def consume(self):
-        self.context.modeling.template.validate(self.context)
+        self.context.modeling.template.validate()
 
-class Model(ConsumerChain):
+
+class ServiceTemplate(ConsumerChain):
     """
-    Generates the service model by deriving it from the presentation.
+    Generates the service template from the presenter.
     """
 
     def __init__(self, context):
-        super(Model, self).__init__(context, (Derive, CoerceModelValues, ValidateModel))
+        super(ServiceTemplate, self).__init__(context, (DeriveServiceTemplate,
+                                                        CoerceServiceTemplateValues,
+                                                        ValidateServiceTemplate))
 
     def dump(self):
         if self.context.has_arg_switch('yaml'):
@@ -71,7 +74,8 @@ class Model(ConsumerChain):
             raw = self.context.modeling.template_as_raw
             self.context.write(json_dumps(raw, indent=indent))
         else:
-            self.context.modeling.template.dump(self.context)
+            self.context.modeling.template.dump()
+
 
 class Types(Consumer):
     """
@@ -88,35 +92,40 @@ class Types(Consumer):
             raw = self.context.modeling.types_as_raw
             self.context.write(json_dumps(raw, indent=indent))
         else:
-            self.context.modeling.dump_types(self.context)
+            self.context.modeling.template.dump_types()
 
-class Instantiate(Consumer):
+
+class InstantiateServiceInstance(Consumer):
     """
-    Instantiates the service model.
+    Instantiates the service template into a service instance.
     """
 
     def consume(self):
         if self.context.modeling.template is None:
-            self.context.validation.report('Instantiate consumer: missing service model')
+            self.context.validation.report('InstantiateServiceInstance consumer: missing
service '
+                                           'model')
             return
 
-        self.context.modeling.template.instantiate(self.context, None)
+        self.context.modeling.template.instantiate(None)
+
 
-class CoerceInstanceValues(Consumer):
+class CoerceServiceInstanceValues(Consumer):
     """
     Coerces values in the service instance.
     """
 
     def consume(self):
-        self.context.modeling.instance.coerce_values(self.context, None, True)
+        self.context.modeling.instance.coerce_values(None, True)
 
-class ValidateInstance(Consumer):
+
+class ValidateServiceInstance(Consumer):
     """
     Validates the service instance.
     """
 
     def consume(self):
-        self.context.modeling.instance.validate(self.context)
+        self.context.modeling.instance.validate()
+
 
 class SatisfyRequirements(Consumer):
     """
@@ -124,7 +133,8 @@ class SatisfyRequirements(Consumer):
     """
 
     def consume(self):
-        self.context.modeling.instance.satisfy_requirements(self.context)
+        self.context.modeling.instance.satisfy_requirements()
+
 
 class ValidateCapabilities(Consumer):
     """
@@ -132,22 +142,27 @@ class ValidateCapabilities(Consumer):
     """
 
     def consume(self):
-        self.context.modeling.instance.validate_capabilities(self.context)
+        self.context.modeling.instance.validate_capabilities()
+
 
-class Instance(ConsumerChain):
+class ServiceInstance(ConsumerChain):
     """
-    Generates the service instance by instantiating the service model.
+    Generates the service instance by instantiating the service template.
     """
 
     def __init__(self, context):
-        super(Instance, self).__init__(context, (Instantiate, CoerceInstanceValues,
-                                                 ValidateInstance, CoerceInstanceValues,
-                                                 SatisfyRequirements, CoerceInstanceValues,
-                                                 ValidateCapabilities, CoerceInstanceValues))
+        super(ServiceInstance, self).__init__(context, (InstantiateServiceInstance,
+                                                        CoerceServiceInstanceValues,
+                                                        ValidateServiceInstance,
+                                                        CoerceServiceInstanceValues,
+                                                        SatisfyRequirements,
+                                                        CoerceServiceInstanceValues,
+                                                        ValidateCapabilities,
+                                                        CoerceServiceInstanceValues))
 
     def dump(self):
         if self.context.has_arg_switch('graph'):
-            self.context.modeling.instance.dump_graph(self.context)
+            self.context.modeling.instance.dump_graph()
         elif self.context.has_arg_switch('yaml'):
             indent = self.context.get_arg_value_int('indent', 2)
             raw = self.context.modeling.instance_as_raw
@@ -157,4 +172,4 @@ class Instance(ConsumerChain):
             raw = self.context.modeling.instance_as_raw
             self.context.write(json_dumps(raw, indent=indent))
         else:
-            self.context.modeling.instance.dump(self.context)
+            self.context.modeling.instance.dump()

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/16a94538/aria/parser/reading/__init__.py
----------------------------------------------------------------------
diff --git a/aria/parser/reading/__init__.py b/aria/parser/reading/__init__.py
index 32aa5b5..b5c0709 100644
--- a/aria/parser/reading/__init__.py
+++ b/aria/parser/reading/__init__.py
@@ -13,7 +13,7 @@
 from .raw import RawReader
 from .reader import Reader
 from .yaml import YamlReader
-from .locator import Locator
+from .locator import (Locator, deepcopy_with_locators, copy_locators)
 from .json import JsonReader
 from .jinja import JinjaReader
 from .context import ReadingContext
@@ -34,6 +34,8 @@ __all__ = (
     'ReadingContext',
     'RawReader',
     'Locator',
+    'deepcopy_with_locators',
+    'copy_locators',
     'YamlReader',
     'JsonReader',
     'JinjaReader')

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/16a94538/aria/parser/reading/locator.py
----------------------------------------------------------------------
diff --git a/aria/parser/reading/locator.py b/aria/parser/reading/locator.py
index a1cfa9c..4142ee7 100644
--- a/aria/parser/reading/locator.py
+++ b/aria/parser/reading/locator.py
@@ -10,12 +10,15 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from copy import deepcopy
+
+
 from ...utils.console import puts, Colored, indent
 
+
 # We are inheriting the primitive types in order to add the ability to set
 # an attribute (_locator) on them.
 
-
 class LocatableString(unicode):
     pass
 
@@ -117,3 +120,35 @@ class Locator(object):
     def __str__(self):
         # Should be in same format as Issue.locator_as_str
         return '"%s":%d:%d' % (self.location, self.line, self.column)
+
+
+def deepcopy_with_locators(value):
+    """
+    Like :code:`deepcopy`, but also copies over locators.
+    """
+
+    res = deepcopy(value)
+    copy_locators(res, value)
+    return res
+
+
+def copy_locators(target, source):
+    """
+    Copies over :code:`_locator` for all elements, recursively.
+
+    Assumes that target and source have exactly the same list/dict structure.
+    """
+
+    locator = getattr(source, '_locator', None)
+    if locator is not None:
+        try:
+            setattr(target, '_locator', locator)
+        except AttributeError:
+            pass
+
+    if isinstance(target, list) and isinstance(source, list):
+        for i, _ in enumerate(target):
+            copy_locators(target[i], source[i])
+    elif isinstance(target, dict) and isinstance(source, dict):
+        for k, v in target.items():
+            copy_locators(v, source[k])

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/16a94538/tests/end2end/test_tosca_simple_v1_0.py
----------------------------------------------------------------------
diff --git a/tests/end2end/test_tosca_simple_v1_0.py b/tests/end2end/test_tosca_simple_v1_0.py
index f2a41ce..4658fc3 100644
--- a/tests/end2end/test_tosca_simple_v1_0.py
+++ b/tests/end2end/test_tosca_simple_v1_0.py
@@ -71,7 +71,7 @@ def test_use_case_network_4():
 
 
 def test_use_case_webserver_dbms_1():
-    consume_use_case('webserver-dbms-1', 'model')
+    consume_use_case('webserver-dbms-1', 'template')
 
 
 def test_use_case_webserver_dbms_2():
@@ -83,7 +83,7 @@ def test_use_case_multi_tier_1():
 
 
 def test_use_case_container_1():
-    consume_use_case('container-1', 'model')
+    consume_use_case('container-1', 'template')
 
 
 # NodeCellar
@@ -101,7 +101,7 @@ def test_node_cellar_presentation():
 
 
 def test_node_cellar_model():
-    consume_node_cellar('model')
+    consume_node_cellar('template')
 
 
 def test_node_cellar_types():

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/16a94538/tests/parser/utils.py
----------------------------------------------------------------------
diff --git a/tests/parser/utils.py b/tests/parser/utils.py
index 994aac6..8460de8 100644
--- a/tests/parser/utils.py
+++ b/tests/parser/utils.py
@@ -21,10 +21,10 @@ from aria.parser.consumption import (
     ConsumerChain,
     Read,
     Validate,
-    Model,
+    ServiceTemplate,
     Types,
     Inputs,
-    Instance
+    ServiceInstance
 )
 from aria.utils.imports import import_fullname
 
@@ -66,14 +66,14 @@ def create_consumer(context, consumer_class_name):
         dumper = None
     elif consumer_class_name == 'presentation':
         dumper = consumer.consumers[0]
-    elif consumer_class_name == 'model':
-        consumer.append(Model)
+    elif consumer_class_name == 'template':
+        consumer.append(ServiceTemplate)
     elif consumer_class_name == 'types':
-        consumer.append(Model, Types)
+        consumer.append(ServiceTemplate, Types)
     elif consumer_class_name == 'instance':
-        consumer.append(Model, Inputs, Instance)
+        consumer.append(ServiceTemplate, Inputs, ServiceInstance)
     else:
-        consumer.append(Model, Inputs, Instance)
+        consumer.append(ServiceTemplate, Inputs, ServiceInstance)
         consumer.append(import_fullname(consumer_class_name))
 
     if dumper is None:


Mime
View raw message