示例#1
0
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
示例#2
0
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)

    if '__init__' in cls.__dict__:
        fd = specs.get_function_definition(
            helpers.function(cls.__init__),
            parameter_type_func=lambda name: _infer_parameter_type(
                name, cls.__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
示例#3
0
    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
示例#4
0
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
示例#5
0
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