Exemplo n.º 1
0
def _produce_output_message(func_name, kparams):
    """Generate an output message for "rpc"-style API methods.

    This message is a wrapper to the declared return type.
    """

    _returns = kparams.get('_returns')
    _body_style = _validate_body_style(kparams)

    # FIXME: Remove after detecting all broken code
    _out_body_bare = kparams.get("_out_body_bare", 0xcc)
    assert _out_body_bare == 0xcc

    _out_message_name = kparams.get(
        '_out_message_name', '%s%s' % (func_name, spyne.const.RESPONSE_SUFFIX))

    out_params = TypeInfo()

    if _returns and _body_style == 'wrapped':
        if isinstance(_returns, (list, tuple)):
            default_names = [
                '%s%s%d' % (func_name, spyne.const.RESULT_SUFFIX, i)
                for i in range(len(_returns))
            ]

            _out_variable_names = kparams.get('_out_variable_names',
                                              default_names)

            assert (len(_returns) == len(_out_variable_names))

            var_pair = zip(_out_variable_names, _returns)
            out_params = TypeInfo(var_pair)

        else:
            _out_variable_name = kparams.get(
                '_out_variable_name',
                '%s%s' % (func_name, spyne.const.RESULT_SUFFIX))

            out_params[_out_variable_name] = _returns

    ns = spyne.const.xml_ns.DEFAULT_NS
    if _out_message_name.startswith("{"):
        ns = _out_message_name[1:].partition("}")[0]

    if _body_style.endswith('bare') and _returns is not None:
        message = _returns.customize(sub_name=_out_message_name, sub_ns=ns)
        if message.__type_name__ is ModelBase.Empty:
            message.__type_name__ = _out_message_name

    else:
        message = ComplexModel.produce(type_name=_out_message_name,
                                       namespace=ns,
                                       members=out_params)

        message.Attributes._wrapper = True
        message.__namespace__ = ns  # FIXME: is this necessary?

    return message
Exemplo n.º 2
0
def _produce_output_message(func_name, kparams):
    """Generate an output message for "rpc"-style API methods.

    This message is a wrapper to the declared return type.
    """

    _returns = kparams.get('_returns')
    _body_style = _validate_body_style(kparams)

    # FIXME: Remove after detecting all broken code
    _out_body_bare = kparams.get("_out_body_bare", 0xcc)
    assert _out_body_bare == 0xcc

    _out_message_name = kparams.get('_out_message_name', '%s%s' %
                                       (func_name, spyne.const.RESPONSE_SUFFIX))

    out_params = TypeInfo()

    if _returns and _body_style == 'wrapped':
        if isinstance(_returns, (list, tuple)):
            default_names = ['%s%s%d'% (func_name, spyne.const.RESULT_SUFFIX, i)
                                                  for i in range(len(_returns))]

            _out_variable_names = kparams.get('_out_variable_names',
                                                                  default_names)

            assert (len(_returns) == len(_out_variable_names))

            var_pair = zip(_out_variable_names, _returns)
            out_params = TypeInfo(var_pair)

        else:
            _out_variable_name = kparams.get('_out_variable_name',
                                '%s%s' % (func_name, spyne.const.RESULT_SUFFIX))

            out_params[_out_variable_name] = _returns

    ns = spyne.const.xml_ns.DEFAULT_NS
    if _out_message_name.startswith("{"):
        ns = _out_message_name[1:].partition("}")[0]

    if _body_style.endswith('bare') and _returns is not None:
        message = _returns.customize(sub_name=_out_message_name, sub_ns=ns)
        if message.__type_name__ is ModelBase.Empty:
            message.__type_name__ = _out_message_name

    else:
        message = ComplexModel.produce(type_name=_out_message_name,
                                               namespace=ns, members=out_params)

        message.Attributes._wrapper = True
        message.__namespace__ = ns  # FIXME: is this necessary?

    return message
Exemplo n.º 3
0
def _produce_input_message(f, params, kparams, in_message_name,
                           in_variable_names, no_ctx, no_self, args):
    _body_style = _validate_body_style(kparams)

    arg_start = 0
    if no_ctx is False:
        arg_start += 1
    if no_self is False:
        arg_start += 1

    if args is None:
        try:
            argcount = f.__code__.co_argcount
            args = f.__code__.co_varnames[arg_start:argcount]

        except AttributeError:
            raise TypeError(
                "It's not possible to instrospect builtins. You must pass a "
                "sequence of argument names as the '_args' argument to the "
                "rpc decorator to manually denote the arguments that this "
                "function accepts.")

        if len(params) != len(args):
            raise Exception("%r function has %d argument(s) but its decorator "
                            "has %d." % (f.__name__, len(args), len(params)))

    else:
        args = copy(args)
        if len(params) != len(args):
            raise Exception("%r function has %d argument(s) but the _args "
                            "argument has %d." %
                            (f.__name__, len(args), len(params)))

    in_params = TypeInfo()
    for k, v in zip(args, params):
        k = in_variable_names.get(k, k)
        in_params[k] = v

    ns = spyne.const.xml_ns.DEFAULT_NS
    if in_message_name.startswith("{"):
        ns, _, in_message_name = in_message_name[1:].partition("}")

    message = None
    if _body_style == 'bare':
        if len(in_params) > 1:
            raise Exception(
                "body_style='bare' can handle at most one function "
                "argument.")
        if len(in_params) == 0:
            message = ComplexModel.produce(type_name=in_message_name,
                                           namespace=ns,
                                           members=in_params)
        else:
            message, = in_params.values()
            message = message.customize(sub_name=in_message_name, sub_ns=ns)
            from spyne.model import ComplexModelBase
            if issubclass(message,
                          ComplexModelBase) and not message._type_info:
                raise Exception("body_style='bare' does not allow empty "
                                "model as param")

    else:
        message = ComplexModel.produce(type_name=in_message_name,
                                       namespace=ns,
                                       members=in_params)
        message.__namespace__ = ns

    return message
Exemplo n.º 4
0
def _produce_input_message(f, params, in_message_name, in_variable_names,
                       no_ctx, no_self, argnames, body_style_str, self_ref_cls,
                       in_wsdl_part_name):
    arg_start = 0
    if no_ctx is False:
        arg_start += 1
    if no_self is False:
        arg_start += 1

    if argnames is None:
        try:
            argcount = f.__code__.co_argcount
            argnames = f.__code__.co_varnames[arg_start:argcount]

        except AttributeError:
            raise TypeError(
                "It's not possible to instrospect builtins. You must pass a "
                "sequence of argument names as the '_args' argument to the "
                "rpc decorator to manually denote the arguments that this "
                "function accepts."
            )

        if no_self is False:
            params = [self_ref_cls.novalidate_freq()] + params
            argnames = ('self',) + argnames

        if len(params) != len(argnames):
            raise LogicError("%r function has %d argument(s) but its decorator "
                           "has %d." % (f.__name__, len(argnames), len(params)))

    else:
        argnames = copy(argnames)
        if len(params) != len(argnames):
            raise LogicError("%r function has %d argument(s) but the _args "
                            "argument has %d." % (
                                f.__name__, len(argnames), len(params)))

    in_params = TypeInfo()
    from spyne import SelfReference
    for k, v in zip(argnames, params):
        try:
            is_self_ref = issubclass(v, SelfReference)
        except TypeError:
            is_self_ref = False

        if is_self_ref:
            if no_self is False:
                raise LogicError("SelfReference can't be used in @rpc")
            v = recust_selfref(v, self_ref_cls)

        k = in_variable_names.get(k, k)
        in_params[k] = v

    ns = spyne.const.xml.DEFAULT_NS
    if in_message_name.startswith("{"):
        ns, _, in_message_name = in_message_name[1:].partition("}")

    message = None
    if body_style_str == 'bare':
        if len(in_params) > 1:
            raise LogicError("body_style='bare' can handle at most one "
                                                           "function argument.")

        if len(in_params) == 0:
            message = ComplexModel.produce(type_name=in_message_name,
                                           namespace=ns, members=in_params)
        else:
            message, = in_params.values()
            message = message.customize(sub_name=in_message_name, sub_ns=ns)

            if issubclass(message, ComplexModelBase) and not message._type_info:
                raise LogicError("body_style='bare' does not allow empty "
                                                               "model as param")

            # there can't be multiple arguments here.
            if message.__type_name__ is ModelBase.Empty:
                message._fill_empty_type_name(ns, in_message_name,
                                                    "%s_arg0" % in_message_name)

    else:
        message = ComplexModel.produce(type_name=in_message_name,
                                       namespace=ns, members=in_params)
        message.__namespace__ = ns
    
    if in_wsdl_part_name:
        message = message.customize(wsdl_part_name=in_wsdl_part_name)

    return message
Exemplo n.º 5
0
def _produce_output_message(func_name, body_style_str, self_ref_cls,
                                                              no_self, kparams):
    """Generate an output message for "rpc"-style API methods.

    This message is a wrapper to the declared return type.
    """

    _returns = kparams.pop('_returns', None)

    try:
        is_self_ref = issubclass(_returns, SelfReference)
    except TypeError:
        is_self_ref = False

    if is_self_ref:
        if no_self is False:
            raise LogicError("SelfReference can't be used in @rpc")

        _returns = recust_selfref(_returns, self_ref_cls)

    _is_out_message_name_overridden = not ('_out_message_name' in kparams)
    _out_message_name = kparams.pop('_out_message_name', '%s%s' %
                                       (func_name, spyne.const.RESPONSE_SUFFIX))

    if no_self is False and \
               (body_style_str == 'wrapped' or _is_out_message_name_overridden):
        _out_message_name = '%s.%s' % \
                               (self_ref_cls.get_type_name(), _out_message_name)
    
    _out_wsdl_part_name = kparams.pop('_wsdl_part_name', None)

    out_params = TypeInfo()

    if _returns and body_style_str == 'wrapped':
        if isinstance(_returns, (list, tuple)):
            default_names = ['%s%s%d'% (func_name, spyne.const.RESULT_SUFFIX, i)
                                                  for i in range(len(_returns))]

            _out_variable_names = kparams.pop('_out_variable_names',
                                                                  default_names)

            assert (len(_returns) == len(_out_variable_names))

            var_pair = zip(_out_variable_names, _returns)
            out_params = TypeInfo(var_pair)

        else:
            _out_variable_name = kparams.pop('_out_variable_name',
                                '%s%s' % (func_name, spyne.const.RESULT_SUFFIX))

            out_params[_out_variable_name] = _returns

    ns = spyne.const.xml.DEFAULT_NS
    if _out_message_name.startswith("{"):
        _out_message_name_parts = _out_message_name[1:].partition("}")
        ns = _out_message_name_parts[0]  # skip index 1, it is the closing '}'
        _out_message_name = _out_message_name_parts[2]
        
    if body_style_str.endswith('bare') and _returns is not None:
        message = _returns.customize(sub_name=_out_message_name, sub_ns=ns)
        if message.__type_name__ is ModelBase.Empty:
            message.__type_name__ = _out_message_name

    else:
        message = ComplexModel.produce(type_name=_out_message_name,
                                               namespace=ns, members=out_params)

        message.Attributes._wrapper = True
        message.__namespace__ = ns  # FIXME: is this necessary?
    
    if _out_wsdl_part_name:
        message = message.customize(wsdl_part_name=_out_wsdl_part_name)

    return message
Exemplo n.º 6
0
def _produce_input_message(f, params, in_message_name, in_variable_names,
                           no_ctx, no_self, argnames, body_style_str,
                           self_ref_cls):
    arg_start = 0
    if no_ctx is False:
        arg_start += 1
    if no_self is False:
        arg_start += 1

    if argnames is None:
        try:
            argcount = f.__code__.co_argcount
            argnames = f.__code__.co_varnames[arg_start:argcount]

        except AttributeError:
            raise TypeError(
                "It's not possible to instrospect builtins. You must pass a "
                "sequence of argument names as the '_args' argument to the "
                "rpc decorator to manually denote the arguments that this "
                "function accepts.")

        if no_self is False:
            params = [self_ref_cls.novalidate_freq()] + params
            argnames = ('self', ) + argnames

        if len(params) != len(argnames):
            raise LogicError(
                "%r function has %d argument(s) but its decorator "
                "has %d." % (f.__name__, len(argnames), len(params)))

    else:
        argnames = copy(argnames)
        if len(params) != len(argnames):
            raise LogicError("%r function has %d argument(s) but the _args "
                             "argument has %d." %
                             (f.__name__, len(argnames), len(params)))

    in_params = TypeInfo()
    from spyne import SelfReference
    for k, v in zip(argnames, params):
        try:
            is_self_ref = issubclass(v, SelfReference)
        except TypeError:
            is_self_ref = False

        if is_self_ref:
            if no_self is False:
                raise LogicError("SelfReference can't be used in @rpc")
            v = recust_selfref(v, self_ref_cls)

        k = in_variable_names.get(k, k)
        in_params[k] = v

    ns = spyne.const.xml.DEFAULT_NS
    if in_message_name.startswith("{"):
        ns, _, in_message_name = in_message_name[1:].partition("}")

    message = None
    if body_style_str == 'bare':
        if len(in_params) > 1:
            raise LogicError("body_style='bare' can handle at most one "
                             "function argument.")

        if len(in_params) == 0:
            message = ComplexModel.produce(type_name=in_message_name,
                                           namespace=ns,
                                           members=in_params)
        else:
            message, = in_params.values()
            message = message.customize(sub_name=in_message_name, sub_ns=ns)

            if issubclass(message,
                          ComplexModelBase) and not message._type_info:
                raise LogicError("body_style='bare' does not allow empty "
                                 "model as param")

            # there can't be multiple arguments here.
            if message.__type_name__ is ModelBase.Empty:
                message._fill_empty_type_name(ns, in_message_name,
                                              "%s_arg0" % in_message_name)

    else:
        message = ComplexModel.produce(type_name=in_message_name,
                                       namespace=ns,
                                       members=in_params)
        message.__namespace__ = ns

    return message
Exemplo n.º 7
0
def _produce_output_message(func_name, body_style_str, self_ref_cls, no_self,
                            kparams):
    """Generate an output message for "rpc"-style API methods.

    This message is a wrapper to the declared return type.
    """

    _returns = kparams.pop('_returns', None)

    try:
        is_self_ref = issubclass(_returns, SelfReference)
    except TypeError:
        is_self_ref = False

    if is_self_ref:
        if no_self is False:
            raise LogicError("SelfReference can't be used in @rpc")

        _returns = recust_selfref(_returns, self_ref_cls)

    _is_out_message_name_overridden = not ('_out_message_name' in kparams)
    _out_message_name = kparams.pop(
        '_out_message_name', '%s%s' % (func_name, spyne.const.RESPONSE_SUFFIX))

    if no_self is False and \
               (body_style_str == 'wrapped' or _is_out_message_name_overridden):
        _out_message_name = '%s.%s' % \
                               (self_ref_cls.get_type_name(), _out_message_name)

    out_params = TypeInfo()

    if _returns and body_style_str == 'wrapped':
        if isinstance(_returns, (list, tuple)):
            default_names = [
                '%s%s%d' % (func_name, spyne.const.RESULT_SUFFIX, i)
                for i in range(len(_returns))
            ]

            _out_variable_names = kparams.pop('_out_variable_names',
                                              default_names)

            assert (len(_returns) == len(_out_variable_names))

            var_pair = zip(_out_variable_names, _returns)
            out_params = TypeInfo(var_pair)

        else:
            _out_variable_name = kparams.pop(
                '_out_variable_name',
                '%s%s' % (func_name, spyne.const.RESULT_SUFFIX))

            out_params[_out_variable_name] = _returns

    ns = spyne.const.xml.DEFAULT_NS
    if _out_message_name.startswith("{"):
        _out_message_name_parts = _out_message_name[1:].partition("}")
        ns = _out_message_name_parts[0]  # skip index 1, it is the closing '}'
        _out_message_name = _out_message_name_parts[2]

    if body_style_str.endswith('bare') and _returns is not None:
        message = _returns.customize(sub_name=_out_message_name, sub_ns=ns)
        if message.__type_name__ is ModelBase.Empty:
            message.__type_name__ = _out_message_name

    else:
        message = ComplexModel.produce(type_name=_out_message_name,
                                       namespace=ns,
                                       members=out_params)

        message.Attributes._wrapper = True
        message.__namespace__ = ns  # FIXME: is this necessary?

    return message
Exemplo n.º 8
0
def _produce_input_message(f, params, kparams, in_message_name,
                           in_variable_names, no_ctx, no_self, args):
    _body_style = _validate_body_style(kparams)

    arg_start = 0
    if no_ctx is False:
        arg_start += 1
    if no_self is False:
        arg_start += 1

    if args is None:
        try:
            argcount = f.__code__.co_argcount
            args = f.__code__.co_varnames[arg_start:argcount]

        except AttributeError:
            raise TypeError(
                "It's not possible to instrospect builtins. You must pass a "
                "sequence of argument names as the '_args' argument to the "
                "rpc decorator to manually denote the arguments that this "
                "function accepts."
            )

        if len(params) != len(args):
            raise Exception("%r function has %d argument(s) but its decorator "
                            "has %d." % (f.__name__, len(args), len(params)))

    else:
        args = copy(args)
        if len(params) != len(args):
            raise Exception("%r function has %d argument(s) but the _args "
                            "argument has %d." % (
                            f.__name__, len(args), len(params)))

    in_params = TypeInfo()
    for k, v in zip(args, params):
        k = in_variable_names.get(k, k)
        in_params[k] = v

    ns = spyne.const.xml_ns.DEFAULT_NS
    if in_message_name.startswith("{"):
        ns, _, in_message_name = in_message_name[1:].partition("}")

    message = None
    if _body_style == 'bare':
        if len(in_params) > 1:
            raise Exception("body_style='bare' can handle at most one function "
                            "argument.")
        if len(in_params) == 0:
            message = ComplexModel.produce(type_name=in_message_name,
                                           namespace=ns, members=in_params)
        else:
            message, = in_params.values()
            message = message.customize(sub_name=in_message_name, sub_ns=ns)
            from spyne.model import ComplexModelBase
            if issubclass(message, ComplexModelBase) and not message._type_info:
                raise Exception("body_style='bare' does not allow empty "
                                "model as param")

    else:
        message = ComplexModel.produce(type_name=in_message_name,
                                       namespace=ns, members=in_params)
        message.__namespace__ = ns

    return message