def transform(self, value, this, owner, context, default=None): if default is None: default = self.default executor = helpers.get_executor(context) if isinstance(this, dsl_types.MuranoType): return self._contract.transform( value, executor.create_object_context(this), None, None, default, helpers.get_type(context)) else: return self._contract.transform( value, executor.create_object_context( this.cast(self._container_type())), this, owner, default, helpers.get_type(context))
def transform(self, value, this, owner, context, default=None, finalize=True): if default is None: default = self.default if isinstance(this, dsl_types.MuranoTypeReference): this = this.type if isinstance(this, dsl_types.MuranoType): return self._contract.transform( value, self._get_this_context(this), None, None, default, helpers.get_type(context), finalize=finalize) else: return self._contract.transform( value, self._get_this_context(this), this, owner, default, helpers.get_type(context), finalize=finalize)
def set_property(self, name, value, context=None): start_type, derived = self.__type, False caller_class = None if not context else helpers.get_type(context) if caller_class is not None and caller_class.is_compatible(self): start_type, derived = caller_class, True declared_properties = start_type.find_property(name) if context is None: context = self.object_store.executor.create_object_context(self) if len(declared_properties) > 0: declared_properties = self.type.find_property(name) values_to_assign = [] for mc in declared_properties: spec = mc.get_property(name) if (caller_class is not None and not helpers.are_property_modifications_allowed(context) and (spec.usage not in typespec.PropertyUsages.Writable or not derived)): raise exceptions.NoWriteAccessError(name) default = self.__config.get(name, spec.default) default = self.__defaults.get(name, default) default = helpers.evaluate(default, context) obj = self.cast(mc) values_to_assign.append((obj, spec.validate( value, self.real_this, self.real_this, default=default))) for obj, value in values_to_assign: obj.__properties[name] = value elif derived: obj = self.cast(caller_class) obj.__properties[name] = value else: raise exceptions.PropertyWriteError(name, start_type)
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
def _get_package(owner, receiver): if owner is None: if isinstance(receiver, dsl_types.MuranoObjectInterface): return receiver.extension._package murano_class = helpers.get_type(helpers.get_caller_context()) else: murano_class = owner.type return murano_class.package
def get_attr(self, this, context, name, default=None, owner=None): if owner is None: owner = helpers.get_type(helpers.get_caller_context(context)) attribute_store = helpers.get_attribute_store(context) result = attribute_store.get(this.object, owner, name) return default if result is None else result
def super_(context, sender, func=None): cast_type = helpers.get_type(context) if func is None: return [ sender.cast(type) for type in cast_type.parents(sender.real_this.type) ] return itertools.imap(func, super_(context, sender))
def setAttr(self, _context, name, value, owner=None): if owner is None: owner = helpers.get_type(helpers.get_caller_context(_context)) if not isinstance(owner, murano_class.MuranoClass): raise TypeError() attribute_store = helpers.get_attribute_store(_context) attribute_store.set(self, owner, name, value)
def compose_stack_frame(context): instruction = helpers.get_current_instruction(context) return { 'instruction': None if instruction is None else str(instruction), 'location': None if instruction is None else instruction.source_file_position, 'method': helpers.get_current_method(context), 'class': helpers.get_type(context) }
def invoke(context, this, *args): try: executor = helpers.get_executor(context) murano_class = helpers.get_type(context) return executor.invoke_method(name, this, context, murano_class, *args) except exceptions.NoMethodFound: raise yaql.exceptions.YaqlExecutionException() except exceptions.AmbiguousMethodName: raise yaql.exceptions.YaqlExecutionException()
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
def getAttr(self, _context, name, default=None, owner=None): if owner is None: owner = helpers.get_type(helpers.get_caller_context(_context)) if not isinstance(owner, murano_class.MuranoClass): raise TypeError() attribute_store = helpers.get_attribute_store(_context) result = attribute_store.get(self, owner, name) return default if result is None else result
def _get_container(context, obj, class_name): namespace_resolver = helpers.get_type(context).namespace_resolver class_loader = helpers.get_class_loader(context) class_name = namespace_resolver.resolve_name(class_name) murano_class = class_loader.get_class(class_name) p = obj.owner while p is not None: if murano_class.is_compatible(p): return p p = p.owner return None
def set_property(self, name, value, context=None, dry_run=False): start_type, derived = self.real_this.type, False caller_class = None if not context else helpers.get_type(context) if caller_class is not None and caller_class.is_compatible(self): start_type, derived = caller_class, True if context is None: context = self.executor.create_object_context(self) declared_properties = start_type.find_properties( lambda p: p.name == name) if len(declared_properties) > 0: ultimate_spec = self.real_this.type.find_single_property(name) property_list = list(self._list_properties(name)) for spec in property_list: if (caller_class is not None and not helpers.are_property_modifications_allowed(context) and (spec.usage not in dsl_types.PropertyUsages.Writable or not derived)): raise exceptions.NoWriteAccessError(name) if spec.usage == dsl_types.PropertyUsages.Static: default = None else: default = self._config.get(name, spec.default) if spec is ultimate_spec: value = spec.transform(value, self.real_this, self.real_this, context, default=default, finalize=len(property_list) == 1) else: spec.validate(value, self.real_this, context, default) if len(property_list) > 1: value = ultimate_spec.finalize(value, self.real_this, context) if ultimate_spec.usage == dsl_types.PropertyUsages.Static: self.executor.set_static_property(ultimate_spec.declaring_type, name, value, context, dry_run=dry_run) elif not dry_run: self.real_this._properties[name] = value elif derived: if not dry_run: obj = self.cast(caller_class) obj._properties[name] = value else: raise exceptions.PropertyWriteError(name, start_type)
def convert(self, value, sender, context, function_spec, engine, *args, **kwargs): context = self._context or context if isinstance(value, yaql_expressions.Expression): value = value(utils.NO_VALUE, context, engine) value = super(MuranoTypeName, self).convert( value, sender, context, function_spec, engine) if isinstance(value, types.StringTypes): murano_type = helpers.get_type(context) value = dsl_types.MuranoClassReference( helpers.get_class( murano_type.namespace_resolver.resolve_name( value), context)) return value
def convert(self, value, sender, context, function_spec, engine, *args, **kwargs): context = self._context or context if isinstance(value, yaql_expressions.Expression): value = value(utils.NO_VALUE, context, engine) value = super(MuranoTypeName, self).convert(value, sender, context, function_spec, engine) if isinstance(value, types.StringTypes): murano_type = helpers.get_type(context) value = dsl_types.MuranoClassReference( helpers.get_class( murano_type.namespace_resolver.resolve_name(value), context)) return value
def get_property(self, name, context=None): start_type, derived = self.__type, False caller_class = None if not context else helpers.get_type(context) if caller_class is not None and caller_class.is_compatible(self): start_type, derived = caller_class, True if name in start_type.properties: return self.cast(start_type)._get_property_value(name) else: declared_properties = start_type.find_single_property(name) if declared_properties: return self.cast(declared_properties).__properties[name] elif derived: return self.cast(caller_class)._get_property_value(name) else: raise exceptions.PropertyReadError(name, start_type)
def convert(self, value, sender, context, function_spec, engine, *args, **kwargs): context = self._context or context if isinstance(value, yaql_expressions.Expression): value = value(utils.NO_VALUE, context, engine) value = super(MuranoTypeName, self).convert( value, sender, context, function_spec, engine) if isinstance(value, six.string_types): if function_spec.meta.get(constants.META_MURANO_METHOD): context = helpers.get_caller_context(context) murano_type = helpers.get_type(context) value = helpers.get_class( murano_type.namespace_resolver.resolve_name(value), context).get_reference() return value
def check(self, value, context, *args, **kwargs): if not super(MuranoType, self).check(value, context, *args, **kwargs): return False if isinstance(value, MuranoObjectInterface): value = value.object if value is None or isinstance(value, yaql_expressions.Expression): return True murano_class = self.murano_class if isinstance(murano_class, types.StringTypes): murano_class_name = murano_class else: murano_class_name = murano_class.name return helpers.is_instance_of( value, murano_class_name, self.version_spec or helpers.get_type(context))
def check(self, value, context, *args, **kwargs): if not super(MuranoType, self).check( value, context, *args, **kwargs): return False if isinstance(value, MuranoObjectInterface): value = value.object if value is None or isinstance(value, yaql_expressions.Expression): return True murano_class = self.murano_class if isinstance(murano_class, six.string_types): murano_class_name = murano_class else: murano_class_name = murano_class.name return helpers.is_instance_of( value, murano_class_name, self.version_spec or helpers.get_type(context))
def initialize(self, _context, includeNativeFrames=True): frames = [] context = _context while True: if not context: break instruction = helpers.get_current_instruction(context) frames.append({ 'instruction': None if instruction is None else str(instruction), 'location': None if instruction is None else instruction.source_file_position, 'method': helpers.get_current_method(context), 'class': helpers.get_type(context) }) context = helpers.get_caller_context(context) frames.pop() frames.reverse() if includeNativeFrames: class InstructionStub(object): def __init__(self, title, position): self._title = title self.source_file_position = position def __str__(self): return self._title native_frames = [] for frame in inspect.trace()[1:]: info = inspect.getframeinfo(frame[0]) position = yaql_expression.YaqlExpressionFilePosition( os.path.abspath(info.filename), info.lineno, -1, -1, -1, -1, -1) instruction = InstructionStub( info.code_context[0].strip(), position) method = info.function native_frames.append({ 'instruction': instruction, 'method': method, 'class': None }) frames.extend(native_frames) self.set_property('frames', frames)
def set_property(self, name, value, context=None): if self is not self.real_this: return self.real_this.set_property(name, value, context) start_type, derived = self.__type, False caller_class = None if not context else helpers.get_type(context) if caller_class is not None and caller_class.is_compatible(self): start_type, derived = caller_class, True declared_properties = start_type.find_properties( lambda p: p.name == name) if context is None: context = helpers.get_executor().create_object_context(self) if len(declared_properties) > 0: values_to_assign = [] classes_for_static_properties = [] first = True for spec in self._list_properties(name): if (caller_class is not None and not helpers.are_property_modifications_allowed(context) and (spec.usage not in dsl_types.PropertyUsages.Writable or not derived)): raise exceptions.NoWriteAccessError(name) if spec.usage == dsl_types.PropertyUsages.Static: classes_for_static_properties.append(spec.declaring_type) else: default = self.__config.get(name, spec.default) obj = self.cast(spec.declaring_type) res = spec.transform(value, self.real_this, self.real_this, context, default=default) if first: value = res first = False values_to_assign.append((obj, res)) for obj, value in values_to_assign: obj.__properties[name] = value for cls in classes_for_static_properties: cls.set_property(name, value, context) elif derived: obj = self.cast(caller_class) obj.__properties[name] = value else: raise exceptions.PropertyWriteError(name, start_type)
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
def inject_method_with_str(context, target, target_method, mock_object, mock_name): ctx_manager = helpers.get_executor().context_manager current_class = helpers.get_type(context) mock_func = current_class.find_single_method(mock_name) original_class = target.type original_function = original_class.find_single_method(target_method) result_fd = original_function.instance_stub.clone() def payload_adapter(__context, __sender, *args, **kwargs): return mock_func.invoke(mock_object, args, kwargs, __context, True) result_fd.payload = payload_adapter existing_mocks = ctx_manager.class_mock_ctx.setdefault( original_class.name, []) existing_mocks.append(result_fd)
def _new(context, name, *args): murano_class = helpers.get_type(context) name = murano_class.namespace_resolver.resolve_name(name) parameters = {} arg_values = [t() for t in args] if len(arg_values) == 1 and isinstance(arg_values[0], types.DictionaryType): parameters = arg_values[0] elif len(arg_values) > 0: for p in arg_values: if not isinstance(p, types.TupleType) or not isinstance(p[0], types.StringType): raise SyntaxError() parameters[p[0]] = p[1] object_store = helpers.get_object_store(context) class_loader = helpers.get_class_loader(context) new_context = yaql.context.Context(parent_context=context) for key, value in parameters.iteritems(): new_context.set_data(value, key) return class_loader.get_class(name).new(None, object_store, new_context, parameters=parameters)
def get_property(self, name, context=None): start_type, derived = self.type, False caller_class = None if not context else helpers.get_type(context) if caller_class is not None and caller_class.is_compatible(self): start_type, derived = caller_class, True declared_properties = start_type.find_properties( lambda p: p.name == name) if len(declared_properties) > 0: spec = self.real_this.type.find_single_property(name) if spec.usage == dsl_types.PropertyUsages.Static: return self.executor.get_static_property( spec.declaring_type, name, context) else: return self.real_this._get_property_value(name) elif derived: return self.cast(caller_class)._get_property_value(name) else: raise exceptions.PropertyReadError(name, start_type)
def inject_method_with_str(context, target, target_method, mock_object, mock_name): ctx_manager = helpers.get_executor().context_manager current_class = helpers.get_type(context) mock_func = current_class.find_single_method(mock_name) original_class = target.type original_function = original_class.find_single_method(target_method) result_fd = original_function.instance_stub.clone() def payload_adapter(__context, __sender, *args, **kwargs): return mock_func.invoke( mock_object, args, kwargs, __context, True) result_fd.payload = payload_adapter existing_mocks = ctx_manager.class_mock_ctx.setdefault( original_class.name, []) existing_mocks.append(result_fd)
def check(self, value, context, *args, **kwargs): if not super(MuranoObjectParameter, self).check( value, context, *args, **kwargs): return False if value is None or isinstance(value, yaql_expressions.Expression): return True if isinstance(value, MuranoObjectInterface): value = value.object if not isinstance(value, dsl_types.MuranoObject): return False if self.murano_class: murano_class = self.murano_class if isinstance(murano_class, six.string_types): return helpers.is_instance_of( value, murano_class, self.version_spec or helpers.get_type(context)) else: return murano_class.is_compatible(value) else: return True
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( 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
def set_property(self, name, value, context=None): start_type, derived = self.__type, False caller_class = None if not context else helpers.get_type(context) if caller_class is not None and caller_class.is_compatible(self): start_type, derived = caller_class, True declared_properties = start_type.find_properties( lambda p: p.name == name) if context is None: context = self.executor.create_object_context(self) if len(declared_properties) > 0: declared_properties = self.type.find_properties( lambda p: p.name == name) values_to_assign = [] classes_for_static_properties = [] for spec in declared_properties: if (caller_class is not None and not helpers.are_property_modifications_allowed(context) and (spec.usage not in dsl_types.PropertyUsages.Writable or not derived)): raise exceptions.NoWriteAccessError(name) if spec.usage == dsl_types.PropertyUsages.Static: classes_for_static_properties.append(spec.declaring_type) else: default = self.__config.get(name, spec.default) # default = helpers.evaluate(default, context) obj = self.cast(spec.declaring_type) values_to_assign.append((obj, spec.transform( value, self.real_this, self.real_this, context, default=default))) for obj, value in values_to_assign: obj.__properties[name] = value for cls in classes_for_static_properties: cls.set_property(name, value, context) elif derived: obj = self.cast(caller_class) obj.__properties[name] = value else: raise exceptions.PropertyWriteError(name, start_type)
def get_property(self, name, context=None): start_type, derived = self.__type, False caller_class = None if not context else helpers.get_type(context) if caller_class is not None and caller_class.is_compatible(self): start_type, derived = caller_class, True if name in start_type.properties: spec = start_type.properties[name] if spec.usage == dsl_types.PropertyUsages.Static: return spec.declaring_type.get_property(name, context) else: return self.cast(start_type)._get_property_value(name) else: try: spec = start_type.find_single_property(name) if spec.usage == dsl_types.PropertyUsages.Static: return spec.declaring_type.get_property(name, context) else: return self.cast(spec.declaring_type).__properties[name] except exceptions.NoPropertyFound: if derived: return self.cast(caller_class)._get_property_value(name) else: raise exceptions.PropertyReadError(name, start_type)
def initialize(self, _context, includeNativeFrames=True): frames = [] context = _context while True: if not context: break instruction = helpers.get_current_instruction(context) frames.append({ 'instruction': None if instruction is None else str(instruction), 'location': None if instruction is None else instruction.source_file_position, 'method': helpers.get_current_method(context), 'class': helpers.get_type(context) }) context = helpers.get_caller_context(context) frames.pop() frames.reverse() if includeNativeFrames: native_frames = [] for frame in inspect.trace()[1:]: location = yaql_expression.YaqlExpressionFilePosition( os.path.abspath(frame[1]), frame[2], -1, -1, -1, -1, -1) method = frame[3] native_frames.append({ 'instruction': frame[4][0].strip(), 'location': location, 'method': method, 'class': None }) frames.extend(native_frames) self.set_property('frames', frames)
def set_property(self, name, value, context=None): start_type, derived = self.__type, False caller_class = None if not context else helpers.get_type(context) if caller_class is not None and caller_class.is_compatible(self): start_type, derived = caller_class, True declared_properties = start_type.find_property(name) if context is None: context = self.object_store.executor.create_object_context(self) if len(declared_properties) > 0: declared_properties = self.type.find_property(name) values_to_assign = [] for mc in declared_properties: spec = mc.get_property(name) if (caller_class is not None and not helpers.are_property_modifications_allowed(context) and (spec.usage not in typespec.PropertyUsages.Writable or not derived)): raise exceptions.NoWriteAccessError(name) default = self.__config.get(name, spec.default) default = self.__defaults.get(name, default) default = helpers.evaluate(default, context) obj = self.cast(mc) values_to_assign.append((obj, spec.validate(value, self.real_this, self.real_this, default=default))) for obj, value in values_to_assign: obj.__properties[name] = value elif derived: obj = self.cast(caller_class) obj.__properties[name] = value else: raise exceptions.PropertyWriteError(name, start_type)
def _new(context, name, *args): murano_class = helpers.get_type(context) name = murano_class.namespace_resolver.resolve_name(name) parameters = {} arg_values = [t() for t in args] if len(arg_values) == 1 and isinstance(arg_values[0], types.DictionaryType): parameters = arg_values[0] elif len(arg_values) > 0: for p in arg_values: if not isinstance(p, types.TupleType) or \ not isinstance(p[0], types.StringType): raise SyntaxError() parameters[p[0]] = p[1] object_store = helpers.get_object_store(context) class_loader = helpers.get_class_loader(context) new_context = yaql.context.Context(parent_context=context) for key, value in parameters.iteritems(): new_context.set_data(value, key) return class_loader.get_class(name).new(None, object_store, new_context, parameters=parameters)
def _super(context, value): cast_type = helpers.get_type(context) return [value.cast(type) for type in cast_type.parents]
def _cast(context, value, type): if '.' not in type: murano_class = helpers.get_type(context) type = murano_class.namespace_resolver.resolve_name(type) class_loader = helpers.get_class_loader(context) return value.cast(class_loader.get_class(type))
def __init__(self, context): murano_class = helpers.get_type(helpers.get_caller_context(context)) self._package = murano_class.package
def is_instance_of(self, murano_class, version_spec=None): return helpers.is_instance_of( self.__object, murano_class, version_spec or helpers.get_type())
def _resolve_names(self, names, context): murano_class = helpers.get_type(context) for name in names: yield murano_class.namespace_resolver.resolve_name(name)
def is_instance_of(self, murano_class, version_spec=None): return helpers.is_instance_of( self.object, murano_class, version_spec or helpers.get_type())
def cast(self, murano_class, version_spec=None): return MuranoObjectInterface.create( helpers.cast( self.object, murano_class, version_spec or helpers.get_type()))
def cast(self, murano_class, version_spec=None): return MuranoObjectInterface( helpers.cast( self.__object, murano_class, version_spec or helpers.get_type()), self.__executor)
def initialize(self, package_loader, _context): murano_class = helpers.get_type(_context) self._package = package_loader.get_package(murano_class.package.name)
def set_attr(self, this, context, name, value, owner=None): if owner is None: owner = helpers.get_type(helpers.get_caller_context(context)) attribute_store = helpers.get_attribute_store(context) attribute_store.set(this.object, owner, name, value)