<commit_before>
#
# soaplib - Copyright (C) Soaplib contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#

import logging
logger = logging.getLogger(__name__)

import soaplib
from lxml import etree

from soaplib import MethodDescriptor
from soaplib.model.clazz import ClassSerializer as Message
from soaplib.model.clazz import ClassSerializerMeta as MessageMeta
from soaplib.model.clazz import TypeInfo
from soaplib.model.primitive import Any

_pref_wsa = soaplib.const_prefmap[soaplib.ns_wsa]

def _produce_input_message(ns, f, params, kparams):
    _in_message = kparams.get('_in_message', f.func_name)
    _in_variable_names = kparams.get('_in_variable_names', {})

    arg_count = f.func_code.co_argcount
    param_names = f.func_code.co_varnames[1:arg_count]

    try:
        in_params = TypeInfo()

        for i in range(len(params)):
            e0 = _in_variable_names.get(param_names[i], param_names[i])
            e1 = params[i]

            in_params[e0] = e1

    except IndexError, e:
        raise Exception("%s has parameter numbers mismatching" % f.func_name)

    message=Message.produce(type_name=_in_message, namespace=ns,
                                            members=in_params)
    message.__namespace__ = ns
    message.resolve_namespace(message, ns)

    return message

def _produce_rpc_output_message(ns, f, params, kparams):
    _returns = kparams.get('_returns')

    _out_message = kparams.get('_out_message', '%sResponse' % f.func_name)

    out_params = TypeInfo()

    if _returns:
        if isinstance(_returns, (list, tuple)):
            default_names = ['%sResult%d' % (f.func_name, 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',
                                                       '%sResult' % f.func_name)

            out_params[_out_variable_name] = _returns

    message=Message.produce(type_name=_out_message, namespace=ns,
                                                             members=out_params)
    message.__namespace__ = ns
    message.resolve_namespace(message, ns)

    return message


class Alias(Message):
    """New type_name, same type_info.
    """
    @classmethod
    def add_to_schema(cls, schema_dict):
        if not schema_dict.has_class(cls._target):
            cls._target.add_to_schema(schema_dict)
        element = etree.Element('{%s}element' % soaplib.ns_xsd)
        element.set('name',cls.get_type_name())
        element.set('type',cls._target.get_type_name_ns(schema_dict.app))

        schema_dict.add_element(cls, element)

def _makeAlias(type_name, namespace, target):
    """ Return an alias class for the given target class.

    This function is a variation on 'ClassSerializer.produce'.

    The alias will borrow the target's typeinfo.
    """
    cls_dict = {}

    cls_dict['__namespace__'] = namespace
    cls_dict['__type_name__'] = type_name
    cls_dict['_type_info'] = target._type_info
    cls_dict['_target'] = target

    return MessageMeta(type_name, (Alias,), cls_dict)

def _produce_document_output_message(ns, f, params, kparams):
    """Generate an output message for "document"-style API methods.

    This message is just an alias for the declared return type.
    """
    _returns = kparams.get('_returns', Any)
    _out_message = kparams.get('_out_message', '%sResponse' % f.func_name)

    message = _makeAlias(_out_message, ns, _returns)
    message.resolve_namespace(message, ns)

    return message

def rpc(*params, **kparams):
    '''<commit_msg>Bugfix:  cope with methods which don't declare a return type.<commit_after>
#
# soaplib - Copyright (C) Soaplib contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#

import logging
logger = logging.getLogger(__name__)

import soaplib
from lxml import etree

from soaplib import MethodDescriptor
from soaplib.model.clazz import ClassSerializer as Message
from soaplib.model.clazz import ClassSerializerMeta as MessageMeta
from soaplib.model.clazz import TypeInfo
from soaplib.model.primitive import Any

_pref_wsa = soaplib.const_prefmap[soaplib.ns_wsa]

def _produce_input_message(ns, f, params, kparams):
    _in_message = kparams.get('_in_message', f.func_name)
    _in_variable_names = kparams.get('_in_variable_names', {})

    arg_count = f.func_code.co_argcount
    param_names = f.func_code.co_varnames[1:arg_count]

    try:
        in_params = TypeInfo()

        for i in range(len(params)):
            e0 = _in_variable_names.get(param_names[i], param_names[i])
            e1 = params[i]

            in_params[e0] = e1

    except IndexError, e:
        raise Exception("%s has parameter numbers mismatching" % f.func_name)

    message=Message.produce(type_name=_in_message, namespace=ns,
                                            members=in_params)
    message.__namespace__ = ns
    message.resolve_namespace(message, ns)

    return message

def _produce_rpc_output_message(ns, f, params, kparams):
    _returns = kparams.get('_returns')

    _out_message = kparams.get('_out_message', '%sResponse' % f.func_name)

    out_params = TypeInfo()

    if _returns:
        if isinstance(_returns, (list, tuple)):
            default_names = ['%sResult%d' % (f.func_name, 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',
                                                       '%sResult' % f.func_name)

            out_params[_out_variable_name] = _returns

    message=Message.produce(type_name=_out_message, namespace=ns,
                                                             members=out_params)
    message.__namespace__ = ns
    message.resolve_namespace(message, ns)

    return message


class Alias(Message):
    """New type_name, same type_info.
    """
    @classmethod
    def add_to_schema(cls, schema_dict):
        if not schema_dict.has_class(cls._target):
            cls._target.add_to_schema(schema_dict)
        element = etree.Element('{%s}element' % soaplib.ns_xsd)
        element.set('name',cls.get_type_name())
        element.set('type',cls._target.get_type_name_ns(schema_dict.app))

        schema_dict.add_element(cls, element)

def _makeAlias(type_name, namespace, target):
    """ Return an alias class for the given target class.

    This function is a variation on 'ClassSerializer.produce'.

    The alias will borrow the target's typeinfo.
    """
    cls_dict = {}

    cls_dict['__namespace__'] = namespace
    cls_dict['__type_name__'] = type_name
    cls_dict['_type_info'] = getattr(target, '_type_info', ())
    cls_dict['_target'] = target

    return MessageMeta(type_name, (Alias,), cls_dict)

def _produce_document_output_message(ns, f, params, kparams):
    """Generate an output message for "document"-style API methods.

    This message is just an alias for the declared return type.
    """
    _returns = kparams.get('_returns', Any)
    _out_message = kparams.get('_out_message', '%sResponse' % f.func_name)

    message = _makeAlias(_out_message, ns, _returns)
    message.resolve_namespace(message, ns)

    return message

def rpc(*params, **kparams):
    '''
{'fraction_non_alphanumeric': 0.08959412780656303, 'fraction_numerical': 0.0038860103626943004, 'mean_word_length': 2.8846960167714886, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6474809', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2667, 'n_words': 730}