def test_evaluate(self): yaql_value = mock.Mock(spec=yaql_expression.YaqlExpression, evaluate=lambda context: 'atom') complex_value = { yaql_value: ['some', (1, yaql_value), lambda: 'hi!'], 'sample': [yaql_value, xrange(5)] } complex_literal = { 'atom': ['some', (1, 'atom'), 'hi!'], 'sample': ['atom', [0, 1, 2, 3, 4]] } # tuple(evaluate(list)) transformation adds + 1 complex_literal_depth = 3 + 1 context = yaql.create_context(False) evaluated_value = helpers.evaluate(yaql_value, context, 1) non_evaluated_value = helpers.evaluate(yaql_value, context, 0) evaluated_complex_value = helpers.evaluate(complex_value, context) non_evaluated_complex_value = helpers.evaluate(complex_value, context, complex_literal_depth) self.assertEqual('atom', evaluated_value) self.assertNotEqual('atom', non_evaluated_value) self.assertEqual(complex_literal, evaluated_complex_value) self.assertNotEqual(complex_literal, non_evaluated_complex_value)
def _evaluate_parameters(self, arguments_scheme, context, this, *args): arg_names = list(arguments_scheme.keys()) parameter_values = {} i = 0 for arg in args: value = helpers.evaluate(arg, context) if isinstance(value, types.TupleType) and len(value) == 2 and \ isinstance(value[0], types.StringTypes): name = value[0] value = value[1] if name not in arguments_scheme: raise TypeError() else: if i >= len(arg_names): raise TypeError() name = arg_names[i] i += 1 if callable(value): value = value() arg_spec = arguments_scheme[name] parameter_values[name] = arg_spec.validate( value, this, None, self._root_context, self._object_store) for name, arg_spec in arguments_scheme.iteritems(): if name not in parameter_values: if not arg_spec.has_default: raise TypeError() parameter_context = self._create_context( this, this.type, context) parameter_values[name] = arg_spec.validate( helpers.evaluate(arg_spec.default, parameter_context), this, None, self._root_context, self._object_store) return parameter_values
def stub(*args, **kwargs): context = self.context args = tuple(helpers.evaluate(arg, context) for arg in args) kwargs = dict((key, helpers.evaluate(value, context)) for key, value in kwargs.iteritems()) return to_mutable( context(item, self.engine, self.sender)(*args, **kwargs), self.engine)
def stub(*args, **kwargs): context = self.context args = tuple(helpers.evaluate(arg, context) for arg in args) kwargs = dict((key, helpers.evaluate(value, context)) for key, value in six.iteritems(kwargs)) return to_mutable( context(item, self.engine, self.sender)(*args, **kwargs), self.engine)
def execute(self, context, murano_class): if not self.code_block: return limit = helpers.evaluate(self._limit, context) gpool = greenpool.GreenPool(helpers.evaluate(limit, context)) for expr in self.code_block: gpool.spawn_n(expr.execute, context, murano_class) gpool.waitall()
def __call__(self, __expression, *args, **kwargs): context = helpers.get_context().create_child_context() for i, param in enumerate(args): context['$' + str(i + 1)] = helpers.evaluate(param, context) for arg_name, arg_value in six.iteritems(kwargs): context['$' + arg_name] = helpers.evaluate(arg_value, context) parsed = self.engine(__expression) res = parsed.evaluate(context=context) return to_mutable(res, self.engine)
def __call__(self, __expression, *args, **kwargs): context = helpers.get_context().create_child_context() for i, param in enumerate(args): context['$' + str(i + 1)] = helpers.evaluate(param, context) for arg_name, arg_value in kwargs.iteritems(): context['$' + arg_name] = helpers.evaluate(arg_value, context) parsed = self.engine(__expression) res = parsed.evaluate(context=context) return to_mutable(res, self.engine)
def execute(self, context): stacktrace = stack_trace.create_stack_trace(context, False) cause = None if self._cause: cause = helpers.evaluate(self._cause, context).get_property( 'nativeException') raise dsl_exception.MuranoPlException( list(self._resolve_names(helpers.evaluate(self._names, context), context)), helpers.evaluate(self._message, context), stacktrace, self._extra, cause)
def execute(self, context, murano_class): stacktrace = yaql_functions.new('io.murano.StackTrace', context, includeNativeFrames=False) cause = None if self._cause: cause = helpers.evaluate(self._cause, context).get_property( 'nativeException') raise dsl_exception.MuranoPlException( list(self._resolve_names(helpers.evaluate(self._names, context), context)), helpers.evaluate(self._message, context), stacktrace, self._extra, cause)
def execute(self, context): stacktrace = stack_trace.create_stack_trace(context, False) cause = None if self._cause: cause = helpers.evaluate(self._cause, context).get_property('nativeException') raise dsl_exception.MuranoPlException( list( self._resolve_names(helpers.evaluate(self._names, context), context)), helpers.evaluate(self._message, context), stacktrace, self._extra, cause)
def execute(self, context, murano_class): stacktrace = yaql_functions.new('io.murano.StackTrace', context, includeNativeFrames=False) cause = None if self._cause: cause = helpers.evaluate(self._cause, context).get_property('nativeException') raise dsl_exception.MuranoPlException( list( self._resolve_names(helpers.evaluate(self._names, context), context)), helpers.evaluate(self._message, context), stacktrace, self._extra, cause)
def test_evaluate(self): yaql_value = mock.Mock(yaql_expression.YaqlExpression, return_value='atom') complex_value = {yaql_value: ['some', (1, yaql_value), 'hi!'], 'sample': [yaql_value, xrange(5)]} complex_literal = utils.FrozenDict({ 'atom': ('some', (1, 'atom'), 'hi!'), 'sample': ('atom', (0, 1, 2, 3, 4)) }) context = yaql.create_context() evaluated_value = helpers.evaluate(yaql_value, context) evaluated_complex_value = helpers.evaluate(complex_value, context) self.assertEqual('atom', evaluated_value) self.assertEqual(complex_literal, evaluated_complex_value)
def test_evaluate(self): yaql_value = mock.Mock(yaql_expression.YaqlExpression, return_value='atom') complex_value = {yaql_value: ['some', (1, yaql_value), 'hi!'], 'sample': [yaql_value, six.moves.range(5)]} complex_literal = utils.FrozenDict({ 'atom': ('some', (1, 'atom'), 'hi!'), 'sample': ('atom', (0, 1, 2, 3, 4)) }) context = yaql.create_context() evaluated_value = helpers.evaluate(yaql_value, context) evaluated_complex_value = helpers.evaluate(complex_value, context) self.assertEqual('atom', evaluated_value) self.assertEqual(complex_literal, evaluated_complex_value)
def extension_payload(__context, __receiver, *args, **kwargs): executor = helpers.get_executor(__context) args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) return helpers.evaluate(murano_method.invoke( executor, murano_method.declaring_type, (__receiver,) + args, kwargs, __context, True), __context)
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 extension_payload(__context, __receiver, *args, **kwargs): args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) return helpers.evaluate( murano_method.invoke(murano_method.declaring_type, (__receiver, ) + args, kwargs, __context, True), __context)
def payload(__context, __receiver, *args, **kwargs): args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) with helpers.contextual(__context): __context[constants.CTX_NAMES_SCOPE] = murano_class result = helpers.evaluate(cls(*args, **kwargs), __context) __receiver.object.extension = result
def payload(__context, __receiver, *args, **kwargs): args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) with helpers.contextual(__context): __context[constants.CTX_NAMES_SCOPE] = \ murano_class return helpers.evaluate(cls(*args, **kwargs), __context)
def execute(self, context): if not self.code_block: return limit = helpers.evaluate(self._limit, context) helpers.parallel_select( self.code_block, lambda expr: expr.execute(context.create_child_context()), limit)
def execute(self, context, murano_class): count = helpers.evaluate(self._count, context) for t in range(0, count): try: self._code.execute(context, murano_class) except exceptions.BreakException: break
def execute(self, context): if not self.code_block: return limit = helpers.evaluate(self._limit, context) helpers.parallel_select( self.code_block, lambda expr: expr.execute(context.create_child_context()), limit)
def execute(self, context): match_value = helpers.evaluate(self._value, context) for key, value in self._switch.iteritems(): if key == match_value: CodeBlock(value).execute(context) return if self._default is not None: self._default.execute(context)
def execute(self, context): matched = False for key, value in self._switch.items(): if helpers.evaluate(key, context): matched = True CodeBlock(value).execute(context) if self._default is not None and not matched: self._default.execute(context)
def execute(self, context, murano_class): collection = helpers.evaluate(self._collection, context) child_context = yaql.context.Context(context) for t in collection: child_context.set_data(t, self._var) try: self._code.execute(child_context, murano_class) except exceptions.BreakException: break
def execute(self, context): matched = False for key, value in six.iteritems(self._switch): if helpers.evaluate(key, context): matched = True CodeBlock(value).execute(context) if self._default is not None and not matched: self._default.execute(context)
def execute(self, context): count = helpers.evaluate(self._count, context) for _ in range(0, count): try: self._code.execute(context) except exceptions.BreakException: break except exceptions.ContinueException: continue
def execute(self, context): collection = helpers.evaluate(self._collection, context) for t in collection: context[self._var] = t try: self._code.execute(context) except exceptions.BreakException: break except exceptions.ContinueException: continue
def __call__(self, data, context, this, owner, default): # TODO(ativelkov, slagun): temporary fix, need a better way of handling # composite defaults # A bug (#1313694) has been filed if data is dsl.NO_VALUE: data = helpers.evaluate(default, context) context = self.prepare_context(context, this, owner, default) return self._map(data, self._spec, context)
def validate(self, data, context, default): if data is dsl.NO_VALUE: data = helpers.evaluate(default, context) context = self.prepare_validate_context(context) try: self._map(data, self._spec, context, '') return True except exceptions.ContractViolationException: return False
def __call__(self, data, context, this, owner, default): # TODO(ativelkov, slagun): temporary fix, need a better way of handling # composite defaults # A bug (#1313694) has been filed if data is dsl.NO_VALUE: data = helpers.evaluate(default, context) context = self.prepare_context(context, this, owner, default) return self._map(data, self._spec, context, '')
def execute(self, context): try: result = helpers.evaluate(self.expression, context) if self.destination: self.destination(result, context) return result except dsl_exception.MuranoPlException: raise except Exception as e: raise dsl_exception.MuranoPlException.from_python_exception(e, context)
def execute(self, context, murano_class): collection = helpers.evaluate(self._collection, context) for t in collection: context.set_data(t, self._var) try: self._code.execute(context, murano_class) except exceptions.BreakException: break except exceptions.ContinueException: continue
def execute(self, context): try: result = helpers.evaluate(self.expression, context) if self.destination: self.destination(result, context) return result except dsl_exception.MuranoPlException: raise except Exception as e: raise dsl_exception.MuranoPlException.from_python_exception( e, context)
def test_evaluate(self): yaql_value = mock.Mock(spec=yaql_expression.YaqlExpression, evaluate=lambda context: 'atom') complex_value = {yaql_value: ['some', (1, yaql_value), lambda: 'hi!'], 'sample': [yaql_value, xrange(5)]} complex_literal = {'atom': ['some', (1, 'atom'), 'hi!'], 'sample': ['atom', [0, 1, 2, 3, 4]]} # tuple(evaluate(list)) transformation adds + 1 complex_literal_depth = 3 + 1 context = yaql.create_context(False) evaluated_value = helpers.evaluate(yaql_value, context, 1) non_evaluated_value = helpers.evaluate(yaql_value, context, 0) evaluated_complex_value = helpers.evaluate(complex_value, context) non_evaluated_complex_value = helpers.evaluate( complex_value, context, complex_literal_depth) self.assertEqual('atom', evaluated_value) self.assertNotEqual('atom', non_evaluated_value) self.assertEqual(complex_literal, evaluated_complex_value) self.assertNotEqual(complex_literal, non_evaluated_complex_value)
def execute(self, context): matched = False for key, value in self._switch.iteritems(): res = helpers.evaluate(key, context) if not isinstance(res, types.BooleanType): raise exceptions.DslInvalidOperationError( 'Switch case must be evaluated to boolean type') if res: matched = True CodeBlock(value).execute(context) if self._default is not None and not matched: self._default.execute(context)
def transform(self, data, context, this, owner, default, calling_type): # TODO(ativelkov, slagun): temporary fix, need a better way of handling # composite defaults # A bug (#1313694) has been filed if data is dsl.NO_VALUE: data = helpers.evaluate(default, context) if is_passkey(data): return data context = self.prepare_transform_context(context, this, owner, default, calling_type) return self._map(data, self._spec, context, '')
def execute(self, context, murano_class): matched = False for key, value in self._switch.iteritems(): if not isinstance(key, (yaql_expression.YaqlExpression, types.BooleanType)): raise SyntaxError() res = helpers.evaluate(key, context) if not isinstance(res, types.BooleanType): raise TypeError() if res: matched = True child_context = yaql.context.Context(context) CodeBlock(value).execute(child_context, murano_class) if self._default is not None and not matched: self._default.execute(context, murano_class)
def _execute(self, base_context_func, data, context, default, **kwargs): # TODO(ativelkov, slagun): temporary fix, need a better way of handling # composite defaults # A bug (#1313694) has been filed if data is dsl.NO_VALUE: data = helpers.evaluate(default, context) if helpers.is_passkey(data): return data contract_context = base_context_func( self._runtime_version).create_child_context() contract_context['root_context'] = context for key, value in six.iteritems(kwargs): contract_context[key] = value contract_context[constants.CTX_NAMES_SCOPE] = \ context[constants.CTX_NAMES_SCOPE] return self._map(data, self._spec, contract_context, '')
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 payload(__context, __sender, *args, **kwargs): assert __sender is None args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) with helpers.contextual(__context): return helpers.evaluate(cls(*args, **kwargs), __context)
def _finalize(obj, context): return helpers.evaluate(obj, context)
def execute(self, context): raise exceptions.ReturnException( helpers.evaluate(self._value, context))
def static_payload(__context, __receiver, *args, **kwargs): args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) return helpers.evaluate( murano_method.invoke(__receiver, args, kwargs, __context, True), __context)
def payload(__context, __sender, *args, **kwargs): executor = helpers.get_executor(__context) args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) return helpers.evaluate(murano_method.invoke( executor, __sender, args, kwargs, __context, True), __context)
def static_payload(__context, __receiver, *args, **kwargs): args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) return helpers.evaluate(murano_method.invoke( __receiver, args, kwargs, __context, True), __context)
def execute(self, context): if helpers.evaluate(self._condition, context): self._code1.execute(context) elif self._code2 is not None: self._code2.execute(context)
def payload(__context, __sender, *args, **kwargs): assert __sender is None args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) with helpers.contextual(__context): return helpers.evaluate(cls(*args, **kwargs), __context)
def _finalize(obj, context): return helpers.evaluate(obj, context)
def __setitem__(self, key, value): context = helpers.get_context() value = helpers.evaluate(value, context) self.__object.set_property(key, value, context)
def __setitem__(self, key, value): context = helpers.get_context() value = helpers.evaluate(value, context) self.object.set_property(key, value, context)
def execute(self, context): if helpers.evaluate(self._condition, context): self._code1.execute(context) elif self._code2 is not None: self._code2.execute(context)
def payload(__context, __receiver, *args, **kwargs): executor = helpers.get_executor(__context) args = tuple(dsl.to_mutable(arg, engine) for arg in args) kwargs = dsl.to_mutable(kwargs, engine) return helpers.evaluate(murano_method.invoke( executor, __receiver, args, kwargs, __context, True), __context)