ariatosca-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From AviaE <...@git.apache.org>
Subject [GitHub] incubator-ariatosca pull request #189: ARIA-174 Refactor instantiation phase
Date Wed, 02 Aug 2017 18:11:22 GMT
Github user AviaE commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/189#discussion_r130826916
  
    --- Diff: aria/orchestrator/topology/topology.py ---
    @@ -0,0 +1,217 @@
    +# Licensed to the Apache Software Foundation (ASF) under one or more
    +# contributor license agreements.  See the NOTICE file distributed with
    +# this work for additional information regarding copyright ownership.
    +# The ASF licenses this file to You under the Apache License, Version 2.0
    +# (the "License"); you may not use this file except in compliance with
    +# the License.  You may obtain a copy of the License at
    +#
    +#     http://www.apache.org/licenses/LICENSE-2.0
    +#
    +# Unless required by applicable law or agreed to in writing, software
    +# distributed under the License is distributed on an "AS IS" BASIS,
    +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +# See the License for the specific language governing permissions and
    +# limitations under the License.
    +
    +from ...parser.validation import issue
    +from ...modeling import models
    +from ...utils import console
    +from . import (
    +    template_handler,
    +    instance_handler,
    +    common
    +)
    +
    +
    +class Topology(issue.Reporter):
    +
    +    _init_map = {
    +        models.ServiceTemplate: models.Service,
    +        models.ArtifactTemplate: models.Artifact,
    +        models.CapabilityTemplate: models.Capability,
    +        models.GroupTemplate: models.Group,
    +        models.InterfaceTemplate: models.Interface,
    +        models.NodeTemplate: models.Node,
    +        models.PolicyTemplate: models.Policy,
    +        models.SubstitutionTemplate: models.Substitution,
    +        models.RelationshipTemplate: models.Relationship,
    +        models.OperationTemplate: models.Operation,
    +        models.RequirementTemplate: None,
    +        models.SubstitutionTemplateMapping: models.SubstitutionMapping,
    +
    +        # Common
    +        models.Metadata: models.Metadata,
    +        models.Attribute: models.Attribute,
    +        models.Property: models.Property,
    +        models.Input: models.Input,
    +        models.Output: models.Output,
    +        models.Configuration: models.Configuration,
    +        models.Argument: models.Argument,
    +        models.Type: models.Type
    +    }
    +
    +    def __init__(self, model_storage=None, *args, **kwargs):
    +        # TODO: model storage is required only for the list of plugins, can we get it
    +        # somewhere else?
    +        super(Topology, self).__init__(*args, **kwargs)
    +        self._model_storage = model_storage
    +        self._handlers = dict(self._init_handlers(instance_handler),
    +                              **self._init_handlers(template_handler))
    +
    +    @staticmethod
    +    def _init_handlers(module_):
    +        handlers = {}
    +        for attribute_name in dir(module_):
    +            if attribute_name.startswith('_'):
    +                continue
    +            attribute = getattr(module_, attribute_name)
    +            if isinstance(attribute, type) and issubclass(attribute, common._Handler):
    +                handlers[getattr(models, attribute_name)] = attribute
    +        return handlers
    +
    +    def instantiate(self, model, **kwargs):
    +        """
    +        all handlers used by instantiate should hold a tuple as value (handler, instnace_cls)
    +        :param model:
    +        :param kwargs:
    +        :return:
    +        """
    +        if isinstance(model, dict):
    +            return dict((name, self.instantiate(value, **kwargs))
    +                        for name, value in model.iteritems())
    +        elif isinstance(model, list):
    +            return list(self.instantiate(value, **kwargs) for value in model)
    +        elif model is not None:
    +            _handler = self._handlers.get(model.__class__)
    +            instance_cls = self._init_map.get(model.__class__)
    +            return _handler(self, model).instantiate(instance_cls, **kwargs)
    +
    +    def validate(self, model, **kwargs):
    +        if isinstance(model, dict):
    +            return self.validate(model.values())
    +        elif isinstance(model, list):
    +            return all(self.validate(value) for value in model)
    +        elif model is not None:
    +            _handler = self._handlers.get(model.__class__)
    +            return _handler(self, model).validate(**kwargs)
    +
    +    def dump(self, model, out_stream=None, section_name=None, **kwargs):
    +        out_stream = out_stream or console.TopologyStylizer()
    +
    +        # if model is empty, no need to print out the section name
    +        if model and section_name:
    +            out_stream.write('{0}:'.format(section_name))
    +
    +        if isinstance(model, dict):
    +            if str(out_stream):
    +                with out_stream.indent():
    +                    return self.dump(model.values(), out_stream=out_stream, **kwargs)
    +            else:
    +                return self.dump(model.values(), out_stream=out_stream, **kwargs)
    +
    +        elif isinstance(model, list):
    +            for value in model:
    +                self.dump(value, out_stream=out_stream, **kwargs)
    +
    +        elif model is not None:
    +            _handler = self._handlers.get(model.__class__)
    +            _handler(self, model).dump(out_stream=out_stream, **kwargs)
    +
    +        return out_stream
    +
    +    def dump_graph(self, service, **kwargs):
    +        out_stream = console.TopologyStylizer()
    +        for node in service.nodes.itervalues():
    +            if not node.inbound_relationships:
    +                self._dump_graph_node(out_stream, node)
    +        return out_stream
    +
    +    def _dump_graph_node(self, out_stream, node, capability=None):
    +        out_stream.write(out_stream.node(node.name))
    +        if capability is not None:
    +            out_stream.write('{0} ({1})'.format(out_stream.property(capability.name),
    +                                                out_stream.type(capability.type.name)))
    +        if node.outbound_relationships:
    +            with out_stream.indent():
    +                for relationship_model in node.outbound_relationships:
    +                    relationship_name = out_stream.property(relationship_model.name)
    +                    if relationship_model.type is not None:
    +                        out_stream.write('-> {0} ({1})'.format(
    +                            relationship_name, out_stream.type(relationship_model.type.name)))
    +                    else:
    +                        out_stream.write('-> {0}'.format(relationship_name))
    +                    with out_stream.indent(3):
    +                        self._dump_graph_node(out_stream,
    +                                              relationship_model.target_node,
    +                                              relationship_model.target_capability)
    +
    +    def coerce(self, model, **kwargs):
    +        if isinstance(model, dict):
    +            return self.validate(model.values())
    +        elif isinstance(model, list):
    +            return all(self.validate(value) for value in model)
    +        elif model is not None:
    +            _handler = self._handlers.get(model.__class__)
    +            return _handler(self, model).coerce(**kwargs)
    +
    +    def dump_types(self, service_template, out_stream=None):
    +        out_stream = out_stream or console.TopologyStylizer()
    +        self.dump(service_template.node_types, out_stream, 'Node types')
    +        self.dump(service_template.group_types, out_stream, 'Group types')
    +        self.dump(service_template.capability_types, out_stream, 'Capability types')
    +        self.dump(service_template.relationship_types, out_stream, 'Relationship types')
    +        self.dump(service_template.policy_types, out_stream, 'Policy types')
    +        self.dump(service_template.artifact_types, out_stream, 'Artifact types')
    +        self.dump(service_template.interface_types, out_stream, 'Interface types')
    +
    +        return str(out_stream)
    --- End diff --
    
    Why do you return `str(out_stream)` here, and `out_stream` in `dump`?


---
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