示例#1
0
 def class_(value, name, default_name=None, version_spec=None):
     object_store = None if this is None else this.object_store
     if not default_name:
         default_name = name
     murano_class = name.type
     if value is None:
         return None
     if isinstance(value, dsl_types.MuranoObject):
         obj = value
     elif isinstance(value, dsl_types.MuranoObjectInterface):
         obj = value.object
     elif isinstance(value, utils.MappingType):
         obj = helpers.instantiate(
             value, owner, object_store, root_context,
             calling_type, default_name)
     elif isinstance(value, six.string_types) and object_store:
         obj = object_store.get(value)
         if obj is None:
             if not object_store.initializing:
                 raise exceptions.NoObjectFoundError(value)
             else:
                 return TypeScheme.ObjRef(value)
     else:
         raise exceptions.ContractViolationException(
             'Value {0} cannot be represented as class {1}'.format(
                 format_scalar(value), name))
     if not helpers.is_instance_of(
             obj, murano_class.name,
             version_spec or helpers.get_type(root_context)):
         raise exceptions.ContractViolationException(
             'Object of type {0} is not compatible with '
             'requested type {1}'.format(obj.type.name, name))
     return obj
示例#2
0
        def template(engine,
                     value,
                     type_,
                     exclude_properties=None,
                     default_type=None,
                     version_spec=None):
            object_store = helpers.get_object_store()
            passkey = None
            if not default_type:
                default_type = type_
            murano_class = type_.type
            if value is None:
                return None
            if isinstance(value, dsl_types.MuranoObject):
                obj = value
            elif isinstance(value, dsl_types.MuranoObjectInterface):
                obj = value.object
            elif isinstance(value, utils.MappingType):
                passkey = utils.create_marker('<Contract Passkey>')
                if exclude_properties:
                    parsed = helpers.parse_object_definition(
                        value, calling_type, context)
                    props = dsl.to_mutable(parsed['properties'], engine)
                    for p in exclude_properties:
                        helpers.patch_dict(props, p, passkey)
                    parsed['properties'] = props
                    value = helpers.assemble_object_definition(parsed)
                with helpers.thread_local_attribute(
                        constants.TL_CONTRACT_PASSKEY, passkey):
                    with helpers.thread_local_attribute(
                            constants.TL_OBJECTS_DRY_RUN, True):
                        obj = object_store.load(value,
                                                owner,
                                                context=context,
                                                default_type=default_type,
                                                scope_type=calling_type)
            else:
                raise exceptions.ContractViolationException(
                    'Value {0} cannot be represented as class {1}'.format(
                        format_scalar(value), type_))
            if not helpers.is_instance_of(
                    obj, murano_class.name, version_spec
                    or helpers.get_type(root_context)):
                raise exceptions.ContractViolationException(
                    'Object of type {0} is not compatible with '
                    'requested type {1}'.format(obj.type.name, type_))

            with helpers.thread_local_attribute(constants.TL_CONTRACT_PASSKEY,
                                                passkey):
                result = serializer.serialize(obj.real_this,
                                              object_store.executor,
                                              dsl_types.DumpTypes.Mixed)
                if exclude_properties:
                    for p in exclude_properties:
                        helpers.patch_dict(result, p, utils.NO_VALUE)
                return result
示例#3
0
 def validate(self):
     if self.value is None or isinstance(self.value, contracts.ObjRef):
         return self.value
     if isinstance(self.value, dsl_types.MuranoObject):
         if helpers.find_object_owner(self.value, lambda t: t is self.this):
             return self.value
         raise exceptions.ContractViolationException(
             'Object {0} violates owned() contract'.format(self.value))
     raise exceptions.ContractViolationException(
         'Value {0} is not an object'.format(self.value))
示例#4
0
 def validate(self):
     if self.value is None or helpers.is_instance_of(
             self.value, self.type.name, self.version_spec
             or helpers.get_names_scope(self.root_context)):
         return self.value
     if not isinstance(
             self.value,
         (dsl_types.MuranoObject, dsl_types.MuranoObjectInterface)):
         raise exceptions.ContractViolationException(
             'Value is not an object')
     raise exceptions.ContractViolationException(
         'Object of type {0} is not compatible with '
         'requested type {1}'.format(self.value.type, self.type))
示例#5
0
    def _map_list(self, data, spec, context):
        if not isinstance(data, types.ListType):
            if data is None or data is NoValue:
                data = []
            else:
                data = [data]
        if len(spec) < 1:
            return data
        result = []
        shift = 0
        max_length = sys.maxint
        min_length = 0
        if isinstance(spec[-1], types.IntType):
            min_length = spec[-1]
            shift += 1
        if len(spec) >= 2 and isinstance(spec[-2], types.IntType):
            max_length = min_length
            min_length = spec[-2]
            shift += 1

        if not min_length <= len(data) <= max_length:
            raise exceptions.ContractViolationException(
                'Array length {0} is not within [{1}..{2}] range'.format(
                    len(data), min_length, max_length))

        for index, item in enumerate(data):
            spec_item = spec[-1 - shift] \
                if index >= len(spec) - shift else spec[index]
            result.append(self._map(item, spec_item, context))
        return result
示例#6
0
 def check(value, predicate):
     if isinstance(value, TypeScheme.ObjRef) or predicate(
             root_context.create_child_context(), value):
         return value
     else:
         raise exceptions.ContractViolationException(
             "Value {0} doesn't match predicate".format(value))
示例#7
0
    def _map_list(self, data, spec, context, path):
        if not utils.is_sequence(data):
            if data is None or data is dsl.NO_VALUE:
                data = []
            else:
                data = [data]
        if len(spec) < 1:
            return data
        shift = 0
        max_length = sys.maxint
        min_length = 0
        if isinstance(spec[-1], types.IntType):
            min_length = spec[-1]
            shift += 1
        if len(spec) >= 2 and isinstance(spec[-2], types.IntType):
            max_length = min_length
            min_length = spec[-2]
            shift += 1

        if not min_length <= len(data) <= max_length:
            raise exceptions.ContractViolationException(
                'Array length {0} is not within [{1}..{2}] range'.format(
                    len(data), min_length, max_length))

        def map_func():
            for index, item in enumerate(data):
                spec_item = (spec[-1 - shift]
                             if index >= len(spec) - shift else spec[index])
                yield self._map(item, spec_item, context,
                                '{0}[{1}]'.format(path, index))

        return tuple(map_func())
示例#8
0
 def _map_scalar(self, data, spec):
     if data != spec:
         raise exceptions.ContractViolationException(
             'Value {0} is not equal to {1}'.format(
                 helpers.format_scalar(data), spec))
     else:
         return data
示例#9
0
 def transform(self):
     value = self.value
     object_store = helpers.get_object_store()
     if isinstance(self.value, contracts.ObjRef):
         value = self.value.object_id
     if value is None:
         return None
     if isinstance(value, dsl_types.MuranoObject):
         obj = value
     elif isinstance(value, dsl_types.MuranoObjectInterface):
         obj = value.object
     elif isinstance(value, utils.MappingType):
         obj = object_store.load(value,
                                 self.owner,
                                 context=self.root_context,
                                 default_type=self.default_type,
                                 scope_type=self.calling_type)
     elif isinstance(value, str):
         obj = object_store.get(value)
         if obj is None:
             if not object_store.initializing:
                 raise exceptions.NoObjectFoundError(value)
             else:
                 return contracts.ObjRef(value)
     else:
         raise exceptions.ContractViolationException(
             'Value {0} cannot be represented as class {1}'.format(
                 helpers.format_scalar(value), self.type))
     self.value = obj
     return self.validate()
示例#10
0
    def _map_dict(self, data, spec, context):
        if data is None or data is dsl.NO_VALUE:
            data = {}
        if not isinstance(data, utils.MappingType):
            raise exceptions.ContractViolationException(
                'Supplied is not of a dictionary type')
        if not spec:
            return data
        result = {}
        yaql_key = None
        for key, value in spec.iteritems():
            if isinstance(key, dsl_types.YaqlExpression):
                if yaql_key is not None:
                    raise exceptions.DslContractSyntaxError(
                        'Dictionary contract '
                        'cannot have more than one expression keys')
                else:
                    yaql_key = key
            else:
                result[key] = self._map(data.get(key), value, context)

        if yaql_key is not None:
            yaql_value = spec[yaql_key]
            for key, value in data.iteritems():
                if key in result:
                    continue
                result[self._map(key, yaql_key,
                                 context)] = self._map(value, yaql_value,
                                                       context)

        return utils.FrozenDict(result)
示例#11
0
    def _map_dict(self, data, spec, context, path):
        if data is None or data is dsl.NO_VALUE:
            data = {}
        if not isinstance(data, utils.MappingType):
            raise exceptions.ContractViolationException(
                'Value {0} is not of a dictionary type'.format(
                    helpers.format_scalar(data)))
        if not spec:
            return data
        result = {}
        yaql_key = None
        for key, value in six.iteritems(spec):
            if isinstance(key, dsl_types.YaqlExpression):
                if yaql_key is not None:
                    raise exceptions.DslContractSyntaxError(
                        'Dictionary contract '
                        'cannot have more than one expression key')
                else:
                    yaql_key = key
            else:
                result[key] = self._map(
                    data.get(key), value, context,
                    '{0}[{1}]'.format(path, helpers.format_scalar(key)))

        if yaql_key is not None:
            yaql_value = spec[yaql_key]
            for key, value in six.iteritems(data):
                if key in result:
                    continue
                key = self._map(key, yaql_key, context, path)
                result[key] = self._map(
                    value, yaql_value, context,
                    '{0}[{1}]'.format(path, helpers.format_scalar(key)))

        return utils.FrozenDict(result)
示例#12
0
 def _check(value, predicate):
     value = value()
     if isinstance(value, TypeScheme.ObjRef) or predicate(value):
         return value
     else:
         raise exceptions.ContractViolationException(
             "Value {0} doesn't match predicate".format(value))
示例#13
0
        def _class2(value, name, default_name):
            name = namespace_resolver.resolve_name(name)
            if not default_name:
                default_name = name
            else:
                default_name = namespace_resolver.resolve_name(default_name)
            value = value()
            class_loader = murano.dsl.helpers.get_class_loader(root_context)
            murano_class = class_loader.get_class(name)
            if not murano_class:
                raise exceptions.NoClassFound(
                    'Class {0} cannot be found'.format(name))
            if value is None:
                return None
            if isinstance(value, murano.dsl.murano_object.MuranoObject):
                obj = value
            elif isinstance(value, types.DictionaryType):
                if '?' not in value:
                    new_value = {
                        '?': {
                            'id': uuid.uuid4().hex,
                            'type': default_name
                        }
                    }
                    new_value.update(value)
                    value = new_value

                obj = object_store.load(value,
                                        owner,
                                        root_context,
                                        defaults=default)
            elif isinstance(value, types.StringTypes):
                obj = object_store.get(value)
                if obj is None:
                    if not object_store.initializing:
                        raise exceptions.NoObjectFoundError(value)
                    else:
                        return TypeScheme.ObjRef(value)
            else:
                raise exceptions.ContractViolationException(
                    'Value {0} cannot be represented as class {1}'.format(
                        value, name))
            if not murano_class.is_compatible(obj):
                raise exceptions.ContractViolationException(
                    'Object of type {0} is not compatible with '
                    'requested type {1}'.format(obj.type.name, name))
            return obj
示例#14
0
 def not_owned(obj):
     try:
         owned(obj)
     except exceptions.ContractViolationException:
         return obj
     else:
         raise exceptions.ContractViolationException(
             'Object {0} violates notOwned() contract'.format(obj))
示例#15
0
        def class_(value, name, default_name=None, version_spec=None):
            object_store = this.object_store
            if not default_name:
                default_name = name
            murano_class = name.murano_class
            if value is None:
                return None
            if isinstance(value, dsl_types.MuranoObject):
                obj = value
            elif isinstance(value, dsl_types.MuranoObjectInterface):
                obj = value.object
            elif isinstance(value, utils.MappingType):
                if '?' not in value:
                    new_value = {
                        '?': {
                            'id': uuid.uuid4().hex,
                            'type': default_name.murano_class.name,
                            'classVersion':
                            str(default_name.murano_class.version)
                        }
                    }
                    new_value.update(value)
                    value = new_value

                obj = object_store.load(value,
                                        owner,
                                        root_context,
                                        defaults=default)
            elif isinstance(value, types.StringTypes):
                obj = object_store.get(value)
                if obj is None:
                    if not object_store.initializing:
                        raise exceptions.NoObjectFoundError(value)
                    else:
                        return TypeScheme.ObjRef(value)
            else:
                raise exceptions.ContractViolationException(
                    'Value {0} cannot be represented as class {1}'.format(
                        format_scalar(value), name))
            if not helpers.is_instance_of(
                    obj, murano_class.name, version_spec
                    or helpers.get_type(root_context)):
                raise exceptions.ContractViolationException(
                    'Object of type {0} is not compatible with '
                    'requested type {1}'.format(obj.type.name, name))
            return obj
示例#16
0
        def not_null(value):
            if isinstance(value, TypeScheme.ObjRef):
                return value

            if value is None:
                raise exceptions.ContractViolationException(
                    'null value violates notNull() contract')
            return value
示例#17
0
        def owned(obj):
            p = obj.owner
            while p is not None:
                if p is this:
                    return obj
                p = p.owner

            raise exceptions.ContractViolationException(
                'Object {0} violates owned() contract'.format(obj))
示例#18
0
 def transform(self, *args, **kwargs):
     try:
         return super(MuranoProperty, self).transform(*args, **kwargs)
     except exceptions.ContractViolationException as e:
         msg = u'[{0}.{1}{2}] {3}'.format(self.declaring_type.name,
                                          self.name, e.path, str(e))
         utils.reraise(exceptions.ContractViolationException,
                       exceptions.ContractViolationException(msg),
                       sys.exc_info()[2])
示例#19
0
 def transform(self):
     if self.value is None:
         return None
     try:
         return int(self.value)
     except Exception:
         raise exceptions.ContractViolationException(
             'Value {0} violates int() contract'.format(
                 helpers.format_scalar(self.value)))
示例#20
0
 def check(value, predicate, msg=None):
     if isinstance(value, TypeScheme.ObjRef) or predicate(
             root_context.create_child_context(), value):
         return value
     else:
         if not msg:
             msg = "Value {0} doesn't match predicate".format(
                 format_scalar(value))
         raise exceptions.ContractViolationException(msg)
示例#21
0
 def int_(value):
     if value is dsl.NO_VALUE:
         value = default
     if value is None:
         return None
     try:
         return int(value)
     except Exception:
         raise exceptions.ContractViolationException(
             'Value {0} violates int() contract'.format(value))
示例#22
0
 def string(value):
     if value is dsl.NO_VALUE:
         value = default
     if value is None:
         return None
     try:
         return unicode(value)
     except Exception:
         raise exceptions.ContractViolationException(
             'Value {0} violates string() contract'.format(value))
示例#23
0
 def validate(self):
     if isinstance(self.value, contracts.ObjRef) or self._call_predicate(
             self.value):
         return self.value
     else:
         msg = self.msg
         if not msg:
             msg = "Value {0} doesn't match predicate".format(
                 helpers.format_scalar(self.value))
         raise exceptions.ContractViolationException(msg)
示例#24
0
 def _string(value):
     value = value()
     if value is NoValue:
         value = default
     if value is None:
         return None
     try:
         return unicode(value)
     except Exception:
         raise exceptions.ContractViolationException(
             'Value {0} violates string() contract'.format(value))
示例#25
0
 def string(value):
     if value is dsl.NO_VALUE:
         value = default
     if value is None:
         return None
     try:
         return six.text_type(value)
     except Exception:
         raise exceptions.ContractViolationException(
             'Value {0} violates string() contract'.format(
                 format_scalar(value)))
示例#26
0
 def transform(self, value, this, *args, **kwargs):
     try:
         if self.murano_method.usage == dsl_types.MethodUsages.Extension:
             this = self.murano_method.declaring_type
         return super(MuranoMethodArgument,
                      self).transform(value, this, *args, **kwargs)
     except exceptions.ContractViolationException as e:
         msg = u'[{0}::{1}({2}{3})] {4}'.format(
             self.murano_method.declaring_type.name,
             self.murano_method.name, self.name, e.path, six.text_type(e))
         six.reraise(exceptions.ContractViolationException,
                     exceptions.ContractViolationException(msg),
                     sys.exc_info()[2])
示例#27
0
        def template(value,
                     type_,
                     exclude_properties=None,
                     default_type=None,
                     version_spec=None):

            if value is None or isinstance(value, utils.MappingType):
                return value

            if helpers.is_instance_of(
                    value, type_.type.name, version_spec
                    or helpers.get_names_scope(root_context)):
                return value
            raise exceptions.ContractViolationException()
示例#28
0
        def _not_owned(obj):
            if isinstance(obj, TypeScheme.ObjRef):
                return obj

            if obj is None:
                return None

            try:
                _owned(obj)
            except exceptions.ContractViolationException:
                return obj
            else:
                raise exceptions.ContractViolationException(
                    'Object {0} violates notOwned() contract'.format(
                        obj.object_id))
示例#29
0
        def _owned(obj):
            if isinstance(obj, TypeScheme.ObjRef):
                return obj

            if obj is None:
                return None

            p = obj.owner
            while p is not None:
                if p is this:
                    return obj
                p = p.owner

            raise exceptions.ContractViolationException(
                'Object {0} violates owned() contract'.format(obj.object_id))
示例#30
0
 def transform(self):
     if self.value is None:
         return None
     if isinstance(self.value, six.text_type):
         return self.value
     if isinstance(self.value, six.string_types) or \
             isinstance(self.value, six.integer_types):
         return six.text_type(self.value)
     if isinstance(self.value, dsl_types.MuranoObject):
         return self.value.object_id
     if isinstance(self.value, dsl_types.MuranoObjectInterface):
         return self.value.id
     raise exceptions.ContractViolationException(
         'Value {0} violates string() contract'.format(
             helpers.format_scalar(self.value)))