http://git-wip-us.apache.org/repos/asf/incubator-ariatosca-website/blob/23d6ba76/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/definitions.py ---------------------------------------------------------------------- diff --git a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/definitions.py b/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/definitions.py deleted file mode 100644 index 1bd0366..0000000 --- a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/definitions.py +++ /dev/null @@ -1,509 +0,0 @@ -# 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 aria.utils.collections import FrozenDict -from aria.utils.caching import cachedmethod -from aria.parser import implements_specification -from aria.parser.presentation import (has_fields, short_form_field, allow_unknown_fields, - primitive_field, primitive_list_field, object_field, - object_list_field, object_dict_field, - object_dict_unknown_fields, field_validator, - field_getter, type_validator, list_type_validator) - -from .data_types import Range -from .misc import (Description, ConstraintClause, OperationImplementation, EntrySchema) -from .presentation.extensible import ExtensiblePresentation -from .presentation.field_getters import data_type_class_getter -from .presentation.field_validators import (data_type_validator, data_value_validator, - entry_schema_validator) -from .presentation.types import (convert_shorthand_to_full_type_name, - get_type_by_full_or_shorthand_name) -from .modeling.data_types import get_data_type, get_property_constraints -from .modeling.interfaces import (get_and_override_input_definitions_from_type, - get_and_override_operation_definitions_from_type) - -@has_fields -@implements_specification('3.5.8', 'tosca-simple-1.0') -class PropertyDefinition(ExtensiblePresentation): - """ - A property definition defines a named, typed value and related data that can be associated with - an entity defined in this specification (e.g., Node Types, Relationship Types, Capability Types, - etc.). Properties are used by template authors to provide input values to TOSCA entities which - indicate their "desired state" when they are instantiated. The value of a property can be - retrieved using the ``get_property`` function within TOSCA Service Templates. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_PROPERTY_DEFN>`__ - """ - - @field_validator(data_type_validator()) - @primitive_field(str, required=True) - def type(self): - """ - The required data type for the property. - - :type: :obj:`basestring` - """ - - @object_field(Description) - def description(self): - """ - The optional description for the property. - - :type: :class:`Description` - """ - - @primitive_field(bool, default=True) - def required(self): - """ - An optional key that declares a property as required (true) or not (false). - - :type: bool - """ - - @field_validator(data_value_validator) - @primitive_field() - def default(self): - """ - An optional key that may provide a value to be used as a default if not provided by another - means. - - :type: :obj:`basestring` - """ - - @primitive_field(str, default='supported', allowed=('supported', 'unsupported', 'experimental', - 'deprecated')) - @implements_specification(section='3.5.8.3', spec='tosca-simple-1.0') - def status(self): - """ - The optional status of the property relative to the specification or implementation. - - :type: :obj:`basestring` - """ - - @object_list_field(ConstraintClause) - def constraints(self): - """ - The optional list of sequenced constraint clauses for the property. - - :type: list of (str, :class:`ConstraintClause`) - """ - - @field_validator(entry_schema_validator) - @object_field(EntrySchema) - def entry_schema(self): - """ - The optional key that is used to declare the name of the Datatype definition for entries of - set types such as the TOSCA list or map. - - :type: :obj:`basestring` - """ - - @cachedmethod - def _get_type(self, context): - return get_data_type(context, self, 'type') - - @cachedmethod - def _get_constraints(self, context): - return get_property_constraints(context, self) - -@has_fields -@implements_specification('3.5.10', 'tosca-simple-1.0') -class AttributeDefinition(ExtensiblePresentation): - """ - An attribute definition defines a named, typed value that can be associated with an entity - defined in this specification (e.g., a Node, Relationship or Capability Type). Specifically, it - is used to expose the "actual state" of some property of a TOSCA entity after it has been - deployed and instantiated (as set by the TOSCA orchestrator). Attribute values can be retrieved - via the ``get_attribute`` function from the instance model and used as values to other - entities within TOSCA Service Templates. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_ATTRIBUTE_DEFN>`__ - """ - - @field_validator(data_type_validator()) - @primitive_field(str, required=True) - def type(self): - """ - The required data type for the attribute. - - :type: :obj:`basestring` - """ - - @object_field(Description) - def description(self): - """ - The optional description for the attribute. - - :type: :class:`Description` - """ - - @field_validator(data_value_validator) - @primitive_field() - def default(self): - """ - An optional key that may provide a value to be used as a default if not provided by another - means. - - This value SHALL be type compatible with the type declared by the property definition's type - keyname. - - :type: :obj:`basestring` - """ - - @primitive_field(str, default='supported', allowed=('supported', 'unsupported', 'experimental', - 'deprecated')) - def status(self): - """ - The optional status of the attribute relative to the specification or implementation. - - :type: :obj:`basestring` - """ - - @field_validator(entry_schema_validator) - @object_field(EntrySchema) - def entry_schema(self): - """ - The optional key that is used to declare the name of the Datatype definition for entries of - set types such as the TOSCA list or map. - - :type: :obj:`basestring` - """ - - @cachedmethod - def _get_type(self, context): - return get_data_type(context, self, 'type') - -@has_fields -@implements_specification('3.5.12', 'tosca-simple-1.0') -class ParameterDefinition(PropertyDefinition): - """ - A parameter definition is essentially a TOSCA property definition; however, it also allows a - value to be assigned to it (as for a TOSCA property assignment). In addition, in the case of - output parameters, it can optionally inherit the data type of the value assigned to it rather - than have an explicit data type defined for it. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_PARAMETER_DEF>`__ - """ - - @field_validator(data_type_validator()) - @primitive_field(str) - def type(self): - """ - The required data type for the parameter. - - Note: This keyname is required for a TOSCA Property definition, but is not for a TOSCA - Parameter definition. - - :type: :obj:`basestring` - """ - - @field_validator(data_value_validator) - @primitive_field() - def value(self): - """ - The type-compatible value to assign to the named parameter. Parameter values may be provided - as the result from the evaluation of an expression or a function. - """ - -@short_form_field('implementation') -@has_fields -@implements_specification('3.5.13-1', 'tosca-simple-1.0') -class OperationDefinition(ExtensiblePresentation): - """ - An operation definition defines a named function or procedure that can be bound to an - implementation artifact (e.g., a script). - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_OPERATION_DEF>`__ - """ - - @object_field(Description) - def description(self): - """ - The optional description string for the associated named operation. - - :type: :class:`Description` - """ - - @object_field(OperationImplementation) - def implementation(self): - """ - The optional implementation artifact name (e.g., a script file name within a TOSCA CSAR - file). - - :type: :class:`OperationImplementation` - """ - - @object_dict_field(PropertyDefinition) - def inputs(self): - """ - The optional list of input property definitions available to all defined operations for - interface definitions that are within TOSCA Node or Relationship Type definitions. This - includes when interface definitions are included as part of a Requirement definition in a - Node Type. - - :type: {:obj:`basestring`: :class:`PropertyDefinition`} - """ - -@allow_unknown_fields -@has_fields -@implements_specification('3.5.14-1', 'tosca-simple-1.0') -class InterfaceDefinition(ExtensiblePresentation): - """ - An interface definition defines a named interface that can be associated with a Node or - Relationship Type. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_INTERFACE_DEF>`__ - """ - - @field_validator(type_validator('interface type', convert_shorthand_to_full_type_name, - 'interface_types')) - @primitive_field(str) - def type(self): - """ - ARIA NOTE: This field is not mentioned in the spec, but is implied. - - :type: :obj:`basestring` - """ - - @object_dict_field(PropertyDefinition) - def inputs(self): - """ - The optional list of input property definitions available to all defined operations for - interface definitions that are within TOSCA Node or Relationship Type definitions. This - includes when interface definitions are included as part of a Requirement definition in a - Node Type. - - :type: {:obj:`basestring`: :class:`PropertyDefinition`} - """ - - @object_dict_unknown_fields(OperationDefinition) - def operations(self): - """ - :type: {:obj:`basestring`: :class:`OperationDefinition`} - """ - - @cachedmethod - def _get_type(self, context): - return get_type_by_full_or_shorthand_name(context, self.type, 'interface_types') - - @cachedmethod - def _get_inputs(self, context): - return FrozenDict(get_and_override_input_definitions_from_type(context, self)) - - @cachedmethod - def _get_operations(self, context): - return FrozenDict(get_and_override_operation_definitions_from_type(context, self)) - - def _validate(self, context): - super(InterfaceDefinition, self)._validate(context) - if self.operations: - for operation in self.operations.itervalues(): # pylint: disable=no-member - operation._validate(context) - -@short_form_field('type') -@has_fields -class RelationshipDefinition(ExtensiblePresentation): - """ - Relationship definition. - """ - - @field_validator(type_validator('relationship type', convert_shorthand_to_full_type_name, - 'relationship_types')) - @primitive_field(str, required=True) - def type(self): - """ - The optional reserved keyname used to provide the name of the Relationship Type for the - requirement definition's relationship keyname. - - :type: :obj:`basestring` - """ - - @object_dict_field(InterfaceDefinition) - def interfaces(self): - """ - The optional reserved keyname used to reference declared (named) interface definitions of - the corresponding Relationship Type in order to declare additional Property definitions for - these interfaces or operations of these interfaces. - - :type: list of :class:`InterfaceDefinition` - """ - - @cachedmethod - def _get_type(self, context): - return get_type_by_full_or_shorthand_name(context, self.type, 'relationship_types') - -@short_form_field('capability') -@has_fields -@implements_specification('3.6.2', 'tosca-simple-1.0') -class RequirementDefinition(ExtensiblePresentation): - """ - The Requirement definition describes a named requirement (dependencies) of a TOSCA Node Type or - Node template which needs to be fulfilled by a matching Capability definition declared by - another TOSCA modelable entity. The requirement definition may itself include the specific name - of the fulfilling entity (explicitly) or provide an abstract type, along with additional - filtering characteristics, that a TOSCA orchestrator can use to fulfill the capability at - runtime (implicitly). - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_REQUIREMENT_DEF>`__ - """ - - @field_validator(type_validator('capability type', convert_shorthand_to_full_type_name, - 'capability_types')) - @primitive_field(str, required=True) - def capability(self): - """ - The required reserved keyname used that can be used to provide the name of a valid - Capability Type that can fulfill the requirement. - - :type: :obj:`basestring` - """ - - @field_validator(type_validator('node type', convert_shorthand_to_full_type_name, 'node_types')) - @primitive_field(str) - def node(self): - """ - The optional reserved keyname used to provide the name of a valid Node Type that contains - the capability definition that can be used to fulfill the requirement. - - :type: :obj:`basestring` - """ - - @object_field(RelationshipDefinition) - def relationship(self): - """ - The optional reserved keyname used to provide the name of a valid Relationship Type to - construct when fulfilling the requirement. - - :type: :class:`RelationshipDefinition` - """ - - @field_getter(data_type_class_getter(Range)) - @primitive_field() - def occurrences(self): - """ - The optional minimum and maximum occurrences for the requirement. - - Note: the keyword UNBOUNDED is also supported to represent any positive integer. - - :type: :class:`Range` - """ - - @cachedmethod - def _get_capability_type(self, context): - return get_type_by_full_or_shorthand_name(context, self.capability, 'capability_types') - - @cachedmethod - def _get_node_type(self, context): - return context.presentation.get_from_dict('service_template', 'node_types', self.node) - -@short_form_field('type') -@has_fields -@implements_specification('3.6.1', 'tosca-simple-1.0') -class CapabilityDefinition(ExtensiblePresentation): - """ - A capability definition defines a named, typed set of data that can be associated with Node Type - or Node Template to describe a transparent capability or feature of the software component the - node describes. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_CAPABILITY_DEFN>`__ - """ - - @field_validator(type_validator('capability type', convert_shorthand_to_full_type_name, - 'capability_types')) - @primitive_field(str, required=True) - def type(self): - """ - The required name of the Capability Type the capability definition is based upon. - - :type: :obj:`basestring` - """ - - @object_field(Description) - def description(self): - """ - The optional description of the Capability definition. - - :type: :class:`Description` - """ - - @object_dict_field(PropertyDefinition) - def properties(self): - """ - An optional list of property definitions for the Capability definition. - - :type: {:obj:`basestring`: :class:`PropertyDefinition`} - """ - - @object_dict_field(AttributeDefinition) - def attributes(self): - """ - An optional list of attribute definitions for the Capability definition. - - :type: {:obj:`basestring`: :class:`AttributeDefinition`} - """ - - @field_validator(list_type_validator('node type', convert_shorthand_to_full_type_name, - 'node_types')) - @primitive_list_field(str) - def valid_source_types(self): - """ - An optional list of one or more valid names of Node Types that are supported as valid - sources of any relationship established to the declared Capability Type. - - :type: [:obj:`basestring`] - """ - - @field_getter(data_type_class_getter(Range)) - @primitive_field() - def occurrences(self): - """ - The optional minimum and maximum occurrences for the capability. By default, an exported - Capability should allow at least one relationship to be formed with it with a maximum of - ``UNBOUNDED`` relationships. - - Note: the keyword ``UNBOUNDED`` is also supported to represent any positive integer. - - ARIA NOTE: The spec seems wrong here: the implied default should be ``[0,UNBOUNDED]``, not - ``[1,UNBOUNDED]``, otherwise it would imply that at 1 least one relationship *must* be - formed. - - :type: :class:`Range` - """ - - @cachedmethod - def _get_type(self, context): - return get_type_by_full_or_shorthand_name(context, self.type, 'capability_types') - - @cachedmethod - def _get_parent(self, context): - container_parent = self._container._get_parent(context) - container_parent_capabilities = container_parent._get_capabilities(context) \ - if container_parent is not None else None - return container_parent_capabilities.get(self._name) \ - if container_parent_capabilities is not None else None
http://git-wip-us.apache.org/repos/asf/incubator-ariatosca-website/blob/23d6ba76/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/filters.py ---------------------------------------------------------------------- diff --git a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/filters.py b/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/filters.py deleted file mode 100644 index 6db140d..0000000 --- a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/filters.py +++ /dev/null @@ -1,105 +0,0 @@ -# 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 aria.utils.caching import cachedmethod -from aria.parser import implements_specification -from aria.parser.presentation import (has_fields, object_sequenced_list_field, field_validator) - -from .misc import ConstraintClause -from .presentation.extensible import ExtensiblePresentation -from .presentation.field_validators import (node_filter_properties_validator, - node_filter_capabilities_validator) - -@has_fields -class CapabilityFilter(ExtensiblePresentation): - """ - Capability filter. - """ - - @object_sequenced_list_field(ConstraintClause) - def properties(self): - pass - - @cachedmethod - def _get_node_type(self, context): - return self._container._get_node_type(context) - - @cachedmethod - def _get_type_for_name(self, context, name): - node_type = self._get_node_type(context) - if node_type is not None: - capabilities = node_type._get_capabilities(context) - capability = capabilities.get(self._name) - properties = capability.properties if capability is not None else None - prop = properties.get(name) if properties is not None else None - return prop._get_type(context) if prop is not None else None - - return None - -@has_fields -@implements_specification('3.5.4', 'tosca-simple-1.0') -class NodeFilter(ExtensiblePresentation): - """ - A node filter definition defines criteria for selection of a TOSCA Node Template based upon the - template's property values, capabilities and capability properties. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_NODE_FILTER_DEFN>`__ - """ - - @field_validator(node_filter_properties_validator) - @object_sequenced_list_field(ConstraintClause) - @implements_specification('3.5.3', 'tosca-simple-1.0') - def properties(self): - """ - An optional sequenced list of property filters that would be used to select (filter) - matching TOSCA entities (e.g., Node Template, Node Type, Capability Types, etc.) based upon - their property definitions' values. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_PROPERTY_FILTER_DEFN>`__ - - :type: list of (str, :class:`ConstraintClause`) - """ - - @field_validator(node_filter_capabilities_validator) - @object_sequenced_list_field(CapabilityFilter) - def capabilities(self): - """ - An optional sequenced list of property filters that would be used to select (filter) - matching TOSCA entities (e.g., Node Template, Node Type, Capability Types, etc.) based upon - their capabilities' property definitions' values. - - :type: list of (str, :class:`CapabilityDefinition`) - """ - - @cachedmethod - def _get_node_type(self, context): - if hasattr(self._container, '_get_node'): - node_type, node_type_variant = self._container._get_node(context) - return node_type if node_type_variant == 'node_type' else None - return None - - @cachedmethod - def _get_type_for_name(self, context, name): - node_type = self._get_node_type(context) - if node_type is not None: - properties = node_type._get_properties(context) - prop = properties.get(name) - return prop._get_type(context) if prop is not None else None - - return None http://git-wip-us.apache.org/repos/asf/incubator-ariatosca-website/blob/23d6ba76/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/misc.py ---------------------------------------------------------------------- diff --git a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/misc.py b/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/misc.py deleted file mode 100644 index f4d43ac..0000000 --- a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/misc.py +++ /dev/null @@ -1,434 +0,0 @@ -# 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 aria.utils.caching import cachedmethod -from aria.utils.console import puts -from aria.utils.formatting import as_raw -from aria.parser import implements_specification -from aria.parser.presentation import (AsIsPresentation, has_fields, allow_unknown_fields, - short_form_field, primitive_field, primitive_list_field, - primitive_dict_unknown_fields, object_field, - object_list_field, object_dict_field, field_validator, - type_validator) - -from .modeling.data_types import (get_data_type, get_data_type_value, get_property_constraints, - apply_constraint_to_value) -from .modeling.substitution_mappings import (validate_subtitution_mappings_requirement, - validate_subtitution_mappings_capability) -from .presentation.extensible import ExtensiblePresentation -from .presentation.field_validators import (constraint_clause_field_validator, - constraint_clause_in_range_validator, - constraint_clause_valid_values_validator, - constraint_clause_pattern_validator, - data_type_validator) -from .presentation.types import (convert_shorthand_to_full_type_name, - get_type_by_full_or_shorthand_name) - -@implements_specification('3.5.1', 'tosca-simple-1.0') -class Description(AsIsPresentation): - """ - Human-readable description. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_DESCRIPTION>`__ - """ - - def __init__(self, name=None, raw=None, container=None, cls=None): # pylint: disable=unused-argument - super(Description, self).__init__(name, raw, container, cls=unicode) - - def _dump(self, context): - value = as_raw(self.value) - puts(context.style.meta(value)) - -@allow_unknown_fields -@has_fields -@implements_specification('3.9.3.2', 'tosca-simple-1.0') -class MetaData(ExtensiblePresentation): - """ - Meta data. - """ - - @primitive_field(str) - @implements_specification('3.9.3.3', 'tosca-simple-1.0') - def template_name(self): - """ - This optional metadata keyname can be used to declare the name of service template as a - single-line string value. - """ - - @primitive_field(str) - @implements_specification('3.9.3.4', 'tosca-simple-1.0') - def template_author(self): - """ - This optional metadata keyname can be used to declare the author(s) of the service template - as a single-line string value. - """ - - @primitive_field(str) - @implements_specification('3.9.3.5', 'tosca-simple-1.0') - def template_version(self): - """ - This optional metadata keyname can be used to declare a domain specific version of the - service template as a single-line string value. - """ - - @primitive_dict_unknown_fields() - def custom(self): - """ - :type: dict - """ - -@short_form_field('url') -@has_fields -@implements_specification('3.5.5', 'tosca-simple-1.0') -class Repository(ExtensiblePresentation): - """ - A repository definition defines a named external repository which contains deployment and - implementation artifacts that are referenced within the TOSCA Service Template. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_REPOSITORY_DEF>`__ - """ - - @object_field(Description) - def description(self): - """ - The optional description for the repository. - - :type: :class:`Description` - """ - - @primitive_field(str, required=True) - def url(self): - """ - The required URL or network address used to access the repository. - - :type: :obj:`basestring` - """ - - @primitive_field() - def credential(self): - """ - The optional Credential used to authorize access to the repository. - - :type: tosca.datatypes.Credential - """ - - @cachedmethod - def _get_credential(self, context): - return get_data_type_value(context, self, 'credential', 'tosca.datatypes.Credential') - -@short_form_field('file') -@has_fields -@implements_specification('3.5.7', 'tosca-simple-1.0') -class Import(ExtensiblePresentation): - """ - An import definition is used within a TOSCA Service Template to locate and uniquely name another - TOSCA Service Template file which has type and template definitions to be imported (included) - and referenced within another Service Template. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_IMPORT_DEF>`__ - """ - - @primitive_field(str, required=True) - def file(self): - """ - The required symbolic name for the imported file. - - :type: :obj:`basestring` - """ - - @primitive_field(str) - def repository(self): - """ - The optional symbolic name of the repository definition where the imported file can be found - as a string. - - :type: :obj:`basestring` - """ - - @primitive_field(str) - def namespace_uri(self): - """ - The optional namespace URI to that will be applied to type definitions found within the - imported file as a string. - - :type: :obj:`basestring` - """ - - @primitive_field(str) - def namespace_prefix(self): - """ - The optional namespace prefix (alias) that will be used to indicate the namespace_uri when - forming a qualified name (i.e., qname) when referencing type definitions from the imported - file. - - :type: :obj:`basestring` - """ - -@has_fields -@implements_specification('3.5.2-1', 'tosca-simple-1.0') -class ConstraintClause(ExtensiblePresentation): - """ - A constraint clause defines an operation along with one or more compatible values that can be - used to define a constraint on a property or parameter's allowed values when it is defined in a - TOSCA Service Template or one of its entities. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #DEFN_ELEMENT_CONSTRAINTS_CLAUSE>`__ - """ - - @field_validator(constraint_clause_field_validator) - @primitive_field() - def equal(self): - """ - Constrains a property or parameter to a value equal to ('=') the value declared. - """ - - @field_validator(constraint_clause_field_validator) - @primitive_field() - def greater_than(self): - """ - Constrains a property or parameter to a value greater than ('>') the value declared. - """ - - @field_validator(constraint_clause_field_validator) - @primitive_field() - def greater_or_equal(self): - """ - Constrains a property or parameter to a value greater than or equal to ('>=') the value - declared. - """ - - @field_validator(constraint_clause_field_validator) - @primitive_field() - def less_than(self): - """ - Constrains a property or parameter to a value less than ('<') the value declared. - """ - - @field_validator(constraint_clause_field_validator) - @primitive_field() - def less_or_equal(self): - """ - Constrains a property or parameter to a value less than or equal to ('<=') the value - declared. - """ - - @field_validator(constraint_clause_in_range_validator) - @primitive_list_field() - def in_range(self): - """ - Constrains a property or parameter to a value in range of (inclusive) the two values - declared. - - Note: subclasses or templates of types that declare a property with the ``in_range`` - constraint MAY only further restrict the range specified by the parent type. - """ - - @field_validator(constraint_clause_valid_values_validator) - @primitive_list_field() - def valid_values(self): - """ - Constrains a property or parameter to a value that is in the list of declared values. - """ - - @primitive_field(int) - def length(self): - """ - Constrains the property or parameter to a value of a given length. - """ - - @primitive_field(int) - def min_length(self): - """ - Constrains the property or parameter to a value to a minimum length. - """ - - @primitive_field(int) - def max_length(self): - """ - Constrains the property or parameter to a value to a maximum length. - """ - - @field_validator(constraint_clause_pattern_validator) - @primitive_field(str) - def pattern(self): - """ - Constrains the property or parameter to a value that is allowed by the provided regular - expression. - - Note: Future drafts of this specification will detail the use of regular expressions and - reference an appropriate standardized grammar. - """ - - @cachedmethod - def _get_type(self, context): - if hasattr(self._container, '_get_type_for_name'): - # NodeFilter or CapabilityFilter - return self._container._get_type_for_name(context, self._name) - elif hasattr(self._container, '_get_type'): - # Properties - return self._container._get_type(context) - else: - # DataType (the DataType itself is our type) - return self._container - - def _apply_to_value(self, context, presentation, value): - return apply_constraint_to_value(context, presentation, self, value) - -@short_form_field('type') -@has_fields -class EntrySchema(ExtensiblePresentation): - """ - ARIA NOTE: The specification does not properly explain this type, however it is implied by - examples. - """ - - @field_validator(data_type_validator('entry schema data type')) - @primitive_field(str, required=True) - def type(self): - """ - :type: :obj:`basestring` - """ - - @object_field(Description) - def description(self): - """ - :type: :class:`Description` - """ - - @object_list_field(ConstraintClause) - def constraints(self): - """ - :type: list of (str, :class:`ConstraintClause`) - """ - - @cachedmethod - def _get_type(self, context): - return get_data_type(context, self, 'type') - - @cachedmethod - def _get_constraints(self, context): - return get_property_constraints(context, self) - -@short_form_field('primary') -@has_fields -class OperationImplementation(ExtensiblePresentation): - """ - Operation implementation. - """ - - @primitive_field(str) - def primary(self): - """ - The optional implementation artifact name (i.e., the primary script file name within a - TOSCA CSAR file). - - :type: :obj:`basestring` - """ - - @primitive_list_field(str) - def dependencies(self): - """ - The optional ordered list of one or more dependent or secondary implementation artifact name - which are referenced by the primary implementation artifact (e.g., a library the script - installs or a secondary script). - - :type: [:obj:`basestring`] - """ - -class SubstitutionMappingsRequirement(AsIsPresentation): - """ - Substitution mapping for requirement. - """ - - @property - @cachedmethod - def node_template(self): - return str(self._raw[0]) - - @property - @cachedmethod - def requirement(self): - return str(self._raw[1]) - - def _validate(self, context): - super(SubstitutionMappingsRequirement, self)._validate(context) - validate_subtitution_mappings_requirement(context, self) - -class SubstitutionMappingsCapability(AsIsPresentation): - """ - Substitution mapping for capability. - """ - - @property - @cachedmethod - def node_template(self): - return str(self._raw[0]) - - @property - @cachedmethod - def capability(self): - return str(self._raw[1]) - - def _validate(self, context): - super(SubstitutionMappingsCapability, self)._validate(context) - validate_subtitution_mappings_capability(context, self) - -@has_fields -@implements_specification('2.10', 'tosca-simple-1.0') -class SubstitutionMappings(ExtensiblePresentation): - """ - Substitution mappings. - """ - - @field_validator(type_validator('node type', convert_shorthand_to_full_type_name, 'node_types')) - @primitive_field(str, required=True) - def node_type(self): - """ - :type: :obj:`basestring` - """ - - @object_dict_field(SubstitutionMappingsRequirement) - def requirements(self): - """ - :type: {:obj:`basestring`: :class:`SubstitutionMappingsRequirement`} - """ - - @object_dict_field(SubstitutionMappingsCapability) - def capabilities(self): - """ - :type: {:obj:`basestring`: :class:`SubstitutionMappingsCapability`} - """ - - @cachedmethod - def _get_type(self, context): - return get_type_by_full_or_shorthand_name(context, self.node_type, 'node_types') - - def _validate(self, context): - super(SubstitutionMappings, self)._validate(context) - self._get_type(context) - - def _dump(self, context): - self._dump_content(context, ( - 'node_type', - 'requirements', - 'capabilities')) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca-website/blob/23d6ba76/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/artifacts.py ---------------------------------------------------------------------- diff --git a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/artifacts.py b/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/artifacts.py deleted file mode 100644 index dd9eeb4..0000000 --- a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/artifacts.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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 aria.utils.collections import OrderedDict - - -# -# NodeType, NodeTemplate -# - -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 http://git-wip-us.apache.org/repos/asf/incubator-ariatosca-website/blob/23d6ba76/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/capabilities.py ---------------------------------------------------------------------- diff --git a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/capabilities.py b/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/capabilities.py deleted file mode 100644 index d75e723..0000000 --- a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/capabilities.py +++ /dev/null @@ -1,186 +0,0 @@ -# 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 aria.utils.collections import deepcopy_with_locators, OrderedDict -from aria.parser.validation import Issue - -from .parameters import (convert_parameter_definitions_to_values, merge_raw_parameter_definitions, - get_assigned_and_defined_parameter_values) - - -# -# CapabilityType -# - -def get_inherited_valid_source_types(context, presentation): - """ - If we haven't set the ``valid_source_types`` fields, uses that value from our parent, if we have - one (recursively). - """ - - valid_source_types = presentation.valid_source_types - - if valid_source_types is None: - parent = presentation._get_parent(context) - valid_source_types = get_inherited_valid_source_types(context, parent) \ - if parent is not None else None - - return valid_source_types - - -# -# NodeType -# - -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 - - -# -# NodeTemplate -# - -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 - else: - context.validation.report( - 'capability "%s" not declared at node type "%s" in "%s"' - % (capability_name, presentation.type, presentation._fullname), - locator=our_capability_assignment._locator, level=Issue.BETWEEN_TYPES) - - return capability_assignments - - -# -# Utils -# - -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) - - -def merge_capability_definition_from_type(context, presentation, capability_definition): - raw_properties = OrderedDict() - - # Merge properties from type - 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 - 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) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca-website/blob/23d6ba76/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/constraints.py ---------------------------------------------------------------------- diff --git a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/constraints.py b/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/constraints.py deleted file mode 100644 index 9a30cc1..0000000 --- a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/constraints.py +++ /dev/null @@ -1,144 +0,0 @@ -# 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. - -import re - -from aria.modeling.constraints import NodeTemplateConstraint -from aria.modeling.utils import NodeTemplateContainerHolder -from aria.modeling.functions import evaluate -from aria.parser import implements_specification - - -@implements_specification('3.5.2-2', 'tosca-simple-1.0') -class EvaluatingNodeTemplateConstraint(NodeTemplateConstraint): - """ - A version of :class:`NodeTemplateConstraint` with boilerplate initialization for TOSCA - constraints. - """ - - def __init__(self, property_name, capability_name, constraint, as_list=False): - self.property_name = property_name - self.capability_name = capability_name - self.constraint = constraint - self.as_list = as_list - - def matches(self, source_node_template, target_node_template): - # TOSCA node template constraints can refer to either capability properties or node - # template properties - if self.capability_name is not None: - # Capability property - capability = target_node_template.capability_templates.get(self.capability_name) - value = capability.properties.get(self.property_name) \ - if capability is not None else None # Parameter - else: - # Node template property - value = target_node_template.properties.get(self.property_name) # Parameter - - value = value.value if value is not None else None - - container_holder = NodeTemplateContainerHolder(source_node_template) - - if self.as_list: - constraints = [] - for constraint in self.constraint: - evaluation = evaluate(constraint, container_holder) - if evaluation is not None: - constraints.append(evaluation.value) - else: - constraints.append(constraint) - constraint = constraints - else: - evaluation = evaluate(self.constraint, container_holder) - if evaluation is not None: - constraint = evaluation.value - else: - constraint = self.constraint - - return self.matches_evaluated(value, constraint) - - def matches_evaluated(self, value, constraint): - raise NotImplementedError - - -class Equal(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - return value == constraint - - -class GreaterThan(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - return value > constraint - - -class GreaterOrEqual(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - return value >= constraint - - -class LessThan(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - return value < constraint - - -class LessOrEqual(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - return value <= constraint - - -class InRange(EvaluatingNodeTemplateConstraint): - def __init__(self, property_name, capability_name, constraint): - super(InRange, self).__init__(property_name, capability_name, constraint, as_list=True) - - def matches_evaluated(self, value, constraints): - lower, upper = constraints - if value < lower: - return False - if (upper != 'UNBOUNDED') and (value > upper): - return False - return True - - -class ValidValues(EvaluatingNodeTemplateConstraint): - def __init__(self, property_name, capability_name, constraint): - super(ValidValues, self).__init__(property_name, capability_name, constraint, as_list=True) - - def matches_evaluated(self, value, constraints): - return value in constraints - - -class Length(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - return len(value) == constraint - - -class MinLength(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - return len(value) >= constraint - - -class MaxLength(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - return len(value) <= constraint - - -class Pattern(EvaluatingNodeTemplateConstraint): - def matches_evaluated(self, value, constraint): - # From TOSCA 1.0 3.5.2.1: - # - # "Note: Future drafts of this specification will detail the use of regular expressions and - # reference an appropriate standardized grammar." - # - # So we will just use Python's. - return re.match(constraint, unicode(value)) is not None http://git-wip-us.apache.org/repos/asf/incubator-ariatosca-website/blob/23d6ba76/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/copy.py ---------------------------------------------------------------------- diff --git a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/copy.py b/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/copy.py deleted file mode 100644 index bd9037f..0000000 --- a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/copy.py +++ /dev/null @@ -1,32 +0,0 @@ -# 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. - -# -# NodeTemplate, RelationshipTemplate -# - -def get_default_raw_from_copy(presentation, field_name): - """ - Used for the ``_get_default_raw`` field hook. - """ - - copy = presentation._raw.get('copy') - if copy is not None: - templates = getattr(presentation._container, field_name) - if templates is not None: - template = templates.get(copy) - if template is not None: - return template._raw - return None http://git-wip-us.apache.org/repos/asf/incubator-ariatosca-website/blob/23d6ba76/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/data_types.py ---------------------------------------------------------------------- diff --git a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/data_types.py b/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/data_types.py deleted file mode 100644 index ba94c70..0000000 --- a/apache-ariatosca-0.1.1/extensions/aria_extension_tosca/simple_v1_0/modeling/data_types.py +++ /dev/null @@ -1,521 +0,0 @@ -# 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. - -import re - -from aria.utils.collections import OrderedDict -from aria.utils.formatting import safe_repr -from aria.utils.type import full_type_name -from aria.utils.imports import import_fullname -from aria.parser import implements_specification -from aria.parser.presentation import (get_locator, validate_primitive) -from aria.parser.validation import Issue - -from .functions import get_function -from ..presentation.types import get_type_by_full_or_shorthand_name - - -# -# DataType -# - -def get_inherited_constraints(context, presentation): - """ - If we don't have constraints, will return our parent's constraints (if we have one), - recursively. - - Implication: if we define even one constraint, the parent's constraints will not be inherited. - """ - - constraints = presentation.constraints - - if constraints is None: - # If we don't have any, use our parent's - parent = presentation._get_parent(context) - parent_constraints = get_inherited_constraints(context, parent) \ - if parent is not None else None - if parent_constraints is not None: - constraints = parent_constraints - - return constraints - - -def coerce_data_type_value(context, presentation, data_type, entry_schema, constraints, value, # pylint: disable=unused-argument - aspect): - """ - Handles the ``_coerce_data()`` hook for complex data types. - - There are two kinds of handling: - - 1. If we have a primitive type as our great ancestor, then we do primitive type coersion, and - just check for constraints. - - 2. Otherwise, for normal complex data types we return 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. We also make sure that required definitions indeed end - up with a value. - """ - - primitive_type = data_type._get_primitive_ancestor(context) - if primitive_type is not None: - # Must be coercible to primitive ancestor - value = coerce_to_primitive(context, presentation, primitive_type, constraints, value, - aspect) - else: - definitions = data_type._get_properties(context) - if isinstance(value, dict): - temp = OrderedDict() - - # Fill in our values, but make sure they are defined - for name, v in value.iteritems(): - if name in definitions: - definition = definitions[name] - definition_type = definition._get_type(context) - definition_entry_schema = definition.entry_schema - definition_constraints = definition._get_constraints(context) - temp[name] = coerce_value(context, presentation, definition_type, - definition_entry_schema, definition_constraints, v, - aspect) - else: - context.validation.report( - 'assignment to undefined property "%s" in type "%s" in "%s"' - % (name, data_type._fullname, presentation._fullname), - locator=get_locator(v, value, presentation), level=Issue.BETWEEN_TYPES) - - # Fill in defaults from the definitions, and check if required definitions have not been - # assigned - for name, definition in definitions.iteritems(): - if (temp.get(name) is None) and hasattr(definition, 'default') \ - and (definition.default is not None): - definition_type = definition._get_type(context) - definition_entry_schema = definition.entry_schema - definition_constraints = definition._get_constraints(context) - temp[name] = coerce_value(context, presentation, definition_type, - definition_entry_schema, definition_constraints, - definition.default, 'default') - - if getattr(definition, 'required', False) and (temp.get(name) is None): - context.validation.report( - 'required property "%s" in type "%s" is not assigned a value in "%s"' - % (name, data_type._fullname, presentation._fullname), - locator=presentation._get_child_locator('definitions'), - level=Issue.BETWEEN_TYPES) - - value = temp - elif value is not None: - context.validation.report('value of type "%s" is not a dict in "%s"' - % (data_type._fullname, presentation._fullname), - locator=get_locator(value, presentation), - level=Issue.BETWEEN_TYPES) - value = None - - return value - - -def validate_data_type_name(context, presentation): - """ - Makes sure the complex data type's name is not that of a built-in type. - """ - - name = presentation._name - if get_primitive_data_type(name) is not None: - context.validation.report('data type name is that of a built-in type: %s' - % safe_repr(name), - locator=presentation._locator, level=Issue.BETWEEN_TYPES) - - -# -# PropertyDefinition, AttributeDefinition, EntrySchema, DataType -# - -def get_data_type(context, presentation, field_name, allow_none=False): - """ - Returns the type, whether it's a complex data type (a DataType instance) or a primitive (a - Python primitive type class). - - If the type is not specified, defaults to :class:`str`, per note in section 3.2.1.1 of the - `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #_Toc379455072>`__ - """ - - type_name = getattr(presentation, field_name) - - if type_name is None: - if allow_none: - return None - else: - return str - - # Make sure not derived from self - if type_name == presentation._name: - return None - - # Avoid circular definitions - container_data_type = get_container_data_type(presentation) - if (container_data_type is not None) and (container_data_type._name == type_name): - return None - - # Try complex data type - data_type = get_type_by_full_or_shorthand_name(context, type_name, 'data_types') - if data_type is not None: - return data_type - - # Try primitive data type - return get_primitive_data_type(type_name) - - -# -# PropertyDefinition, EntrySchema -# - -def get_property_constraints(context, presentation): - """ - If we don't have constraints, will return our type's constraints (if we have one), recursively. - - Implication: if we define even one constraint, the type's constraints will not be inherited. - """ - - constraints = presentation.constraints - - if constraints is None: - # If we don't have any, use our type's - the_type = presentation._get_type(context) - type_constraints = the_type._get_constraints(context) \ - if hasattr(the_type, '_get_constraints') else None - if type_constraints is not None: - constraints = type_constraints - - return constraints - - -# -# ConstraintClause -# - -def apply_constraint_to_value(context, presentation, constraint_clause, value): # pylint: disable=too-many-statements,too-many-return-statements,too-many-branches - """ - Returns false if the value does not conform to the constraint. - """ - - constraint_key = constraint_clause._raw.keys()[0] - the_type = constraint_clause._get_type(context) - # PropertyAssignment does not have this: - entry_schema = getattr(presentation, 'entry_schema', None) - - def coerce_constraint(constraint): - return coerce_value(context, presentation, the_type, entry_schema, None, constraint, - constraint_key) - - def report(message, constraint): - context.validation.report('value %s %s per constraint in "%s": %s' - % (message, safe_repr(constraint), - presentation._name or presentation._container._name, - safe_repr(value)), - locator=presentation._locator, level=Issue.BETWEEN_FIELDS) - - if constraint_key == 'equal': - constraint = coerce_constraint(constraint_clause.equal) - if value != constraint: - report('is not equal to', constraint) - return False - - elif constraint_key == 'greater_than': - constraint = coerce_constraint(constraint_clause.greater_than) - if value <= constraint: - report('is not greater than', constraint) - return False - - elif constraint_key == 'greater_or_equal': - constraint = coerce_constraint(constraint_clause.greater_or_equal) - if value < constraint: - report('is not greater than or equal to', constraint) - return False - - elif constraint_key == 'less_than': - constraint = coerce_constraint(constraint_clause.less_than) - if value >= constraint: - report('is not less than', constraint) - return False - - elif constraint_key == 'less_or_equal': - constraint = coerce_constraint(constraint_clause.less_or_equal) - if value > constraint: - report('is not less than or equal to', constraint) - return False - - elif constraint_key == 'in_range': - lower, upper = constraint_clause.in_range - lower, upper = coerce_constraint(lower), coerce_constraint(upper) - if value < lower: - report('is not greater than or equal to lower bound', lower) - return False - if (upper != 'UNBOUNDED') and (value > upper): - report('is not lesser than or equal to upper bound', upper) - return False - - elif constraint_key == 'valid_values': - constraint = tuple(coerce_constraint(v) for v in constraint_clause.valid_values) - if value not in constraint: - report('is not one of', constraint) - return False - - elif constraint_key == 'length': - constraint = constraint_clause.length - try: - if len(value) != constraint: - report('is not of length', constraint) - return False - except TypeError: - pass # should be validated elsewhere - - elif constraint_key == 'min_length': - constraint = constraint_clause.min_length - try: - if len(value) < constraint: - report('has a length lesser than', constraint) - return False - except TypeError: - pass # should be validated elsewhere - - elif constraint_key == 'max_length': - constraint = constraint_clause.max_length - try: - if len(value) > constraint: - report('has a length greater than', constraint) - return False - except TypeError: - pass # should be validated elsewhere - - elif constraint_key == 'pattern': - constraint = constraint_clause.pattern - try: - # From TOSCA 1.0 3.5.2.1: - # - # "Note: Future drafts of this specification will detail the use of regular expressions - # and reference an appropriate standardized grammar." - # - # So we will just use Python's. - if re.match(constraint, str(value)) is None: - report('does not match regular expression', constraint) - return False - except re.error: - pass # should be validated elsewhere - - return True - - -# -# Repository -# - -def get_data_type_value(context, presentation, field_name, type_name): - the_type = get_type_by_full_or_shorthand_name(context, type_name, 'data_types') - if the_type is not None: - value = getattr(presentation, field_name) - if value is not None: - return coerce_data_type_value(context, presentation, the_type, None, None, value, None) - else: - context.validation.report('field "%s" in "%s" refers to unknown data type "%s"' - % (field_name, presentation._fullname, type_name), - locator=presentation._locator, level=Issue.BETWEEN_TYPES) - return None - - -# -# Utils -# - -PRIMITIVE_DATA_TYPES = { - # YAML 1.2: - 'tag:yaml.org,2002:str': unicode, - 'tag:yaml.org,2002:integer': int, - 'tag:yaml.org,2002:float': float, - 'tag:yaml.org,2002:bool': bool, - 'tag:yaml.org,2002:null': None.__class__, - - # TOSCA aliases: - 'string': unicode, - 'integer': int, - 'float': float, - 'boolean': bool, - 'null': None.__class__} - - -@implements_specification('3.2.1-3', 'tosca-simple-1.0') -def get_primitive_data_type(type_name): - """ - Many of the types we use in this profile are built-in types from the YAML 1.2 specification - (i.e., those identified by the "tag:yaml.org,2002" version tag) [YAML-1.2]. - - See the `TOSCA Simple Profile v1.0 cos01 specification <http://docs.oasis-open.org/tosca - /TOSCA-Simple-Profile-YAML/v1.0/cos01/TOSCA-Simple-Profile-YAML-v1.0-cos01.html - #_Toc373867862>`__ - """ - - return PRIMITIVE_DATA_TYPES.get(type_name) - - -def get_data_type_name(the_type): - """ - Returns the name of the type, whether it's a DataType, a primitive type, or another class. - """ - - return the_type._name if hasattr(the_type, '_name') else full_type_name(the_type) - - -def coerce_value(context, presentation, the_type, entry_schema, constraints, value, aspect=None): # pylint: disable=too-many-return-statements - """ - Returns the value after it's coerced to its type, reporting validation errors if it cannot be - coerced. - - Supports both complex data types and primitives. - - Data types can use the ``coerce_value`` extension to hook their own specialized function. - If the extension is present, we will delegate to that hook. - """ - - # TODO: should support models as well as presentations - - is_function, func = get_function(context, presentation, value) - if is_function: - return func - - if the_type is None: - return value - - if the_type == None.__class__: - if value is not None: - context.validation.report('field "%s" is of type "null" but has a non-null value: %s' - % (presentation._name, safe_repr(value)), - locator=presentation._locator, level=Issue.BETWEEN_FIELDS) - return None - - # Delegate to 'coerce_value' extension - if hasattr(the_type, '_get_extension'): - coerce_value_fn_name = the_type._get_extension('coerce_value') - if coerce_value_fn_name is not None: - if value is None: - return None - coerce_value_fn = import_fullname(coerce_value_fn_name) - return coerce_value_fn(context, presentation, the_type, entry_schema, constraints, - value, aspect) - - if hasattr(the_type, '_coerce_value'): - # Delegate to '_coerce_value' (likely a DataType instance) - return the_type._coerce_value(context, presentation, entry_schema, constraints, value, - aspect) - - # Coerce to primitive type - return coerce_to_primitive(context, presentation, the_type, constraints, value, aspect) - - -def coerce_to_primitive(context, presentation, primitive_type, constraints, value, aspect=None): - """ - Returns the value after it's coerced to a primitive type, translating exceptions to validation - errors if it cannot be coerced. - """ - - if value is None: - return None - - try: - # Coerce - value = validate_primitive(value, primitive_type, - context.validation.allow_primitive_coersion) - - # Check constraints - apply_constraints_to_value(context, presentation, constraints, value) - except ValueError as e: - report_issue_for_bad_format(context, presentation, primitive_type, value, aspect, e) - value = None - except TypeError as e: - report_issue_for_bad_format(context, presentation, primitive_type, value, aspect, e) - value = None - - return value - - -def coerce_to_data_type_class(context, presentation, cls, entry_schema, constraints, value, - aspect=None): - """ - Returns the value after it's coerced to a data type class, reporting validation errors if it - cannot be coerced. Constraints will be applied after coersion. - - Will either call a ``_create`` static function in the class, or instantiate it using a - constructor if ``_create`` is not available. - - This will usually be called by a ``coerce_value`` extension hook in a :class:`DataType`. - """ - - try: - if hasattr(cls, '_create'): - # Instantiate using creator function - value = cls._create(context, presentation, entry_schema, constraints, value, aspect) - else: - # Normal instantiation - value = cls(entry_schema, constraints, value, aspect) - except ValueError as e: - report_issue_for_bad_format(context, presentation, cls, value, aspect, e) - value = None - - # Check constraints - value = apply_constraints_to_value(context, presentation, constraints, value) - - return value - - -def apply_constraints_to_value(context, presentation, constraints, value): - """ - Applies all constraints to the value. If the value conforms, returns the value. If it does not - conform, returns None. - """ - - if (value is not None) and (constraints is not None): - valid = True - for constraint in constraints: - if not constraint._apply_to_value(context, presentation, value): - valid = False - if not valid: - value = None - return value - - -def get_container_data_type(presentation): - if presentation is None: - return None - if type(presentation).__name__ == 'DataType': - return presentation - return get_container_data_type(presentation._container) - - -def report_issue_for_bad_format(context, presentation, the_type, value, aspect, e): - if aspect == 'default': - aspect = '"default" value' - elif aspect is not None: - aspect = '"%s" aspect' % aspect - - if aspect is not None: - context.validation.report('%s for field "%s" is not a valid "%s": %s' - % (aspect, presentation._name or presentation._container._name, - get_data_type_name(the_type), safe_repr(value)), - locator=presentation._locator, level=Issue.BETWEEN_FIELDS, - exception=e) - else: - context.validation.report('field "%s" is not a valid "%s": %s' - % (presentation._name or presentation._container._name, - get_data_type_name(the_type), safe_repr(value)), - locator=presentation._locator, level=Issue.BETWEEN_FIELDS, - exception=e)
