<s> """<STR_LIT>""" <EOL> from xml . etree import ElementTree as ET <EOL> LOG_SYSTEM = '<STR_LIT>' <EOL> SOAP_ENVELOPE_NS = "<STR_LIT>" <EOL> SOAP_ENV = ET . QName ( "<STR_LIT>" % SOAP_ENVELOPE_NS ) <EOL> SOAP_HEADER = ET . QName ( "<STR_LIT>" % SOAP_ENVELOPE_NS ) <EOL> SOAP_BODY = ET . QName ( "<STR_LIT>" % SOAP_ENVELOPE_NS ) <EOL> SOAP_FAULT = ET . QName ( "<STR_LIT>" % SOAP_ENVELOPE_NS ) <EOL> FAULTCODE = '<STR_LIT>' <EOL> FAULTSTRING = '<STR_LIT>' <EOL> DETAIL = '<STR_LIT>' <EOL> FAULTCODE_SERVER = '<STR_LIT>' <EOL> ET . register_namespace ( '<STR_LIT>' , SOAP_ENVELOPE_NS ) <EOL> def _indent ( elem , level = <NUM_LIT:0> ) : <EOL> i = "<STR_LIT:\n>" + level * "<STR_LIT:U+0020>" <EOL> if len ( elem ) : <EOL> if not elem . text or not elem . text . strip ( ) : <EOL> elem . text = i + "<STR_LIT:U+0020>" <EOL> if not elem . tail or not elem . tail . strip ( ) : <EOL> elem . tail = i <EOL> for elem in elem : <EOL> _indent ( elem , level + <NUM_LIT:1> ) <EOL> if not elem . tail or not elem . tail . strip ( ) : <EOL> elem . tail = i <EOL> else : <EOL> if level and ( not elem . tail or not elem . tail . strip ( ) ) : <EOL> elem . tail = i <EOL> def createSoapEnvelope ( ) : <EOL> envelope = ET . Element ( SOAP_ENV ) <EOL> header = ET . SubElement ( envelope , SOAP_HEADER ) <EOL> body = ET . SubElement ( envelope , SOAP_BODY ) <EOL> return envelope , header , body <EOL> def createSoapPayload ( body_element = None , header_element = None ) : <EOL> envelope , header , body = createSoapEnvelope ( ) <EOL> if header_element is not None : <EOL> header . append ( header_element ) <EOL> if body_element is not None : <EOL> if type ( body_element ) is list : <EOL> body . extend ( body_element ) <EOL> else : <EOL> body . append ( body_element ) <EOL> _indent ( envelope ) <EOL> payload = ET . tostring ( envelope , '<STR_LIT:utf-8>' ) <EOL> return payload <EOL> def createSoapFault ( fault_msg , detail_element = None ) : <EOL> assert type ( fault_msg ) is str , '<STR_LIT>' <EOL> fault_element = ET . Element ( SOAP_FAULT ) <EOL> fault_code = ET . SubElement ( fault_element , FAULTCODE ) <EOL> fault_code . text = FAULTCODE_SERVER <EOL> fault_string = ET . SubElement ( fault_element , FAULTSTRING ) <EOL> fault_string . text = fault_msg <EOL> if detail_element is not None : <EOL> dec = ET . SubElement ( fault_element , DETAIL ) <EOL> dec . append ( detail_element ) <EOL> payload = createSoapPayload ( fault_element ) <EOL> return payload <EOL> def parseSoapPayload ( payload ) : <EOL> envelope = ET . fromstring ( payload ) <EOL> assert envelope . tag == SOAP_ENV , '<STR_LIT>' % envelope . tag <EOL> header_elements = None <EOL> for ec in envelope : <EOL> if ec . tag == SOAP_HEADER : <EOL> if header_elements is not None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> header_elements = list ( ec ) <EOL> continue <EOL> elif ec . tag == SOAP_BODY : <EOL> return header_elements , list ( ec ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( ec . tag ) ) <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def parseFault ( payload ) : <EOL> envelope = ET . fromstring ( payload ) <EOL> if envelope . tag != SOAP_ENV : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> body = envelope . find ( str ( SOAP_BODY ) ) <EOL> if body is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> fault = body . find ( str ( SOAP_FAULT ) ) <EOL> if fault is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> fault_code = fault . find ( '<STR_LIT>' ) <EOL> if fault_code is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> fault_string = fault . find ( '<STR_LIT>' ) <EOL> if fault_string is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> detail = None <EOL> dt = fault . find ( '<STR_LIT>' ) <EOL> if dt is not None : <EOL> dc = dt . getchildren ( ) [ <NUM_LIT:0> ] <EOL> if dc is not None : <EOL> detail = ET . tostring ( dc ) <EOL> return fault_code . text , fault_string . text , detail </s>
