a
    »ô_Fb  ã                   @   sP  d Z ddlmZ ddlZejdkr&eZddlZddlZddlZddl	Z	ddl
Z
ddlZzddlmZmZ W n" eyŒ   ddlmZmZ Y n0 ddlmZmZmZmZ ddlmZmZmZmZ e e¡Ze
 d	¡ZG d
d„ de ƒZ!G dd„ de"ƒZ#G dd„ deƒZ$G dd„ de"ƒZ%edkrLe#dddddddddd	Z&dAdd„Z'dd„ Z(dd„ Z)e&j*d e'd!e+eej,d"œie+e+d#œed$eigd%œd& e&j*d'e(d(eid)eid& e& *d*e)¡ d+ej-v râe& .¡ Z.e& /¡ D ]\Z0Z1e& 2e0¡\Z3Z4Z1qÆd,ej-v re 5d-¡ ed.e$ƒZ6e&e6_&e6 7¡  d/ej-v rNe 5d0¡ dd1l8m9Z9 e%e&ƒZ:e9d2d3e:ƒZ;e; 7¡  d4ej-v rÊdd5l<m=Z= e=ddd6d7ddd8Z<dd9d#œZ>d$d:id$d;igZ?e<j@e>d<e?d%Z4e4jAZBe 5e+eBjCƒ¡ e 5eeBjDƒ¡ d=ej-v rLdd5l<m=Z= e=dd>Z<dd9d#œZ>d$d:id$d;igZ?ej, E¡ ZFe<j@e>eFe?d%Z4e4d! ZBe 5e+eBd? ƒ¡ e 5eeBd@ ƒ¡ dS )Bz*Pythonic simple SOAP Server implementationé    )Úunicode_literalsNÚ3)ÚBaseHTTPRequestHandlerÚ
HTTPServeré   )Ú
__author__Ú__copyright__Ú__license__Ú__version__)ÚSimpleXMLElementÚTYPE_MAPÚDateÚDecimalzxmlns:(\w+)="(.+?)"c                   @   s   e Zd Zddd„ZdS )Ú	SoapFaultNc                 C   s"   |p
| j j| _|pd| _|| _d S )NÚ )Ú	__class__Ú__name__Ú	faultcodeÚfaultstringÚdetail)Úselfr   r   r   © r   ú5/usr/lib/python3/dist-packages/pysimplesoap/server.pyÚ__init__+   s    
zSoapFault.__init__)NNN)r   Ú
__module__Ú__qualname__r   r   r   r   r   r   *   s   r   c                
   @   sp   e Zd ZdZdddddddi ddf
dd„Zed	d
„ ƒZddd„Zdd„ Zddd„Z	dd„ Z
ddd„Zdd„ ZdS )ÚSoapDispatcherz!Simple Dispatcher for SOAP Serverr   NFú)http://schemas.xmlsoap.org/soap/envelope/Úsoapc                 K   sL   i | _ || _|| _|| _|| _|| _|| _|| _|| _|	| _	|
| _
|| _dS )a'  
        :param namespace: Target namespace; xmlns=targetNamespace
        :param prefix: Prefix for target namespace; xmlns:prefix=targetNamespace
        :param namespaces: Specify additional namespaces; example: {'external': 'http://external.mt.moboperator'}
        :param pretty: Prettifies generated xmls
        :param debug: Use to add tracebacks in generated xmls.

        Multiple namespaces
        ===================

        It is possible to support multiple namespaces.
        You need to specify additional namespaces by passing `namespace` parameter.

        >>> dispatcher = SoapDispatcher(
        ...    name = "MTClientWS",
        ...    location = "http://localhost:8008/ws/MTClientWS",
        ...    action = 'http://localhost:8008/ws/MTClientWS', # SOAPAction
        ...    namespace = "http://external.mt.moboperator", prefix="external",
        ...    documentation = 'moboperator MTClientWS',
        ...    namespaces = {
        ...        'external': 'http://external.mt.moboperator',
        ...        'model': 'http://model.common.mt.moboperator'
        ...    },
        ...    ns = True)

        Now the registered method must return node names with namespaces' prefixes.

        >>> def _multi_ns_func(self, serviceMsisdn):
        ...    ret = {
        ...        'external:activateSubscriptionsReturn': [
        ...            {'model:code': '0'},
        ...            {'model:description': 'desc'},
        ...        ]}
        ...    return ret

        Our prefixes will be changed to those used by the client.
        N)ÚmethodsÚnameÚdocumentationÚactionÚlocationÚ	namespaceÚprefixÚsoap_nsÚsoap_uriÚ
namespacesÚprettyÚdebug)r   r    r!   r"   r#   r$   r%   r'   r&   r(   r)   r*   Úkwargsr   r   r   r   4   s    -zSoapDispatcher.__init__c                    s>   |r:d‰ d  ‡ ‡fdd„| ¡ D ƒ¡}ˆ dd| d ¡‰ˆS )z‰Extends xml with extra namespaces.
        :param ns: dict with namespaceUrl:prefix pairs
        :param xml: XML node to modify
        zxmlns:%s="%s"ú c                    s$   g | ]\}}|ˆvrˆ ||f ‘qS r   r   )Ú.0Zurir%   ©Z_tplÚxmlr   r   Ú
<listcomp>v   ó    z4SoapDispatcher._extra_namespaces.<locals>.<listcomp>z/>)ÚjoinÚitemsÚreplace)r/   ÚnsZ_ns_strr   r.   r   Ú_extra_namespacesn   s
    z SoapDispatcher._extra_namespacesc                 C   s"   ||||pt |ddƒf| j|< d S )NÚ__doc__r   )Úgetattrr   )r   r    ÚfnÚreturnsÚargsÚdocr   r   r   Úregister_functionz   s    z SoapDispatcher.register_functionc                 C   s   d| S )Nú
%sResponser   )r   Úmethodr   r   r   Úresponse_element_name}   s    z$SoapDispatcher.response_element_namec           !   
      s   | j }d}|du ri }| j| j }}d}d}	tdd„ | j ¡ D ƒƒ‰ zzt|| jd}
|
dd… D ]`\}}|dv r|
 ¡ | j	}|
 ¡ | j
}qb|| j ¡ v rb|
 ¡ | j	}|
 ¡ | j
}|ˆ |< qbt |¡}|D ]\}}|| j ¡ v rÒ|ˆ |< qÒd}|
d|d	 ¡ d
ƒ}|r,|t| jƒd d… }	| j }|r8|	sH| ¡ }	| ¡ }t d|	¡ | j|	 \}‰}}t dˆ¡ |rˆ| ¡  |¡}n|du rœd|i}ni }d}|f i |¤Ž}t d|¡ W nÌ ty } z,| d||jf |j|jdœ¡ W Y d}~nd}~0  tyŒ   d
dl}| ¡ \}}}t t  !¡ ¡ | jrhd "t  #|||¡¡}|d| $d¡ 7 }nd}| d||j%f ||dœ¡ Y n0 |sšd}nd}|| j|||dœ; }t& '|ˆ ¡}t‡ fdd„| j ¡ D ƒƒ}t|| j||d}d|d< d|d< |j(d | d!d	}|r*|j)d"| |d!d	 nè|j(|  *|	¡| jd	}|sP| j|d#< ˆrèt+|tƒ}|r t,‡fd$d%„| -¡ D ƒƒ} | s t. /d&t0ˆƒt0|ƒf ¡ |r¬| sÆ| )t1ˆ -¡ ƒd
 |¡ n | ¡ D ]\}}| )||¡ qÎn*ˆdu rþ| 2|¡ nˆi krt 3d'¡ |j4| j5d(S ))z.Receive and process SOAP call, returns the xmlNZVersionMismatchc                 s   s   | ]\}}||fV  qd S ©Nr   ©r-   ÚkÚvr   r   r   Ú	<genexpr>   r1   z*SoapDispatcher.dispatch.<locals>.<genexpr>)r$   )r   z"http://www.w3.org/2003/05/soap-envz'http://www.w3.org/2003/05/soap-envelopeZClientZBody)r5   r   r   éÿÿÿÿzdispatch method: %szreturns_types %sÚrequestZServerzdispathed method returns: %sz%s.%s)r   r   r   r   z

XML REQUEST

zUTF-8z8<%(soap_ns)s:Envelope xmlns:%(soap_ns)s="%(soap_uri)s"/>zp<%(soap_ns)s:Envelope xmlns:%(soap_ns)s="%(soap_uri)s"
                       xmlns:%(prefix)s="%(namespace)s"/>)r$   r%   r&   r'   c                 3   s   | ]\}}|ˆ | fV  qd S rA   r   rB   )Ú_ns_reversedr   r   rE   î   r1   )r$   Znamespaces_mapr%   z)http://www.w3.org/2001/XMLSchema-instancez	xmlns:xsiz http://www.w3.org/2001/XMLSchemaz	xmlns:xsdz%s:BodyFz%s:FaultZxmlnsc                    s   g | ]}|ˆ v ‘qS r   r   )r-   rC   )Úreturns_typesr   r   r0   
  r1   z+SoapDispatcher.dispatch.<locals>.<listcomp>z3Return value doesn't match type structure: %s vs %sz%Given returns_types is an empty dict.©r)   )6r%   r&   r'   Údictr(   r3   r   r$   Z
attributesZ	localNameÚvalueÚvaluesÚNS_RXÚfindallZchildrenÚlenr"   Zget_local_nameZ
get_prefixÚlogr*   r   Z
unmarshallr   Úupdater   r   r   Ú	ExceptionÚsysÚexc_infoÚerrorÚ	tracebackÚ
format_excr2   Úformat_exceptionÚdecoder   r   r6   Ú	add_childÚmarshallr@   Ú
isinstanceÚallÚkeysÚwarningsÚwarnÚstrÚlistZimport_nodeZwarningÚas_xmlr)   )!r   r/   r"   Úfaultr%   Zretr&   r'   Zsoap_fault_coder    rG   rC   rD   Z_nsZ_urir5   r?   ÚfunctionZ
args_typesr<   r;   ÚerT   ÚetypeZevalueZetbr   ÚmappingÚresponseÚbodyÚresZcomplex_typeZtypes_okr   )rH   rI   r   Údispatch€   sÈ    




ýþüý

ÿ


zSoapDispatcher.dispatchc                 C   s   dd„ | j  ¡ D ƒS )z'Return a list of aregistered operationsc                 S   s    g | ]\}\}}}}||f‘qS r   r   )r-   r?   rf   r:   r;   r<   r   r   r   r0   !  r1   z/SoapDispatcher.list_methods.<locals>.<listcomp>)r   r3   )r   r   r   r   Úlist_methods  s    zSoapDispatcher.list_methodsc                 C   s   | j | \}}}}d|| jdœ }t|| j| jd}|rB| ¡ }n|du rRdg}ng }|D ]\}	}
||ƒj|	|
ddd qZd	|| jdœ }t|| j| jd}|rª| ¡ }n|du rºdg}ng }|D ]"\}	}
|d
| ƒj|	|
ddd qÂ|jdd|jdd|fS )z-Generate sample request and response messagesz“
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body><%(method)s xmlns="%(namespace)s"/></soap:Body>
</soap:Envelope>)r?   r$   )r$   r%   N©rL   NTF)Zadd_commentsr5   z›
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body><%(method)sResponse xmlns="%(namespace)s"/></soap:Body>
</soap:Envelope>r>   rJ   )r   r$   r   r%   r3   r\   rd   )r   r?   rf   r:   r;   r<   r/   rG   r3   rC   rD   rj   r   r   r   Úhelp#  s0    
ý

ý
zSoapDispatcher.helpc                    sp  d| j | j| jdœ }t|ƒ‰| j ¡ D ]˜\}\}}}}d2‡ ‡fdd„	‰ ˆ d| |oZ| ¡ ƒ ˆ d| |op| ¡ ƒ dD ]F\}}ˆ d	¡}	d
||f |	d< |	 d¡}
dd||f dœ|
dd…< qxq(ˆ d¡}d| j |d< | j ¡ D ]`\}\}}}}| d¡}||d< |r| d|¡ | d¡}d| |d< | d¡}d| |d< qäˆ d¡}d| j |d< d| j |d< | d¡}d|d < d!|d"< | j ¡ D ]p}| d¡}||d< | d#¡}| j| |d$< d|d < | d¡}| d%¡}d&|d'< | d¡}| d%¡}d&|d'< qˆ d(¡}d)| j |d< |jd| jd* | d+¡}d| j |d< d,| j |d-< | d.¡}| j	|d/< ˆj
d0d1S )3z%Generate Web Service Description v1.1aŠ  <?xml version="1.0"?>
<wsdl:definitions name="%(name)s"
          targetNamespace="%(namespace)s"
          xmlns:tns="%(namespace)s"
          xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
          xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">%(documentation)s</wsdl:documentation>

    <wsdl:types>
       <xsd:schema targetNamespace="%(namespace)s"
              elementFormDefault="qualified"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       </xsd:schema>
    </wsdl:types>

</wsdl:definitions>
)r$   r    r!   Fc                    s‚  |s"ˆdƒdƒ  d¡}|  d¡}nˆdƒdƒ  d¡}|}| |d< |rJ|}n|d u rZdg}ng }|sr|rr|  d¡}n|r€|  d¡}|D ]ø\}}|  d¡}	||	d< |r´d	d
dœ|	d d …< |d u rÂd}
n®t|tƒrd| |f }g }|D ]}| | ¡ ¡ qâˆ ||ddd d| }
n`t|tƒrDd| |f }ˆ || ¡ dd d| }
n,|t ¡ v r`dt|  }
ntdt	|ƒ ƒ‚|	 
d|
¡ q„d S )Nz
wsdl:typesz
xsd:schemazxsd:elementzxsd:complexTyper    ro   zxsd:allzxsd:sequenceÚ0Z	unbounded)Z	minOccursZ	maxOccurszxsd:anyTypezArrayOf%s%sT)ÚarrayÚcomplexztns:%sú%s%s)rs   zxsd:%szunknonw type %s for marshallingÚtype)r[   r]   rc   Úextendr3   rK   r   r_   Ú	TypeErrorrb   Zadd_attribute)r    rM   rr   rs   Úelementr3   r^   rC   rD   rg   ÚtÚnÚlÚd©Úparse_elementÚwsdlr   r   r~   ]  sJ    



z*SoapDispatcher.wsdl.<locals>.parse_elementz%sr>   ))ZInputr   )ZOutputZResponsezwsdl:messagert   r    z	wsdl:partÚ
parametersztns:%s%s)r    rx   Nzwsdl:portTypez
%sPortTypezwsdl:operationzwsdl:documentationz
wsdl:inputztns:%sInputÚmessagezwsdl:outputztns:%sOutputzwsdl:bindingz	%sBindingztns:%sPortTyperu   zsoap:bindingZdocumentZstylez$http://schemas.xmlsoap.org/soap/httpZ	transportzsoap:operationZ
soapActionz	soap:bodyÚliteralZusezwsdl:servicez	%sService)Útextz	wsdl:portztns:%sBindingÚbindingzsoap:addressr#   TrJ   )FF)r$   r    r!   r   r   r3   r[   r_   r"   r#   rd   )r   r/   r?   rf   r:   r;   r<   Úmrg   r   ÚpartZportTypeÚopÚinputÚoutputr„   ZsoapbindingZsoapopZsoapbodyZserviceZportZsoapaddressr   r}   r   r   D  sj    ï.


ÿ















zSoapDispatcher.wsdl)NNN)NN)N)r   r   r   r7   r   Ústaticmethodr6   r=   r@   rm   rn   rp   r   r   r   r   r   r   1   s$   ú
:


  
!r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSOAPHandlerc                 C   sÊ   | j dd…  d¡}| j dkrJ|d | jjj ¡ vrJ|  dd|d  ¡ n|| j dkrb| jj ¡ }n:| jj |d ¡\}}}t	|ƒdks’|d dkr˜|}n|}|  
d	¡ |  d
d¡ |  ¡  | j |¡ dS )z'User viewable help information and wsdlr   Nú?ú/r   i”  úMethod not found: %srG   éÈ   úContent-typeútext/xml)ÚpathÚsplitÚserverÚ
dispatcherr   r_   Z
send_errorr   rp   rP   Úsend_responseÚsend_headerÚend_headersÚwfileÚwrite)r   r;   rj   Úreqrl   r<   r   r   r   Údo_GETÆ  s     

zSOAPHandler.do_GETc                 C   sš   | j  t| j d¡ƒ¡}tjdk r0| j d¡}n| j d¡}| 	|¡}i }| j
jj||d}|rl|  d¡ n
|  d¡ |  dd¡ |  ¡  | j |¡ d	S )
zSOAP POST gatewayzcontent-lengthr   Úcharset)re   iô  r   r   r‘   N)ZrfileÚreadÚintZheadersÚgetrT   ÚversionZgetparamZ	get_paramrZ   r”   r•   rm   r–   r—   r˜   r™   rš   )r   rG   Úencodingre   rj   r   r   r   Údo_POSTÛ  s    


zSOAPHandler.do_POSTN)r   r   r   rœ   r£   r   r   r   r   r‹   Ä  s   r‹   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚWSGISOAPHandlerc                 C   s
   || _ d S rA   )r•   )r   r•   r   r   r   r   ó  s    zWSGISOAPHandler.__init__c                 C   s   |   ||¡S rA   )Úhandler©r   ÚenvironÚstart_responser   r   r   Ú__call__ö  s    zWSGISOAPHandler.__call__c                 C   sF   |d dkr|   ||¡S |d dkr0|  ||¡S |ddgƒ dgS d S )NZREQUEST_METHODZGETZPOSTz405 Method not allowed©úContent-Typez
text/plainzMethod not allowed)Údo_getÚdo_postr¦   r   r   r   r¥   ù  s    zWSGISOAPHandler.handlerc           	      C   s¬   |  d¡ d¡}|  d¡}|dkrH|| jj ¡ vrH|ddgƒ d| gS |dkr\| j ¡ }n0| j |¡\}}}t|ƒdks‚|d	krˆ|}n|}|d
ddtt|ƒƒfgƒ |gS )NZ	PATH_INFOr   ZQUERY_STRINGr   z404 Not Foundrª   rŽ   r   rG   ú200 OK©r«   r‘   úContent-Length)	r    Úlstripr•   r   r_   r   rp   rP   rb   )	r   r§   r¨   r’   Zqueryrj   r›   rl   r<   r   r   r   r¬     s    

zWSGISOAPHandler.do_getc                 C   sF   t |d ƒ}|d  |¡}| j |¡}|dddtt|ƒƒfgƒ |gS )NZCONTENT_LENGTHz
wsgi.inputr®   r¯   r°   )rŸ   rž   r•   rm   rb   rP   )r   r§   r¨   ZlengthrG   rj   r   r   r   r­     s
    zWSGISOAPHandler.do_postN)r   r   r   r   r©   r¥   r¬   r­   r   r   r   r   r¤   ñ  s
   	r¤   Ú__main__ZPySimpleSoapSamplezhttp://localhost:8008/z%http://example.com/pysimplesoapsamle/Zns0z'Example soap service using PySimpleSoapT)	r    r#   r"   r$   r%   r!   Útracer*   r5   c                 C   s:   |t  d¡ }| d | d  |d d |d d  |dœS )zAdd several valuesim  ÚaÚbr   r|   r   ©ÚabÚddÚdt)ÚdatetimeZ	timedelta)ÚpÚcr¹   r   r   r   Úadder(  s    r½   c                 C   s   | S )zJust return inputr   )Úin0r   r   r   Údummy-  s    r¿   c                 C   s   | j S )z*Copy request->response (generic, any type))rL   )rG   r   r   r   Úecho1  s    rÀ   ÚAdderÚ	AddResultr¶   )r´   rµ   r|   )r»   r¹   r¼   )r:   r;   ZDummyZout0r¾   ZEchoz--localz--servezStarting server...)r   éH  z--wsgi-servezStarting wsgi server...)Úmake_serverr   rÃ   z	--consume)Ú
SoapClientzhttp://example.com/sample.wsdlr   )r#   r"   r$   r&   r³   r5   é   z1.20z2.01z
2010-07-24z--consume-wsdl)r   r·   r¸   )N)Gr7   Z
__future__r   rT   r¡   rb   Zunicoderº   Zloggingr`   ÚrerW   ZBaseHTTPServerr   r   ÚImportErrorZhttp.serverr   r   r   r	   r
   Z	simplexmlr   r   r   r   Z	getLoggerr   rQ   ÚcompilerN   rS   r   Úobjectr   r‹   r¤   r•   r½   r¿   rÀ   r=   rŸ   ÚdateÚargvr   rn   r?   r<   rp   rG   rj   ÚinfoZhttpdZserve_foreverZwsgiref.simple_serverrÄ   ZapplicationZwsgidZclientrÅ   r»   r¼   rÁ   rÂ   Úresultr·   r¸   Ztodayr¹   r   r   r   r   Ú<module>   s¸   


   -,
ù	
ýý


ú
ÿ

