<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 cStringIO
import soaplib
from lxml import etree
from soaplib.serializers import Base

_ns_xs = soaplib.nsmap['xs']
_ns_xsi = soaplib.nsmap['xsi']

class Fault(Exception, Base):
    def __init__(self, faultcode = 'Server', faultstring = None,
                 detail = None, name = 'ExceptionFault'):
        self.faultcode = faultcode
        self.faultstring = faultstring
        self.detail = detail
        self.name = name

    @classmethod
    def to_xml(cls, value, name):
        fault = etree.Element(name)

        etree.SubElement(fault, 'faultcode').text = value.faultcode
        etree.SubElement(fault, 'faultstring').text = value.faultstring
        etree.SubElement(fault, 'detail').text = value.detail

        return fault

    @classmethod
    def from_xml(cls, element):
        code = element.find('faultcode').text
        string = element.find('faultstring').text
        detail_element = element.find('detail')
        if detail_element is not None:
            if len(detail_element.getchildren()):
                detail = etree.tostring(detail_element)
            else:
                detail = element.find('detail').text
        else:
            detail = ''
        return Fault(faultcode=code, faultstring=string, detail=detail)

    @classmethod
    def add_to_schema(self, schema_dict):
        complex_type = etree.Element('complexType')
        complex_type.set('name', self.get_datatype())
        sequenceNode = etree.SubElement(complex_type, 'sequence')

        element = etree.SubElement(sequenceNode, 'element')
        element.set('name', 'detail')
        element.set('{%s}type' % _ns_xsi, '{%s}string' % _ns_xs)

        element = etree.SubElement(sequenceNode, 'element')
        element.set('name', 'message')
        element.set('{%s}type' % _ns_xsi, '{%s}string' % _ns_xs)

        schema_dict[self.type_name_ns] = complex_type

        typeElementItem = etree.Element('element')
        typeElementItem.set('name', 'ExceptionFaultType')
        typeElementItem.set('{%s}type' % _ns_xsi, self.get_type_name_ns())
        schema_dict['%sElement' % (self.type_name_ns)] = typeElementItem

    def __str__(self):
        io = cStringIO.StringIO()
        io.write("*" * 80)
        io.write("\r\n")
        io.write(" Recieved soap fault \r\n")
        io.write(" FaultCode            %s \r\n" % self.faultcode)
        io.write(" FaultString          %s \r\n" % self.faultstring)
        io.write(" FaultDetail          \r\n")

        if self.detail is not None:
            io.write(self.detail)

        return io.getvalue()
<commit_msg>fixed some obvious errors. need to add exception tests.<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 cStringIO
import soaplib
from lxml import etree
from soaplib.serializers import Base

_ns_xs = soaplib.nsmap['xs']
_ns_xsi = soaplib.nsmap['xsi']

class Fault(Exception, Base):
    def __init__(self, faultcode = 'Server', faultstring = None,
                 detail = None, name = 'ExceptionFault'):
        self.faultcode = faultcode
        self.faultstring = faultstring
        self.detail = detail
        self.name = name

    @classmethod
    def to_xml(cls, value, name):
        fault = etree.Element(name)

        etree.SubElement(fault, 'faultcode').text = value.faultcode
        etree.SubElement(fault, 'faultstring').text = value.faultstring
        etree.SubElement(fault, 'detail').text = value.detail

        return fault

    @classmethod
    def from_xml(cls, element):
        code = element.find('faultcode').text
        string = element.find('faultstring').text
        detail_element = element.find('detail')
        if detail_element is not None:
            if len(detail_element.getchildren()):
                detail = etree.tostring(detail_element)
            else:
                detail = element.find('detail').text
        else:
            detail = ''
        return Fault(faultcode=code, faultstring=string, detail=detail)

    @classmethod
    def add_to_schema(cls, schema_dict):
        complex_type = etree.Element('complexType')
        complex_type.set('name', cls.get_datatype())
        sequenceNode = etree.SubElement(complex_type, 'sequence')

        element = etree.SubElement(sequenceNode, 'element')
        element.set('name', 'detail')
        element.set('{%s}type' % _ns_xsi, 'xs:string')

        element = etree.SubElement(sequenceNode, 'element')
        element.set('name', 'message')
        element.set('{%s}type' % _ns_xsi, 'xs:string')

        schema_dict.add_complex_type(cls, complex_type)

        top_level_element = etree.Element('element')
        top_level_element.set('name', 'ExceptionFaultType')
        top_level_element.set('{%s}type' % _ns_xsi, cls.get_type_name_ns())

        schema_dict.add_element(cls, top_level_element)

    def __str__(self):
        io = cStringIO.StringIO()
        io.write("*" * 80)
        io.write("\r\n")
        io.write(" Recieved soap fault \r\n")
        io.write(" FaultCode            %s \r\n" % self.faultcode)
        io.write(" FaultString          %s \r\n" % self.faultstring)
        io.write(" FaultDetail          \r\n")

        if self.detail is not None:
            io.write(self.detail)

        return io.getvalue()

{'fraction_non_alphanumeric': 0.1078532488581111, 'fraction_numerical': 0.0044202151171357, 'mean_word_length': 2.8177727784027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4922101', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1979, 'n_words': 561}