Пример #1
0
    def __init__(self, type_name=None, type_version=None, **kwargs):
        """Defines an artifact reference

        :param type_name: type name of the target artifact
        :param type_version: type version of the target artifact
        """
        super(ArtifactReference, self).__init__(**kwargs)
        if type_name is not None:
            if isinstance(type_name, list):
                type_names = list(type_name)
                if type_version is not None:
                    raise exc.InvalidArtifactTypePropertyDefinition(
                        _('Unable to specify version '
                          'if multiple types are possible'))
            else:
                type_names = [type_name]

            def validate_reference(artifact):
                if artifact.type_name not in type_names:
                    return False
                if (type_version is not None
                        and artifact.type_version != type_version):
                    return False
                return True

            self._add_validator('referenced_type', validate_reference,
                                _("Invalid referenced type"))
        elif type_version is not None:
            raise exc.InvalidArtifactTypePropertyDefinition(
                _('Unable to specify version '
                  'if type is not specified'))
        self._check_definition()
Пример #2
0
 def _check_item_type(self, item):
     if not isinstance(item, self.ALLOWED_ITEM_TYPES):
         raise exc.InvalidArtifactTypePropertyDefinition(
             _('Invalid item type specification'))
     if item.default is not None:
         raise exc.InvalidArtifactTypePropertyDefinition(
             _('List definitions may hot have defaults'))
Пример #3
0
 def _validate_default(self):
     if self.default:
         try:
             self.validate(self.default, 'default')
         except exc.InvalidArtifactPropertyValue:
             raise exc.InvalidArtifactTypePropertyDefinition(
                 _("Default value is invalid"))
Пример #4
0
    def __init__(self,
                 internal=False,
                 allowed_values=None,
                 validators=None,
                 **kwargs):
        """Defines a metadata property

        :param internal: a flag indicating that the property is internal, i.e.
        not returned to client
        :param allowed_values: specifies a list of values allowed for the
        property
        :param validators: specifies a list of custom validators for the
        property
        """
        super(PropertyDefinition, self).__init__(**kwargs)
        self.internal = internal
        self._allowed_values = None

        if validators is not None:
            try:
                for i, (f, m) in enumerate(validators):
                    self._add_validator("custom_%i" % i, f, m)
            except ValueError:
                raise exc.InvalidArtifactTypePropertyDefinition(
                    _("Custom validators list should contain tuples "
                      "'(function, message)'"))

        if allowed_values is not None:
            # copy the allowed_values, as this is going to create a
            # closure, and we need to make sure that external modification of
            # this list does not affect the created validator
            self.allowed_values(allowed_values)
        self._check_definition()
Пример #5
0
    def __init__(self,
                 item_type=String(),
                 min_size=0,
                 max_size=None,
                 unique=False,
                 extra_items=True,
                 **kwargs):
        """Defines an Array metadata property

        :param item_type: defines the types of elements in Array. If set to an
        instance of PropertyDefinition then all the elements have to be of that
        type. If set to list of such instances, then the elements on the
        corresponding positions have to be of the appropriate type.
        :param min_size: minimum size of the Array
        :param max_size: maximum size of the Array
        :param unique: if set to true, all the elements in the Array have to be
        unique
        """
        if isinstance(item_type, Array):
            msg = _("Array property can't have item_type=Array")
            raise exc.InvalidArtifactTypePropertyDefinition(msg)
        declarative.ListAttributeDefinition.__init__(self,
                                                     item_type=item_type,
                                                     min_size=min_size,
                                                     max_size=max_size,
                                                     unique=unique)
        declarative.PropertyDefinition.__init__(self, **kwargs)
Пример #6
0
 def __init__(self, internal=False, **kwargs):
     self.internal = internal
     kwargs.setdefault('mutable', False)
     # if mutable=True has been passed -> raise an exception
     if kwargs['mutable'] is True:
         raise exc.InvalidArtifactTypePropertyDefinition(
             _("Dependency relations cannot be mutable"))
     super(RelationDefinition, self).__init__(**kwargs)
Пример #7
0
 def _validate_allowed_values(self):
     if self._allowed_values:
         try:
             for allowed_value in self._allowed_values:
                 self.validate(allowed_value, 'allowed_value')
         except exc.InvalidArtifactPropertyValue:
             raise exc.InvalidArtifactTypePropertyDefinition(
                 _("Allowed values %s are invalid under given validators") %
                 self._allowed_values)
Пример #8
0
def _build_declarative_meta(cls):
    attrs = dict(cls.__dict__)
    type_name = None
    type_display_name = None
    type_version = None
    type_description = None
    endpoint = None

    for base in cls.__mro__:
        for name, value in six.iteritems(vars(base)):
            if name == '__type_name__':
                if not type_name:
                    type_name = cls.__type_name__
            elif name == '__type_version__':
                if not type_version:
                    type_version = cls.__type_version__
            elif name == '__type_description__':
                if not type_description:
                    type_description = cls.__type_description__
            elif name == '__endpoint__':
                if not endpoint:
                    endpoint = cls.__endpoint__
            elif name == '__type_display_name__':
                if not type_display_name:
                    type_display_name = cls.__type_display_name__
            elif base is not cls and name not in attrs:
                if isinstance(value, AttributeDefinition):
                    attrs[name] = value
                elif isinstance(value, ArtifactPropertyDescriptor):
                    attrs[name] = value.prop

    meta = ArtifactTypeMetadata(type_name=type_name or cls.__name__,
                                type_display_name=type_display_name,
                                type_version=type_version,
                                type_description=type_description,
                                endpoint=endpoint)
    setattr(cls, 'metadata', meta)
    for k, v in attrs.items():
        if k == 'metadata':
            raise exc.InvalidArtifactTypePropertyDefinition(
                _("Cannot declare artifact property with reserved name "
                  "'metadata'"))
        if isinstance(v, AttributeDefinition):
            v._set_name(k)
            wrapper_class = None
            if isinstance(v, ListAttributeDefinition):
                wrapper_class = type("ValidatedList", (list, ), {})
                _add_validation_to_list(wrapper_class)
            if isinstance(v, DictAttributeDefinition):
                wrapper_class = type("ValidatedDict", (dict, ), {})
                _add_validation_to_dict(wrapper_class)
            prop_descr = ArtifactPropertyDescriptor(v, wrapper_class)
            setattr(cls, k, prop_descr)
            meta.attributes.add(v)
Пример #9
0
    def __init__(self,
                 item_type,
                 min_size=0,
                 max_size=None,
                 unique=False,
                 **kwargs):

        super(ListAttributeDefinition, self).__init__(**kwargs)
        if isinstance(item_type, list):
            for it in item_type:
                self._check_item_type(it)

            # we need to copy the item_type collection
            self.item_type = item_type[:]

            if min_size != 0:
                raise exc.InvalidArtifactTypePropertyDefinition(
                    _("Cannot specify 'min_size' explicitly"))

            if max_size is not None:
                raise exc.InvalidArtifactTypePropertyDefinition(
                    _("Cannot specify 'max_size' explicitly"))

            # setting max_size and min_size to the length of item_type,
            # as tuple-semantic assumes that the number of elements is set
            # by the type spec
            min_size = max_size = len(item_type)
        else:
            self._check_item_type(item_type)
            self.item_type = item_type

        if min_size:
            self.min_size(min_size)

        if max_size:
            self.max_size(max_size)

        if unique:
            self.unique()
Пример #10
0
 def max_length(self, value):
     """Sets the maximum value length"""
     self._max_length = value
     if value is not None:
         if value > 255:
             raise exc.InvalidArtifactTypePropertyDefinition(
                 _('Max string length may not exceed 255 characters'))
         self._add_validator('max_length',
                             lambda v: len(v) <= self._max_length,
                             _('Length  is greater than maximum'))
     else:
         self._remove_validator('max_length')
     self._check_definition()
Пример #11
0
    def min_length(self, value):
        """Sets the minimum value length"""
        self._min_length = value
        if value is not None:
            if value < 0:
                raise exc.InvalidArtifactTypePropertyDefinition(
                    _('Min string length may not be negative'))

            self._add_validator('min_length',
                                lambda v: len(v) >= self._min_length,
                                _('Length is less than minimum'))
        else:
            self._remove_validator('min_length')
        self._check_definition()
Пример #12
0
 def __init__(self,
              references=ArtifactReference(),
              min_size=0,
              max_size=None,
              **kwargs):
     if isinstance(references, list):
         raise exc.InvalidArtifactTypePropertyDefinition(
             _("Invalid reference list specification"))
     declarative.RelationDefinition.__init__(self, **kwargs)
     declarative.ListAttributeDefinition.__init__(
         self,
         item_type=references,
         min_size=min_size,
         max_size=max_size,
         unique=True,
         default=[] if min_size == 0 else None)
Пример #13
0
    def __init__(self,
                 max_file_size=None,
                 min_file_size=None,
                 min_locations=None,
                 max_locations=None,
                 **kwargs):
        """Defines a binary object as part of Artifact Type
        :param max_file_size: maximum size of the associate Blob
        :param min_file_size: minimum size of the associated Blob
        :param min_locations: minimum number of locations in the associated
        Blob
        :param max_locations: maximum number of locations in the associated
        Blob
        """
        mutable = kwargs.pop('mutable', False)
        if mutable:
            raise exc.InvalidArtifactTypePropertyDefinition(
                _("BinaryObject property cannot be declared mutable"))
        super(BinaryObject, self).__init__(default=None,
                                           readonly=False,
                                           mutable=mutable,
                                           **kwargs)
        self._max_file_size = max_file_size
        self._min_file_size = min_file_size
        self._min_locations = min_locations
        self._max_locations = max_locations

        self._add_validator('size_not_empty', lambda v: v.size is not None,
                            _('Blob size is not set'))
        if max_file_size:
            self._add_validator('max_size',
                                lambda v: v.size <= self._max_file_size,
                                _("File too large"))
        if min_file_size:
            self._add_validator('min_size',
                                lambda v: v.size >= self._min_file_size,
                                _("File too small"))
        if min_locations:
            self._add_validator(
                'min_locations',
                lambda v: len(v.locations) >= self._min_locations,
                _("Too few locations"))
        if max_locations:
            self._add_validator(
                'max_locations',
                lambda v: len(v.locations) <= self._max_locations,
                _("Too many locations"))
Пример #14
0
 def _check_prop(self, key, item):
     if (not isinstance(item, self.ALLOWED_PROPERTY_TYPES) or
         (key is not None and not isinstance(key, six.string_types))):
         raise exc.InvalidArtifactTypePropertyDefinition(
             _('Invalid dict property type specification'))