<commit_before>
#
# rpclib - Copyright (C) Rpclib 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
#

"""This module contains the implementation of a subset of the SOAP 1.1 remote
procedure call standard.
"""

import logging
logger = logging.getLogger(__name__)

import cgi

import rpclib.const.xml_ns as ns

from lxml import etree
from lxml.etree import XMLSyntaxError

from rpclib.const.http import HTTP_405
from rpclib.const.http import HTTP_500
from rpclib.const.ansi_color import LIGHT_GREEN
from rpclib.const.ansi_color import LIGHT_RED
from rpclib.const.ansi_color import END_COLOR
from rpclib.error import RequestNotAllowed
from rpclib.model.fault import Fault
from rpclib.protocol.xml import XmlObject
from rpclib.protocol.soap.mime import collapse_swa

def _from_soap(in_envelope_xml, xmlids=None):
    '''Parses the xml string into the header and payload.'''

    if xmlids:
        resolve_hrefs(in_envelope_xml, xmlids)

    if in_envelope_xml.tag != '{%s}Envelope' % ns.soap_env:
        raise Fault('Client.SoapError', 'No {%s}Envelope element was found!' %
                                                            ns.soap_env)

    header_envelope = in_envelope_xml.xpath('e:Header',
                                          namespaces={'e': ns.soap_env})
    body_envelope = in_envelope_xml.xpath('e:Body',
                                          namespaces={'e': ns.soap_env})

    if len(header_envelope) == 0 and len(body_envelope) == 0:
        raise Fault('Client.SoapError', 'Soap envelope is empty!' %
                                                            ns.soap_env)

    header=None
    if len(header_envelope) > 0 and len(header_envelope[0]) > 0:
        header = header_envelope[0].getchildren()

    body=None
    if len(body_envelope) > 0 and len(body_envelope[0]) > 0:
        body = body_envelope[0].getchildren()[0]

    return header, body

def _parse_xml_string(xml_string, charset=None):
    if charset:
        string = ''.join([s.decode(charset) for s in xml_string])
    else:
        string = ''.join(xml_string)

    try:
        try:
            root, xmlids = etree.XMLID(string)

        except XMLSyntaxError, e:
            logger.error(string)
            raise Fault('Client.XMLSyntaxError', str(e))
<commit_msg>simplify soap envelop decompose code.<commit_after>
#
# rpclib - Copyright (C) Rpclib 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
#

"""This module contains the implementation of a subset of the SOAP 1.1 remote
procedure call standard.
"""

import logging
logger = logging.getLogger(__name__)

import cgi

import rpclib.const.xml_ns as ns

from lxml import etree
from lxml.etree import XMLSyntaxError

from rpclib.const.http import HTTP_405
from rpclib.const.http import HTTP_500
from rpclib.const.ansi_color import LIGHT_GREEN
from rpclib.const.ansi_color import LIGHT_RED
from rpclib.const.ansi_color import END_COLOR
from rpclib.error import RequestNotAllowed
from rpclib.model.fault import Fault
from rpclib.protocol.xml import XmlObject
from rpclib.protocol.soap.mime import collapse_swa

def _from_soap(in_envelope_xml, xmlids=None):
    '''Parses the xml string into the header and payload.'''

    if xmlids:
        resolve_hrefs(in_envelope_xml, xmlids)

    if in_envelope_xml.tag != '{%s}Envelope' % ns.soap_env:
        raise Fault('Client.SoapError', 'No {%s}Envelope element was found!' %
                                                            ns.soap_env)

    header_envelope = in_envelope_xml.xpath('e:Header',
                                          namespaces={'e': ns.soap_env})
    body_envelope = in_envelope_xml.xpath('e:Body',
                                          namespaces={'e': ns.soap_env})

    if len(header_envelope) == 0 and len(body_envelope) == 0:
        raise Fault('Client.SoapError', 'Soap envelope is empty!' %
                                                            ns.soap_env)

    header=None
    if len(header_envelope) > 0:
        header = header_envelope[0].getchildren()

    body=None
    if len(body_envelope) > 0 and len(body_envelope[0]) > 0:
        body = body_envelope[0][0]

    return header, body

def _parse_xml_string(xml_string, charset=None):
    if charset:
        string = ''.join([s.decode(charset) for s in xml_string])
    else:
        string = ''.join(xml_string)

    try:
        try:
            root, xmlids = etree.XMLID(string)

        except XMLSyntaxError, e:
            logger.error(string)
            raise Fault('Client.XMLSyntaxError', str(e))

{'fraction_non_alphanumeric': 0.08282520325203252, 'fraction_numerical': 0.010501355013550135, 'mean_word_length': 2.8670595939751147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 36}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4415767', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1715, 'n_words': 575}