def get_class_factory_definition(cls, murano_class): runtime_version = murano_class.package.runtime_version engine = choose_yaql_engine(runtime_version) 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 try: fd = specs.get_function_definition( helpers.function(cls.__init__), parameter_type_func=lambda name: _infer_parameter_type( name, cls.__name__), convention=CONVENTION) except AttributeError: # __init__ is a slot wrapper inherited from object or other C type fd = specs.get_function_definition(lambda self: None) fd.meta[constants.META_NO_TRACE] = True fd.insert_parameter( specs.ParameterDefinition('?1', yaqltypes.Context(), position=0)) fd.is_method = True fd.is_function = False fd.name = '__init__' fd.payload = payload return fd
def get_class_factory_definition(cls, murano_class): runtime_version = murano_class.package.runtime_version engine = choose_yaql_engine(runtime_version) 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) try: fd = specs.get_function_definition( helpers.function(cls.__init__), parameter_type_func=lambda name: _infer_parameter_type( name, cls.__name__), convention=CONVENTION) except AttributeError: # __init__ is a slot wrapper inherited from object or other C type fd = specs.get_function_definition(lambda self: None) fd.meta[constants.META_NO_TRACE] = True fd.insert_parameter(specs.ParameterDefinition( '?1', yaqltypes.Context(), position=0)) fd.is_method = True fd.is_function = False fd.name = '__init__' fd.payload = payload return fd
def _get_contract_factory(cls, action_func): def payload(context, value, *args, **kwargs): instance = object.__new__(cls) instance.value = value instance.context = context instance.__init__(*args, **kwargs) return action_func(instance) name = yaql_integration.CONVENTION.convert_function_name(cls.name) try: init_spec = specs.get_function_definition( helpers.function(cls.__init__), name=name, method=True, convention=yaql_integration.CONVENTION) except AttributeError: init_spec = specs.get_function_definition(lambda self: None, name=name, method=True) init_spec.parameters['self'] = specs.ParameterDefinition( 'self', yaqltypes.PythonType(object, nullable=True), 0) init_spec.insert_parameter( specs.ParameterDefinition('?1', yaqltypes.Context(), 0)) init_spec.payload = payload return init_spec
def get_class_factory_definition(cls, murano_class): runtime_version = murano_class.package.runtime_version engine = choose_yaql_engine(runtime_version) 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 cls(*args, **kwargs) if hasattr(cls.__init__, 'im_func'): fd = specs.get_function_definition( cls.__init__.im_func, parameter_type_func=lambda name: _infer_parameter_type( name, cls.__init__.im_class.__name__), convention=CONVENTION) else: fd = specs.get_function_definition(lambda self: None) fd.meta[constants.META_NO_TRACE] = True fd.insert_parameter( specs.ParameterDefinition('?1', yaqltypes.Context(), position=0)) fd.is_method = True fd.is_function = False fd.name = '__init__' fd.payload = payload return fd
def get_class_factory_definition(cls, murano_class): runtime_version = murano_class.package.runtime_version engine = choose_yaql_engine(runtime_version) 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) if hasattr(cls.__init__, 'im_func'): fd = specs.get_function_definition( cls.__init__.im_func, parameter_type_func=lambda name: _infer_parameter_type( name, cls.__init__.im_class.__name__), convention=CONVENTION) else: fd = specs.get_function_definition(lambda self: None) fd.meta[constants.META_NO_TRACE] = True fd.insert_parameter(specs.ParameterDefinition( '?1', yaqltypes.Context(), position=0)) fd.is_method = True fd.is_function = False fd.name = '__init__' fd.payload = payload return fd
def _build_native_stub_function_definitions(murano_method): runtime_version = murano_method.declaring_type.package.runtime_version engine = choose_yaql_engine(runtime_version) @specs.method @specs.name(murano_method.name) @specs.meta(constants.META_MURANO_METHOD, murano_method) @specs.parameter('__receiver', yaqltypes.NotOfType(dsl_types.MuranoTypeReference)) def 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) @specs.method @specs.name(murano_method.name) @specs.meta(constants.META_MURANO_METHOD, murano_method) @specs.parameter('__receiver', yaqltypes.NotOfType(dsl_types.MuranoTypeReference)) 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) @specs.method @specs.name(murano_method.name) @specs.meta(constants.META_MURANO_METHOD, murano_method) @specs.parameter('__receiver', dsl_types.MuranoTypeReference) 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) if murano_method.usage in dsl_types.MethodUsages.InstanceMethods: return specs.get_function_definition(payload), None elif murano_method.usage == dsl_types.MethodUsages.Static: return (specs.get_function_definition(payload), specs.get_function_definition(static_payload)) elif murano_method.usage == dsl_types.MethodUsages.Extension: return (specs.get_function_definition(extension_payload), specs.get_function_definition(static_payload)) else: raise ValueError('Unknown method usage ' + murano_method.usage)
def _build_native_stub_function_definitions(murano_method): runtime_version = murano_method.declaring_type.package.runtime_version engine = choose_yaql_engine(runtime_version) @specs.method @specs.name(murano_method.name) @specs.meta(constants.META_MURANO_METHOD, murano_method) @specs.parameter('__receiver', yaqltypes.NotOfType( dsl_types.MuranoTypeReference)) 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) @specs.method @specs.name(murano_method.name) @specs.meta(constants.META_MURANO_METHOD, murano_method) @specs.parameter('__receiver', yaqltypes.NotOfType( dsl_types.MuranoTypeReference)) 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) @specs.method @specs.name(murano_method.name) @specs.meta(constants.META_MURANO_METHOD, murano_method) @specs.parameter('__receiver', dsl_types.MuranoTypeReference) def static_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) if murano_method.usage in dsl_types.MethodUsages.InstanceMethods: return specs.get_function_definition(payload), None elif murano_method.usage == dsl_types.MethodUsages.Static: return (specs.get_function_definition(payload), specs.get_function_definition(static_payload)) elif murano_method.usage == dsl_types.MethodUsages.Extension: return (specs.get_function_definition(extension_payload), specs.get_function_definition(static_payload)) else: raise ValueError('Unknown method usage ' + murano_method.usage)
def get_function_definition(func): body = func param_type_func = lambda name: _infer_parameter_type(name, None) is_method = False if inspect.ismethod(func): is_method = True body = func.im_func param_type_func = lambda name: _infer_parameter_type( name, func.im_class.__name__) fd = specs.get_function_definition(body, convention=CONVENTION, parameter_type_func=param_type_func) if is_method: fd.is_method = True fd.is_function = False fd.set_parameter(0, yaqltypes.PythonType(func.im_class), overwrite=True) name = getattr(func, '__murano_name', None) if name: fd.name = name fd.insert_parameter(specs.ParameterDefinition('?1', yaqltypes.Context(), 0)) def payload(__context, *args, **kwargs): with helpers.contextual(__context): return body(*args, **kwargs) fd.payload = payload return fd
def get_function_definition(func, murano_method): body = func param_type_func = lambda name: _infer_parameter_type(name, None) is_method = False if inspect.ismethod(func): is_method = True body = func.im_func param_type_func = lambda name: _infer_parameter_type( name, func.im_class.__name__) fd = specs.get_function_definition( body, convention=CONVENTION, parameter_type_func=param_type_func) if is_method: fd.is_method = True fd.is_function = False fd.set_parameter( 0, yaqltypes.PythonType(func.im_class), overwrite=True) name = getattr(func, '__murano_name', None) if name: fd.name = name fd.insert_parameter(specs.ParameterDefinition( '?1', yaqltypes.Context(), 0)) def payload(__context, *args, **kwargs): with helpers.contextual(__context): return body(*args, **kwargs) fd.payload = payload fd.meta[constants.META_MURANO_METHOD] = murano_method return fd
def get_function_definition(func, murano_method, original_name): cls = murano_method.declaring_type.extension_class param_type_func = \ lambda name: None if not cls else _infer_parameter_type( name, cls.__name__) body = func if (cls is None or helpers.inspect_is_method(cls, original_name) or helpers.inspect_is_classmethod(cls, original_name)): body = helpers.function(func) fd = specs.get_function_definition( body, convention=CONVENTION, parameter_type_func=param_type_func) fd.is_method = True fd.is_function = False if not cls or helpers.inspect_is_method(cls, original_name): fd.set_parameter( 0, dsl.MuranoObjectParameter(murano_method.declaring_type), overwrite=True) if cls and helpers.inspect_is_classmethod(cls, original_name): _remove_first_parameter(fd) body = func name = getattr(func, '__murano_name', None) if name: fd.name = name fd.insert_parameter(specs.ParameterDefinition( '?1', yaqltypes.Context(), 0)) is_static = cls and ( helpers.inspect_is_static(cls, original_name) or helpers.inspect_is_classmethod(cls, original_name)) if is_static: fd.insert_parameter(specs.ParameterDefinition( '?2', yaqltypes.PythonType(object), 1)) def payload(__context, __self, *args, **kwargs): with helpers.contextual(__context): __context[constants.CTX_NAMES_SCOPE] = \ murano_method.declaring_type return body(__self.extension, *args, **kwargs) def static_payload(__context, __receiver, *args, **kwargs): with helpers.contextual(__context): __context[constants.CTX_NAMES_SCOPE] = \ murano_method.declaring_type return body(*args, **kwargs) if is_static: fd.payload = static_payload else: fd.payload = payload fd.meta[constants.META_MURANO_METHOD] = murano_method return fd
def get_function_definition(func, murano_method, original_name): cls = murano_method.declaring_type.extension_class def param_type_func(name): return None if not cls else _infer_parameter_type(name, cls.__name__) body = func if (cls is None or helpers.inspect_is_method(cls, original_name) or helpers.inspect_is_classmethod(cls, original_name)): body = helpers.function(func) fd = specs.get_function_definition(body, convention=CONVENTION, parameter_type_func=param_type_func) fd.is_method = True fd.is_function = False if not cls or helpers.inspect_is_method(cls, original_name): fd.set_parameter(0, dsl.MuranoObjectParameter( murano_method.declaring_type), overwrite=True) if cls and helpers.inspect_is_classmethod(cls, original_name): _remove_first_parameter(fd) body = func name = getattr(func, '__murano_name', None) if name: fd.name = name fd.insert_parameter(specs.ParameterDefinition('?1', yaqltypes.Context(), 0)) is_static = cls and (helpers.inspect_is_static(cls, original_name) or helpers.inspect_is_classmethod(cls, original_name)) if is_static: fd.insert_parameter( specs.ParameterDefinition('?2', yaqltypes.PythonType(object), 1)) def payload(__context, __self, *args, **kwargs): with helpers.contextual(__context): __context[constants.CTX_NAMES_SCOPE] = \ murano_method.declaring_type return body(__self.extension, *args, **kwargs) def static_payload(__context, __receiver, *args, **kwargs): with helpers.contextual(__context): __context[constants.CTX_NAMES_SCOPE] = \ murano_method.declaring_type return body(*args, **kwargs) if is_static: fd.payload = static_payload else: fd.payload = payload fd.meta[constants.META_MURANO_METHOD] = murano_method return fd
def register_function(self, spec, *args, **kwargs): exclusive = kwargs.pop("exclusive", False) if not isinstance(spec, specs.FunctionDefinition) and six.callable(spec): spec = specs.get_function_definition(spec, *args, convention=self._convention, **kwargs) spec = self._import_function_definition(spec) if spec.is_method: if not spec.is_valid_method(): raise exceptions.InvalidMethodException(spec.name) self._functions.setdefault(spec.name, set()).add(spec) if exclusive: self._exclusive_funcs.add(spec.name)
def _build_native_wrapper_function_definition(murano_method): runtime_version = murano_method.murano_class.package.runtime_version engine = choose_yaql_engine(runtime_version) @specs.method @specs.name(murano_method.name) 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 murano_method.invoke(executor, __sender, args, kwargs, __context, True) return specs.get_function_definition(payload)
def _build_native_wrapper_function_definition(murano_method): runtime_version = murano_method.murano_class.package.runtime_version engine = choose_yaql_engine(runtime_version) @specs.method @specs.name(murano_method.name) @specs.meta(constants.META_MURANO_METHOD, murano_method) 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) return specs.get_function_definition(payload)
def register_function(self, spec, *args, **kwargs): exclusive = kwargs.pop('exclusive', False) if not isinstance(spec, specs.FunctionDefinition) and callable(spec): spec = specs.get_function_definition( spec, *args, convention=self._convention, **kwargs) spec = self._import_function_definition(spec) if spec.is_method: if not spec.is_valid_method(): raise exceptions.InvalidMethodException(spec.name) self._functions.setdefault(spec.name, set()).add(spec) if exclusive: self._exclusive_funcs.add(spec.name)
def test_function_definition(self): def func(a, b, *args, **kwargs): return a, b, args, kwargs fd = specs.get_function_definition(func) self.assertEqual( (1, 2, (5, 7), {'kw1': 'x', 'kw2': None}), fd(self.engine, self.context)( 1, 2, 5, 7, kw1='x', kw2=None)) self.assertEqual( (1, 5, (), {}), fd(self.engine, self.context)(1, b=5))
def test_function_definition(self): def func(a, b, *args, **kwargs): return a, b, args, kwargs fd = specs.get_function_definition(func) self.assertEqual((1, 2, (5, 7), { 'kw1': 'x', 'kw2': None }), fd(self.engine, self.context)(1, 2, 5, 7, kw1='x', kw2=None)) self.assertEqual((1, 5, (), {}), fd(self.engine, self.context)(1, b=5))
def test_function_in(self): def f(): pass def f_(): pass def f__(): pass context = contexts.Context() context2 = context.create_child_context() context3 = context2.create_child_context() context.register_function(f) context.register_function(f_) context3.register_function(f__) functions = context3.collect_functions("f") self.assertNotIn(specs.get_function_definition(f__), context3) self.assertIn(functions[0].pop(), context3) self.assertNotIn(functions[1].pop(), context3)
def test_function_in(self): def f(): pass def f_(): pass def f__(): pass context = contexts.Context() context2 = context.create_child_context() context3 = context2.create_child_context() context.register_function(f) context.register_function(f_) context3.register_function(f__) functions = context3.collect_functions('f') self.assertNotIn(specs.get_function_definition(f__), context3) self.assertIn(functions[0].pop(), context3) self.assertNotIn(functions[1].pop(), context3)
def wrapper(func): fd = specs.get_function_definition(func) mpl_meta = fd.meta.get(constants.META_MPL_META, []) mpl_meta.append({type_name: value}) specs.meta(type_name, mpl_meta)(func)
def wrapper(func): fd = specs.get_function_definition(func) mpl_meta = fd.meta.get(constants.META_MPL_META, []) mpl_meta.append({type_name: value}) specs.meta(type_name, mpl_meta)(func)