Example #1
0
def convert_interface_definition_from_type_to_raw_template(context, presentation):                  # pylint: disable=invalid-name
    raw = OrderedDict()

    # Copy default values for inputs
    interface_inputs = presentation._get_inputs(context)
    if interface_inputs is not None:
        raw['inputs'] = convert_parameter_definitions_to_values(context, interface_inputs)

    # Copy operations
    operations = presentation._get_operations(context)
    if operations:
        for operation_name, operation in operations.iteritems():
            raw[operation_name] = OrderedDict()
            description = operation.description
            if description is not None:
                raw[operation_name]['description'] = deepcopy_with_locators(description._raw)
            implementation = operation.implementation
            if implementation is not None:
                raw[operation_name]['implementation'] = deepcopy_with_locators(implementation._raw)
            inputs = operation.inputs
            if inputs is not None:
                raw[operation_name]['inputs'] = convert_parameter_definitions_to_values(context,
                                                                                        inputs)

    return raw
Example #2
0
def get_inherited_property_definitions(context,
                                       presentation,
                                       field_name,
                                       for_presentation=None):
    """
    Returns our property definitions added on top of those of our parent, if we have one
    (recursively).

    Allows overriding all aspects of parent properties except data type.
    """

    # Get definitions from parent
    # If we inherit from a primitive, it does not have a parent:
    parent = presentation._get_parent(context) if hasattr(
        presentation, '_get_parent') else None
    definitions = get_inherited_property_definitions(context, parent, field_name,
                                                     for_presentation=presentation) \
                                                     if parent is not None else OrderedDict()

    # Add/merge our definitions
    # If we inherit from a primitive, it does not have our field
    our_definitions = getattr(presentation, field_name, None)
    if our_definitions:
        our_definitions_clone = OrderedDict()
        for name, our_definition in our_definitions.iteritems():
            our_definitions_clone[name] = our_definition._clone(
                for_presentation)
        our_definitions = our_definitions_clone
        merge_property_definitions(context, presentation, definitions,
                                   our_definitions, field_name)

    for definition in definitions.itervalues():
        definition._reset_method_cache()

    return definitions
Example #3
0
def merge_requirement_assignment(context, relationship_property_definitions,
                                 relationship_interface_definitions,
                                 requirement, our_requirement):
    our_capability = our_requirement.capability
    if our_capability is not None:
        requirement._raw['capability'] = deepcopy_with_locators(our_capability)

    our_node = our_requirement.node
    if our_node is not None:
        requirement._raw['node'] = deepcopy_with_locators(our_node)

    our_node_filter = our_requirement.node_filter
    if our_node_filter is not None:
        requirement._raw['node_filter'] = deepcopy_with_locators(
            our_node_filter._raw)

    our_relationship = our_requirement.relationship  # RelationshipAssignment
    if our_relationship is not None:
        # Make sure we have a dict
        if 'relationship' not in requirement._raw:
            requirement._raw['relationship'] = OrderedDict()
        elif not isinstance(requirement._raw['relationship'], dict):
            # Convert existing short form to long form
            the_type = requirement._raw['relationship']
            requirement._raw['relationship'] = OrderedDict()
            requirement._raw['relationship']['type'] = deepcopy_with_locators(
                the_type)

        merge_requirement_assignment_relationship(
            context, our_relationship, relationship_property_definitions,
            relationship_interface_definitions, requirement, our_relationship)
Example #4
0
def get_inherited_capability_definitions(context,
                                         presentation,
                                         for_presentation=None):
    """
    Returns our capability capability definitions added on top of those of our parent, if we have
    one (recursively).

    Allows overriding all aspects of parent capability properties except data type.
    """

    if for_presentation is None:
        for_presentation = presentation

    # Get capability definitions from parent
    parent = presentation._get_parent(context)
    capability_definitions = get_inherited_capability_definitions(
        context, parent,
        for_presentation) if parent is not None else OrderedDict()

    # Add/merge our capability definitions
    our_capability_definitions = presentation.capabilities
    if our_capability_definitions:
        for capability_name, our_capability_definition in our_capability_definitions.iteritems(
        ):
            if capability_name in capability_definitions:
                capability_definition = capability_definitions[capability_name]

                # Check if we changed the type
                type1 = capability_definition._get_type(context)
                type2 = our_capability_definition._get_type(context)

                if not type1._is_descendant(context, type2):
                    context.validation.report(
                        u'capability definition type "{0}" is not a descendant of overridden '
                        u'capability definition type "{1}"' \
                        .format(type1._name, type2._name),
                        locator=our_capability_definition._locator, level=Issue.BETWEEN_TYPES)

                merge_capability_definition(context, presentation,
                                            capability_definition,
                                            our_capability_definition)
            else:
                capability_definition = our_capability_definition._clone(
                    for_presentation)
                if isinstance(capability_definition._raw, basestring):
                    # Make sure we have a dict
                    the_type = capability_definition._raw
                    capability_definition._raw = OrderedDict()
                    capability_definition._raw['type'] = the_type
                capability_definitions[capability_name] = capability_definition

            merge_capability_definition_from_type(context, presentation,
                                                  capability_definition)

    for capability_definition in capability_definitions.itervalues():
        capability_definition._reset_method_cache()

    return capability_definitions
def get_inherited_capability_definitions(context,
                                         presentation,
                                         for_presentation=None):
    """
    Returns our capability capability definitions added on top of those of our parent, if we have
    one (recursively).

    Allows overriding all aspects of parent capability properties except data type.
    """

    # Get capability definitions from parent
    parent = presentation._get_parent(context)
    capability_definitions = get_inherited_capability_definitions(context, parent,
                                                                  for_presentation=presentation) \
                                                                  if parent is not None \
                                                                  else OrderedDict()

    # Add/merge our capability definitions
    our_capability_definitions = presentation.capabilities
    if our_capability_definitions:
        for capability_name, our_capability_definition in our_capability_definitions.iteritems(
        ):
            if capability_name in capability_definitions:
                capability_definition = capability_definitions[capability_name]

                # Check if we changed the type
                type1 = capability_definition.type
                type2 = our_capability_definition.type
                if type1 != type2:
                    context.validation.report(
                        'capability definition changes type from "%s" to "%s" in "%s"'
                        % (type1, type2, presentation._fullname),
                        locator=our_capability_definition._locator,
                        level=Issue.BETWEEN_TYPES)

                # Already cloned?
                #capability_definition = capability_definition._clone(for_presentation)
                #capability_definitions[capability_name] = capability_definition
            else:
                capability_definition = our_capability_definition._clone(
                    for_presentation)
                if isinstance(capability_definition._raw, basestring):
                    # Make sure we have a dict
                    the_type = capability_definition._raw
                    capability_definition._raw = OrderedDict()
                    capability_definition._raw['type'] = the_type
                capability_definitions[capability_name] = capability_definition

            merge_capability_definition_from_type(context, presentation,
                                                  capability_definition)

    for capability_definition in capability_definitions.itervalues():
        capability_definition._reset_method_cache()

    return capability_definitions
def merge_requirement_assignment_relationship(context, presentation,
                                              property_definitions,
                                              interface_definitions,
                                              requirement, our_relationship):
    our_relationship_properties = our_relationship._raw.get('properties')
    if our_relationship_properties:
        # Make sure we have a dict
        if 'properties' not in requirement._raw['relationship']:
            requirement._raw['relationship']['properties'] = OrderedDict()

        # Merge our properties
        for property_name, prop in our_relationship_properties.iteritems():
            if property_name in property_definitions:
                definition = property_definitions[property_name]
                requirement._raw['relationship']['properties'][property_name] = \
                    coerce_parameter_value(context, presentation, definition, prop)
            else:
                context.validation.report(
                    'relationship property "%s" not declared at definition of requirement "%s"'
                    ' in "%s"' %
                    (property_name, requirement._fullname,
                     presentation._container._container._fullname),
                    locator=our_relationship._get_child_locator(
                        'properties', property_name),
                    level=Issue.BETWEEN_TYPES)

    our_interfaces = our_relationship.interfaces
    if our_interfaces:
        # Make sure we have a dict
        if 'interfaces' not in requirement._raw['relationship']:
            requirement._raw['relationship']['interfaces'] = OrderedDict()

        # Merge interfaces
        for interface_name, our_interface in our_interfaces.iteritems():
            if interface_name not in requirement._raw['relationship'][
                    'interfaces']:
                requirement._raw['relationship']['interfaces'][
                    interface_name] = OrderedDict()

            if (interface_definitions
                    is not None) and (interface_name in interface_definitions):
                interface_definition = interface_definitions[interface_name]
                interface_assignment = requirement.relationship.interfaces[
                    interface_name]
                merge_interface(context, presentation, interface_assignment,
                                our_interface, interface_definition,
                                interface_name)
            else:
                context.validation.report(
                    'relationship interface "%s" not declared at definition of requirement "%s"'
                    ' in "%s"' %
                    (interface_name, requirement._fullname,
                     presentation._container._container._fullname),
                    locator=our_relationship._locator,
                    level=Issue.BETWEEN_TYPES)
Example #7
0
def merge_interface_definition(context, interface, our_source, presentation,
                               type_name):
    if hasattr(our_source, 'type'):
        # Check if we changed the interface type
        type1 = interface._get_type(context)
        type2 = our_source._get_type(context)

        if (type2 is not None) and not type1._is_descendant(context, type2):
            context.validation.report(
                u'interface definition type "{0}" is not a descendant of overridden '
                u'interface definition type "{1}"' \
                .format(type1._name, type2._name),
                locator=our_source._locator, level=Issue.BETWEEN_TYPES)

    # Add/merge inputs
    our_interface_inputs = our_source._get_inputs(context) \
        if hasattr(our_source, '_get_inputs') else our_source.inputs
    if our_interface_inputs:
        # Make sure we have the dict
        if ('inputs' not in interface._raw) or (interface._raw.get('inputs') is
                                                None):
            interface._raw['inputs'] = OrderedDict()

        merge_raw_input_definitions(context, interface._raw['inputs'],
                                    our_interface_inputs, our_source._name,
                                    None, presentation, type_name)

    # Add/merge operations
    our_operations = our_source._get_operations(context) \
        if hasattr(our_source, '_get_operations') else our_source.operations
    if our_operations is not None:
        merge_raw_operation_definitions(context, interface._raw,
                                        our_operations, our_source._name,
                                        presentation, type_name)
def merge_requirement_assignment(context, relationship_property_definitions,
                                 relationship_interface_definitions,
                                 requirement, our_requirement):
    our_capability = our_requirement.capability
    if our_capability is not None:
        requirement._raw['capability'] = deepcopy_with_locators(our_capability)

    our_node = our_requirement.node
    if our_node is not None:
        requirement._raw['node'] = deepcopy_with_locators(our_node)

    our_node_filter = our_requirement.node_filter
    if our_node_filter is not None:
        requirement._raw['node_filter'] = deepcopy_with_locators(
            our_node_filter._raw)

    our_relationship = our_requirement.relationship  # RelationshipAssignment
    if (our_relationship is not None) and (our_relationship.type is None):
        # Make sure we have a dict
        if 'relationship' not in requirement._raw:
            requirement._raw['relationship'] = OrderedDict()

        merge_requirement_assignment_relationship(
            context, our_relationship, relationship_property_definitions,
            relationship_interface_definitions, requirement, our_relationship)
Example #9
0
def convert_property_definitions_to_values(context, definitions):
    values = OrderedDict()
    for name, definition in definitions.iteritems():
        default = definition.default
        values[name] = coerce_property_value(context, definition, definition,
                                             default)
    return values
Example #10
0
def get_inherited_interface_definitions(context,
                                        presentation,
                                        type_name,
                                        for_presentation=None):
    """
    Returns our interface definitions added on top of those of our parent, if we have one
    (recursively).

    Allows overriding all aspects of parent interfaces except interface and operation input data
    types.
    """

    # Get interfaces from parent
    parent = presentation._get_parent(context)
    interfaces = get_inherited_interface_definitions(context, parent, type_name, presentation) \
        if parent is not None else OrderedDict()

    # Add/merge interfaces from their types
    merge_interface_definitions_from_their_types(context, interfaces,
                                                 presentation)

    # Add/merge our interfaces
    our_interfaces = presentation.interfaces
    merge_interface_definitions(context,
                                interfaces,
                                our_interfaces,
                                presentation,
                                for_presentation=for_presentation)

    return interfaces
Example #11
0
def merge_capability_definition_from_type(context, presentation, capability_definition):
    """
    Merge ``properties`` and ``valid_source_types`` from the node type's capability definition
    over those taken from the parent node type.
    """
    raw_properties = OrderedDict()

    # Merge properties from parent
    the_type = capability_definition._get_type(context)
    type_property_defintions = the_type._get_properties(context)
    merge_raw_parameter_definitions(context, presentation, raw_properties, type_property_defintions,
                                    'properties')

    # Merge our properties (might override definitions in parent)
    merge_raw_parameter_definitions(context, presentation, raw_properties,
                                    capability_definition.properties, 'properties')

    if raw_properties:
        capability_definition._raw['properties'] = raw_properties

    # Override valid_source_types
    if capability_definition._raw.get('valid_source_types') is None:
        valid_source_types = the_type._get_valid_source_types(context)
        if valid_source_types is not None:
            capability_definition._raw['valid_source_types'] = \
                deepcopy_with_locators(valid_source_types)
Example #12
0
def get_and_override_input_definitions_from_type(context, presentation):
    """
    Returns our input definitions added on top of those of the interface type, if specified.

    Allows overriding all aspects of parent interface type inputs except data types.
    """

    inputs = OrderedDict()

    # Get inputs from type
    the_type = presentation._get_type(context)  # IntefaceType
    type_inputs = the_type._get_inputs(
        context) if the_type is not None else None
    if type_inputs:
        for input_name, type_input in type_inputs.iteritems():
            inputs[input_name] = type_input._clone(presentation)

    # Add/merge our inputs
    our_inputs = presentation.inputs  # PropertyDefinition
    if our_inputs:
        merge_input_definitions(context, inputs, our_inputs,
                                presentation._name, None, presentation,
                                'definition')

    return inputs
Example #13
0
def merge_interface_definition(context, interface, our_source, presentation,
                               type_name):
    if hasattr(our_source, 'type'):
        # Check if we changed the interface type
        input_type1 = interface.type
        input_type2 = our_source.type
        if (input_type1 is not None) and (input_type2 is not None) and (
                input_type1 != input_type2):
            context.validation.report(
                'interface definition "%s" changes type from "%s" to "%s" in "%s"'
                % (interface._name, input_type1, input_type2,
                   presentation._fullname),
                locator=input_type2._locator,
                level=Issue.BETWEEN_TYPES)

    # Add/merge inputs
    our_interface_inputs = our_source._get_inputs(context) \
        if hasattr(our_source, '_get_inputs') else our_source.inputs
    if our_interface_inputs:
        # Make sure we have the dict
        if ('inputs' not in interface._raw) or (interface._raw.get('inputs') is
                                                None):
            interface._raw['inputs'] = OrderedDict()

        merge_raw_input_definitions(context, interface._raw['inputs'],
                                    our_interface_inputs, our_source._name,
                                    None, presentation, type_name)

    # Add/merge operations
    our_operations = our_source._get_operations(context) \
        if hasattr(our_source, '_get_operations') else our_source.operations
    if our_operations is not None:
        merge_raw_operation_definitions(context, interface._raw,
                                        our_operations, our_source._name,
                                        presentation, type_name)
Example #14
0
def merge_raw_operation_definition(context, raw_operation, our_operation,
                                   interface_name, presentation, type_name):
    if not isinstance(our_operation._raw, dict):
        # Convert short form to long form
        raw_operation['implementation'] = deepcopy_with_locators(
            our_operation._raw)
        return

    # Add/merge inputs
    our_operation_inputs = our_operation.inputs
    if our_operation_inputs:
        # Make sure we have the dict
        if ('inputs'
                not in raw_operation) or (raw_operation.get('inputs') is None):
            raw_operation['inputs'] = OrderedDict()

        merge_raw_input_definitions(context, raw_operation['inputs'],
                                    our_operation_inputs, interface_name,
                                    our_operation._name, presentation,
                                    type_name)

    # Override the description
    if our_operation._raw.get('description') is not None:
        raw_operation['description'] = deepcopy_with_locators(
            our_operation._raw['description'])

    # Add/merge implementation
    if our_operation._raw.get('implementation') is not None:
        if raw_operation.get('implementation') is not None:
            merge(raw_operation['implementation'],
                  deepcopy_with_locators(our_operation._raw['implementation']))
        else:
            raw_operation['implementation'] = \
                deepcopy_with_locators(our_operation._raw['implementation'])
Example #15
0
def assign_raw_inputs(context, values, assignments, definitions,
                      interface_name, operation_name, presentation):
    if not assignments:
        return

    # Make sure we have the dict
    if ('inputs' not in values) or (values['inputs'] is None):
        values['inputs'] = OrderedDict()

    # Assign inputs
    for input_name, assignment in assignments.iteritems():
        if (definitions is not None) and (input_name not in definitions):
            if operation_name is not None:
                context.validation.report(
                    'interface definition "%s" assigns a value to an unknown operation input'
                    ' "%s.%s" in "%s"' % (interface_name, operation_name,
                                          input_name, presentation._fullname),
                    locator=assignment._locator,
                    level=Issue.BETWEEN_TYPES)
            else:
                context.validation.report(
                    'interface definition "%s" assigns a value to an unknown input "%s" in "%s"'
                    % (interface_name, input_name, presentation._fullname),
                    locator=assignment._locator,
                    level=Issue.BETWEEN_TYPES)

        definition = definitions.get(
            input_name) if definitions is not None else None

        # Note: default value has already been assigned

        # Coerce value
        values['inputs'][input_name] = coerce_property_value(
            context, assignment, definition, assignment.value)
Example #16
0
def validate_substitution_mappings_interface(context, presentation, type_name = "node template"):
    # Validate that the capability in substitution_mapping is defined in the substitution node type
    #print "Inside intrface Validation method of substitution mapping"
    #print "Interfce presetation -->", presentation.__dict__
    #print "Interfce contaiiner -->", presentation._container.__dict__

    template_interfaces = OrderedDict()
    substitution_node_type = presentation._container._get_type(context)         # aka the_type  --> NodeType
    #print ("Substitution mapping interface node type -------------------> ", substitution_node_type)
    #print ("Substitution mapping interface node type dict-------------------> ", substitution_node_type.__dict__)
    #print ("Substitution mapping interface node type container -------------------> ", substitution_node_type.__dict__['_container'])

    if substitution_node_type is None:
        return

    substitution_type_interfaces = substitution_node_type._get_interfaces(context)   #aka interface_definitions

    substitution_type_interface = substitution_type_interfaces.get(presentation._name)

    print "Substitution type interface ------------->", substitution_type_interface
    if substitution_type_interface is None:
        context.validation.report(
            u'substitution mapping interface "{0}" '
            u'is not declared in node type "{substitution_type}"'.format(
                presentation._name, substitution_type=substitution_node_type._name),
            locator=presentation._locator, level=Issue.BETWEEN_TYPES)
        return


    """
    template_interfaces = OrderedDict()

    the_type = presentation._get_type(context)  # NodeType, RelationshipType, GroupType
    # InterfaceDefinition (or InterfaceAssignment in the case of RelationshipTemplate):
    interface_definitions = the_type._get_interfaces(context) if the_type is not None else None
    """
    """
    operation_name = presentation._raw[0]
    our_interface_name = presentation._raw[1]
    # Copy over interfaces from the type (will initialize inputs with default values)
    if substitution_type_interfaces:
        for interface_name, interface_definition in substitution_type_interfaces.iteritems():
            # Note that in the case of a RelationshipTemplate, we will already have the values as
            # InterfaceAssignment.
            if interface_name == our_interface_name:
               operation_definitions = interface_definition._get_operations(context)
               operation_definition = operation_definitions.get(operation_name)
               if operation_definition is None:
                  context.validation.report(
                    u'interface definition "{0}" refers to an unknown operation "{1}" in "{2}"'
                    .format(interface_name, operation_name, presentation._fullname),
                    locator=presentation._locator,level=Issue.BETWEEN_TYPES)

          #  template_interfaces[interface_name] = \
          #      convert_interface_definition_from_type_to_template(context, interface_definition,
          #                                                         presentation)
    """
    """
Example #17
0
def create_operation_template_model(context, service_template, operation):
    model = OperationTemplate(name=operation._name)

    if operation.description:
        model.description = operation.description.value

    implementation = operation.implementation
    if implementation is not None:
        primary = implementation.primary
        extract_implementation_primary(context, service_template, operation,
                                       model, primary)
        relationship_edge = operation._get_extensions(context).get(
            'relationship_edge')
        if relationship_edge is not None:
            if relationship_edge == 'source':
                model.relationship_edge = False
            elif relationship_edge == 'target':
                model.relationship_edge = True

        dependencies = implementation.dependencies
        configuration = OrderedDict()
        if dependencies:
            for dependency in dependencies:
                key, value = split_prefix(dependency)
                if key is not None:
                    # Special ARIA prefix: signifies configuration parameters

                    # Parse as YAML
                    try:
                        value = yaml.load(value)
                    except yaml.parser.MarkedYAMLError as e:
                        context.validation.report(
                            'YAML parser {0} in operation configuration: {1}'.
                            format(e.problem, value),
                            locator=implementation._locator,
                            level=Issue.FIELD)
                        continue

                    # Coerce to intrinsic functions, if there are any
                    value = coerce_parameter_value(context, implementation,
                                                   None, value).value

                    # Support dot-notation nesting
                    set_nested(configuration, key.split('.'), value)
                else:
                    if model.dependencies is None:
                        model.dependencies = []
                    model.dependencies.append(dependency)

        # Convert configuration to Configuration models
        for key, value in configuration.iteritems():
            model.configurations[key] = Configuration.wrap(
                key, value, description='Operation configuration.')

    create_parameter_models_from_assignments(model.inputs,
                                             operation.inputs,
                                             model_cls=Input)
    return model
Example #18
0
    def _get_columns_from_field_names(self, include, filters, sort):
        """Go over the optional parameters (include, filters, sort), and
        replace column names with actual SQLA column objects
        """
        include = [self._get_column(c) for c in include]
        filters = dict((self._get_column(c), filters[c]) for c in filters)
        sort = OrderedDict((self._get_column(c), sort[c]) for c in sort)

        return include, filters, sort
Example #19
0
def get_template_capabilities(context, presentation):
    """
    Returns the node type's capabilities with our assignments to properties and attributes merged
    in.

    Capability properties' default values, if available, will be used if we did not assign them.

    Makes sure that required properties indeed end up with a value.
    """

    capability_assignments = OrderedDict()

    the_type = presentation._get_type(context)  # NodeType
    capability_definitions = the_type._get_capabilities(
        context) if the_type is not None else None

    # Copy over capability definitions from the type (will initialize properties with default
    # values)
    if capability_definitions:
        for capability_name, capability_definition in capability_definitions.iteritems(
        ):
            capability_assignments[capability_name] = \
                convert_capability_from_definition_to_assignment(context, capability_definition,
                                                                 presentation)

    # Fill in our capability assignments
    our_capability_assignments = presentation.capabilities
    if our_capability_assignments:
        for capability_name, our_capability_assignment in our_capability_assignments.iteritems(
        ):
            if capability_name in capability_assignments:
                capability_assignment = capability_assignments[capability_name]

                # Assign properties
                values = get_assigned_and_defined_parameter_values(
                    context, our_capability_assignment, 'property')

                if values:
                    capability_assignment._raw['properties'] = values
                    capability_assignment._reset_method_cache()

                # Assign attributes
                values = get_assigned_and_defined_parameter_values(
                    context, our_capability_assignment, 'attribute')

                if values:
                    capability_assignment._raw['attributes'] = values
                    capability_assignment._reset_method_cache()
            else:
                context.validation.report(
                    u'capability "{0}" not declared at node type "{1}" in "{2}"'
                    .format(capability_name, presentation.type,
                            presentation._fullname),
                    locator=our_capability_assignment._locator,
                    level=Issue.BETWEEN_TYPES)

    return capability_assignments
Example #20
0
def get_assigned_and_defined_parameter_values(context, presentation,
                                              field_name):
    """
    Returns the assigned parameter values while making sure they are defined in our type.

    The parameter definition's default value, if available, will be used if we did not assign it.

    Makes sure that required parameters indeed end up with a value.
    """

    values = OrderedDict()

    the_type = presentation._get_type(context)
    field_name_plural = pluralize(field_name)
    assignments = getattr(presentation, field_name_plural)
    get_fn_name = '_get_{0}'.format(field_name_plural)
    definitions = getattr(
        the_type, get_fn_name)(context) if the_type is not None else None

    # Fill in our assignments, but make sure they are defined
    if assignments:
        for name, value in assignments.iteritems():
            if (definitions is not None) and (name in definitions):
                definition = definitions[name]
                values[name] = coerce_parameter_value(context, value,
                                                      definition, value.value)
            else:
                context.validation.report(
                    u'assignment to undefined {0} "{1}" in "{2}"'.format(
                        field_name, name, presentation._fullname),
                    locator=value._locator,
                    level=Issue.BETWEEN_TYPES)

    # Fill in defaults from the definitions
    if definitions:
        for name, definition in definitions.iteritems():
            # Note: attributes will always have a default value, even if it's None
            if (name not in values) and \
                (('default' in definition._raw) or (field_name == 'attribute')):
                values[name] = coerce_parameter_value(context, presentation,
                                                      definition,
                                                      definition.default,
                                                      'default')

    validate_required_values(context, presentation, values, definitions)

    # Fill in nulls for missing values that are *not* required
    if definitions:
        for name, definition in definitions.iteritems():
            if (name not in values) and not getattr(definition, 'required',
                                                    False):
                values[name] = coerce_parameter_value(context, presentation,
                                                      definition, None)

    return values
Example #21
0
def convert_capability_from_definition_to_assignment(context, presentation, container):
    from ..assignments import CapabilityAssignment

    raw = OrderedDict()

    properties = presentation.properties
    if properties is not None:
        raw['properties'] = convert_parameter_definitions_to_values(context, properties)

    # TODO attributes

    return CapabilityAssignment(name=presentation._name, raw=raw, container=container)
Example #22
0
def merge_interface(context, presentation, interface_assignment,
                    our_interface_assignment, interface_definition,
                    interface_name):
    # Assign/merge interface inputs
    assign_raw_inputs(context, interface_assignment._raw,
                      our_interface_assignment.inputs,
                      interface_definition._get_inputs(context),
                      interface_name, None, presentation)

    our_operation_templates = our_interface_assignment.operations  # OperationAssignment
    if our_operation_templates is None:
        our_operation_templates = {}

    # OperationDefinition or OperationAssignment:
    operation_definitions = interface_definition._get_operations(context) \
        if hasattr(interface_definition, '_get_operations') else interface_definition.operations
    if operation_definitions is None:
        operation_definitions = {}

    # OperationAssignment:
    for operation_name, our_operation_template in our_operation_templates.iteritems(
    ):
        operation_definition = operation_definitions.get(
            operation_name)  # OperationDefinition

        our_input_assignments = our_operation_template.inputs
        our_implementation = our_operation_template.implementation

        if operation_definition is None:
            context.validation.report(
                u'interface definition "{0}" refers to an unknown operation "{1}" in "{2}"'
                .format(interface_name, operation_name,
                        presentation._fullname),
                locator=our_operation_template._locator,
                level=Issue.BETWEEN_TYPES)

        # Make sure we have the dict
        if (operation_name not in interface_assignment._raw) \
            or (interface_assignment._raw[operation_name] is None):
            interface_assignment._raw[operation_name] = OrderedDict()

        if our_implementation is not None:
            interface_assignment._raw[operation_name]['implementation'] = \
                deepcopy_with_locators(our_implementation._raw)

        # Assign/merge operation inputs
        input_definitions = operation_definition.inputs \
            if operation_definition is not None else None
        assign_raw_inputs(context, interface_assignment._raw[operation_name],
                          our_input_assignments, input_definitions,
                          interface_name, operation_name, presentation)
Example #23
0
def merge_capability_definition(context, presentation, capability_definition,
                                from_capability_definition):
    capability_definition._raw['type'] = from_capability_definition.type

    raw_properties = OrderedDict()
    raw_attributes = OrderedDict()

    # Merge parameters from type
    merge_raw_parameter_definitions(context, presentation, raw_properties,
                                    capability_definition.properties,
                                    'properties')
    merge_raw_parameter_definitions(context, presentation, raw_attributes,
                                    capability_definition.attributes,
                                    'attributes')

    # Merge our parameters
    merge_raw_parameter_definitions(context, presentation, raw_properties,
                                    from_capability_definition.properties,
                                    'properties')
    merge_raw_parameter_definitions(context, presentation, raw_attributes,
                                    from_capability_definition.attributes,
                                    'attributes')

    if raw_properties:
        capability_definition._raw['properties'] = raw_properties
        capability_definition._reset_method_cache()
    if raw_attributes:
        capability_definition._raw['attributes'] = raw_attributes
        capability_definition._reset_method_cache()

    # Merge occurrences
    occurrences = from_capability_definition._raw.get('occurrences')
    if (occurrences is not None) and (
            capability_definition._raw.get('occurrences') is None):
        capability_definition._raw['occurrences'] = \
            deepcopy_with_locators(occurrences)
Example #24
0
    def _get_joins_and_converted_columns(self, include, filters, sort):
        """Get a list of tables on which we need to join and the converted
        `include`, `filters` and `sort` arguments (converted to actual SQLA
        column/label objects instead of column names)
        """
        include = include or []
        filters = filters or dict()
        sort = sort or OrderedDict()

        all_columns = set(include) | set(filters.keys()) | set(sort.keys())
        joins = self._get_joins(self.model_cls, all_columns)

        include, filters, sort = self._get_columns_from_field_names(
            include, filters, sort)
        return include, filters, sort, joins
Example #25
0
def merge_raw_operation_definitions(context, raw_operations, our_operations,
                                    interface_name, presentation, type_name):
    for operation_name, our_operation in our_operations.iteritems():
        if operation_name in raw_operations:
            raw_operation = raw_operations[operation_name]
            if isinstance(raw_operation, basestring):
                # Convert short form to long form
                raw_operations[operation_name] = OrderedDict(
                    (('implementation', raw_operation), ))
                raw_operation = raw_operations[operation_name]
            merge_raw_operation_definition(context, raw_operation,
                                           our_operation, interface_name,
                                           presentation, type_name)
        else:
            raw_operations[operation_name] = deepcopy_with_locators(
                our_operation._raw)
Example #26
0
def convert_requirement_interface_definitions_from_type_to_raw_template(
        context,
        raw_requirement,  # pylint: disable=invalid-name
        interface_definitions):
    if not interface_definitions:
        return
    if 'interfaces' not in raw_requirement:
        raw_requirement['interfaces'] = OrderedDict()
    for interface_name, interface_definition in interface_definitions.iteritems(
    ):
        raw_interface = convert_interface_definition_from_type_to_raw_template(
            context, interface_definition)
        if interface_name in raw_requirement['interfaces']:
            merge(raw_requirement['interfaces'][interface_name], raw_interface)
        else:
            raw_requirement['interfaces'][interface_name] = raw_interface
def get_assigned_and_defined_parameter_values(context, presentation,
                                              field_name):
    """
    Returns the assigned property values while making sure they are defined in our type.

    The property definition's default value, if available, will be used if we did not assign it.

    Makes sure that required properties indeed end up with a value.
    """

    values = OrderedDict()

    the_type = presentation._get_type(context)
    field_name_plural = pluralize(field_name)
    assignments = getattr(presentation, field_name_plural)
    get_fn_name = '_get_{0}'.format(field_name_plural)
    definitions = getattr(
        the_type, get_fn_name)(context) if the_type is not None else None

    # Fill in our assignments, but make sure they are defined
    if assignments:
        for name, value in assignments.iteritems():
            if (definitions is not None) and (name in definitions):
                definition = definitions[name]
                values[name] = coerce_parameter_value(context, value,
                                                      definition, value.value)
            else:
                context.validation.report(
                    'assignment to undefined {0} "{1}" in "{2}"'.format(
                        field_name, name, presentation._fullname),
                    locator=value._locator,
                    level=Issue.BETWEEN_TYPES)

    # Fill in defaults from the definitions
    if definitions:
        for name, definition in definitions.iteritems():
            if values.get(name) is None:
                values[name] = coerce_parameter_value(context, presentation,
                                                      definition,
                                                      definition.default)

    validate_required_values(context, presentation, values, definitions)

    return values
def get_inherited_artifact_definitions(context, presentation, for_presentation=None):

    if hasattr(presentation, '_get_type'):
        # In NodeTemplate
        parent = presentation._get_type(context)
    else:
        # In NodeType
        parent = presentation._get_parent(context)

    # Get artifact definitions from parent
    artifacts = get_inherited_artifact_definitions(context, parent, for_presentation=presentation) \
        if parent is not None else OrderedDict()

    # Add/override our artifact definitions
    our_artifacts = presentation.artifacts
    if our_artifacts:
        for artifact_name, artifact in our_artifacts.iteritems():
            artifacts[artifact_name] = artifact._clone(for_presentation)

    return artifacts
Example #29
0
def assign_raw_inputs(context, raw, assignments, definitions, interface_name,
                      operation_name, presentation):
    if assignments is None:
        assignments = {}
    if definitions is None:
        definitions = {}

    # Make sure we have the dict
    if ('inputs' not in raw) or (raw['inputs'] is None):
        raw['inputs'] = OrderedDict()

    # Defaults
    for input_name, definition in definitions.iteritems():
        if ('default' in definition._raw) and (input_name
                                               not in raw['inputs']):
            raw['inputs'][input_name] = coerce_parameter_value(
                context, definition, definition, definition.default, 'default')

    # Assign inputs
    for input_name, assignment in assignments.iteritems():
        if (not context.presentation.configuration.get('tosca.adhoc_inputs', True)) and \
            (input_name not in definitions):
            if operation_name is not None:
                context.validation.report(
                    u'interface definition "{0}" assigns a value to an unknown operation input'
                    u' "{1}.{2}" in "{3}"'.format(interface_name,
                                                  operation_name, input_name,
                                                  presentation._fullname),
                    locator=assignment._locator,
                    level=Issue.BETWEEN_TYPES)
            else:
                context.validation.report(
                    u'interface definition "{0}" assigns a value to an unknown input "{1}" in "{2}"'
                    .format(interface_name, input_name,
                            presentation._fullname),
                    locator=assignment._locator,
                    level=Issue.BETWEEN_TYPES)

        definition = definitions.get(input_name)  # Could be None!
        raw['inputs'][input_name] = coerce_parameter_value(
            context, assignment, definition, assignment.value)
Example #30
0
def get_assigned_and_defined_property_values(context, presentation):
    """
    Returns the assigned property values while making sure they are defined in our type.

    The property definition's default value, if available, will be used if we did not assign it.

    Makes sure that required properties indeed end up with a value.
    """

    values = OrderedDict()

    the_type = presentation._get_type(context)
    assignments = presentation.properties
    definitions = the_type._get_properties(
        context) if the_type is not None else None

    # Fill in our assignments, but make sure they are defined
    if assignments:
        for name, value in assignments.iteritems():
            if (definitions is not None) and (name in definitions):
                definition = definitions[name]
                values[name] = coerce_property_value(context, value,
                                                     definition, value.value)
            else:
                context.validation.report(
                    'assignment to undefined property "%s" in "%s"' %
                    (name, presentation._fullname),
                    locator=value._locator,
                    level=Issue.BETWEEN_TYPES)

    # Fill in defaults from the definitions
    if definitions:
        for name, definition in definitions.iteritems():
            if (values.get(name) is None) and (definition.default is not None):
                values[name] = coerce_property_value(context, presentation,
                                                     definition,
                                                     definition.default)

    validate_required_values(context, presentation, values, definitions)

    return values