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 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 _register_native_class(self, cls, name): if cls in self._imported_types: return self._classes[name] try: m_class = self.find_class(name, False) except exceptions.NoClassFound: m_class = self._register_mpl_classes({'Name': name}, name) m_class.extension_class = cls for method_name in dir(cls): if method_name.startswith('_'): continue method = getattr(cls, method_name) if not any((helpers.inspect_is_method(cls, method_name), helpers.inspect_is_static(cls, method_name), helpers.inspect_is_classmethod(cls, method_name))): continue method_name_alias = (getattr(method, '__murano_name', None) or specs.convert_function_name( method_name, yaql_integration.CONVENTION)) m_class.add_method(method_name_alias, method, method_name) self._imported_types.add(cls) return m_class
def _register_native_class(self, cls, name): if cls in self._imported_types: return self._classes[name] try: m_class = self.find_class(name, False) except exceptions.NoClassFound: m_class = self._register_mpl_classes({'Name': name}, name) m_class.extension_class = cls for method_name in dir(cls): if method_name.startswith('_'): continue method = getattr(cls, method_name) if not any(( helpers.inspect_is_method(cls, method_name), helpers.inspect_is_static(cls, method_name), helpers.inspect_is_classmethod(cls, method_name))): continue method_name_alias = (getattr( method, '__murano_name', None) or specs.convert_function_name( method_name, yaql_integration.CONVENTION)) m_class.add_method(method_name_alias, method, method_name) self._imported_types.add(cls) return m_class
def test_inspect_is_method(self): mock_cls = mock.Mock(foo=lambda: None, bar=None) self.assertTrue(helpers.inspect_is_method(mock_cls, 'foo')) self.assertFalse(helpers.inspect_is_method(mock_cls, 'bar'))