o
    g<                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZ ejr]ddlmZ ddiZe e!Z"G dd dZ#G dd dZ$dS )z(
    zeep.wsdl.wsdl
    ~~~~~~~~~~~~~~

    N)OrderedDict)etree)IncompleteMessage)absolute_locationis_relative_pathload_external)Settings)findall_multiple_ns)parse)BindingPortTypeService)Schema)	Transportwsdl http://schemas.xmlsoap.org/wsdl/c                   @   s`   e Zd ZdZ	ddejd fddZdd Zd	d
 Zdd Z	dej
dejfddZdddZdS )Documenta  A WSDL Document exists out of one or more definitions.

    There is always one 'root' definition which should be passed as the
    location to the Document.  This definition can import other definitions.
    These imports are non-transitive, only the definitions defined in the
    imported document are available in the parent definition.  This Document is
    mostly just a simple interface to the root definition.

    After all definitions are loaded the definitions are resolved. This
    resolves references which were not yet available during the initial
    parsing phase.


    :param location: Location of this WSDL
    :type location: string
    :param transport: The transport object to be used
    :type transport: zeep.transports.Transport
    :param base: The base location of this document
    :type base: str
    :param strict: Indicates if strict mode is enabled
    :type strict: bool

    N	transportr   c                 C   sj   |pt  | _t|trt|rtj|}|| _n|| _|| _	i | _
td| j	| j| jd| _| | dS )zjInitialize a WSDL document.

        The root definition properties are exposed as entry points.

        N)noder   locationsettings)r   r   
isinstancestrr   ospathabspathr   r   _definitionsr   typesload)selfr   r   baser    r!   G/home/ubuntu/webapp/venv/lib/python3.10/site-packages/zeep/wsdl/wsdl.py__init__9   s    
zDocument.__init__c                 C   sD   |  |}t| || j}|  |j| _|j| _|j| _|j| _d S N)_get_xml_document
Definitionr   resolve_importsmessages
port_typesbindingsservices)r   r   documentroot_definitionsr!   r!   r"   r   X   s   
zDocument.loadc                 C   s
   d| j  S )Nz<WSDL(location=%r)>)r   r   r!   r!   r"   __repr__d   s   
zDocument.__repr__c                 C   s  t d t d | jj D ]\}}t dd||f  qt d t d t| jjdd dD ]}|j| jd	}t d| q.t d t d
 t| jjdd dD ]}|j| jd	}t d| qOt d t d t| j dd dD ]	}t dt	| qqt d | j
 D ]<}t t	| |j D ].}t dt	| t dd t|jj tdd}	|	D ]}
t ddt	|
f  qt d qqd S )N z	Prefixes:z    z%s: %szGlobal elements:c                 S   s   | j S r$   qnamekr!   r!   r"   <lambda>o   s    zDocument.dump.<locals>.<lambda>)key)schemazGlobal types:c                 S   s
   | j pdS )Nr0   r1   r3   r!   r!   r"   r5   u   s   
 z	Bindings:c                 S   s   t | S r$   )r   r3   r!   r!   r"   r5   {   s    z        zOperations:namez%s%sz            )printr   
prefix_mapitemssortedelements	signaturer*   valuesr   r+   portsbinding_operationsoperator
attrgetter)r   prefix	namespaceelm_objvaluetype_objbinding_objserviceport
operations	operationr!   r!   r"   dumpg   s@   

zDocument.dumpr   returnc                 C   s   t || j| j| jdS )zLoad the XML content from the given location and return an
        lxml.Element object.

        :param location: The URL of the document to load
        :type location: string

        )r   )r   r   r   r   )r   r   r!   r!   r"   r%      s   zDocument._get_xml_document
definitionr&   c                 C   s   |j |jf}|| j|< d S r$   )target_namespacer   r   )r   rQ   r6   r!   r!   r"   _add_definition   s   zDocument._add_definition)NN)rQ   r&   )__name__
__module____qualname____doc__typingTyper#   r   r/   rO   IOr   _Elementr%   rS   r!   r!   r!   r"   r       s    
&r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	d
ZdddZdd Z	dd Z
dejfddZdejdejeef fddZdejdejeeje f fddZdejdejeef fddZdS )r&   zaThe Definition represents one wsdl:definition within a Document.

    :param wsdl: The wsdl

    c                 C   st   t d| || _|| _|j| _i | _i | _i | _t | _	i | _
d| _|d| _| j|  |j| _| | dS )z+fo

        :param wsdl: The wsdl

        zCreating definition for %sFtargetNamespaceN)loggerdebugr   r   r   r)   r(   r*   r   r+   imports_resolved_importsgetrR   rS   nsmap_load)r   r   docr   r!   r!   r"   r#      s   zDefinition.__init__c                 C   sH   |  | | | | || _| || _| || _| || _	d S r$   )
parse_importsparse_typesparse_messagesr(   parse_portsr)   parse_bindingr*   parse_servicer+   )r   rd   r!   r!   r"   rc      s   

zDefinition._loadc                 C   s   d| j j| jf S )Nz<%s(location=%r)>)	__class__rT   r   r.   r!   r!   r"   r/      s   zDefinition.__repr__Nc                 C   s   t | |}||v r|| S |pt }| j|vrT|| j | j D ]1}z
||||W   S  tyS   t	|j
}z||||W  Y   S  tyP   Y nw Y q"w td||f )NzNo definition %r in %r found)getattrsetrR   addr_   r?   ra   
IndexErrorr   QName	localname)r   r8   r6   
_processed	containerrQ   fallback_keyr!   r!   r"   ra      s&   


zDefinition.getrP   c                 C   s   | j rdS d| _ | j D ]}|  q| j D ]}||  q| j D ]}||  q&| j D ]}||  q3| j D ]}||  q@dS )z1Resolve all root elements (types, messages, etc).NT)	r`   r_   r?   r'   r(   resolver)   r*   r+   )r   rQ   message	port_typerA   rK   r!   r!   r"   r'      s   
zDefinition.resolve_importsc                 C   s   |j dtdD ]S}|d}|d}|std| qt|| j}||f}|| jjv r6| jj| | j	|< q| j
|}t|jjdkrN| j|g| qt| j||}|| j	|< qdS )a  Import other WSDL definitions in this document.

        Note that imports are non-transitive, so only import definitions
        which are defined in the imported document and ignore definitions
        imported in that document.

        This should handle recursive imports though:

            A -> B -> A
            A -> B -> C -> A

        :param doc: The source document
        :type doc: lxml.etree._Element

        zwsdl:import
namespacesrF   r   z1Skipping import for namespace %s (empty location)r7   N)findallNSMAPra   r]   r^   r   r   r   r   r_   r%   r   rp   tagrq   r   add_documentsr&   )r   rd   import_noderF   r   r6   r,   r   r!   r!   r"   re      s$   

zDefinition.parse_importsc                 C   s4   ddddddg}t |d|}| j|| j dS )a_  Return an xsd.Schema() instance for the given wsdl:types element.

        If the wsdl:types contain multiple schema definitions then a new
        wrapping xsd.Schema is defined with xsd:import statements linking them
        together.

        If the wsdl:types doesn't container an xml schema then an empty schema
        is returned instead.

        Definition::

            <definitions .... >
                <types>
                    <xsd:schema .... />*
                </types>
            </definitions>

        :param doc: The source document
        :type doc: lxml.etree._Element

        z http://www.w3.org/2001/XMLSchemar   )xsdr   z http://www.w3.org/1999/XMLSchemazwsdl:types/xsd:schemaN)r	   r   r}   r   )r   rd   namespace_setsschema_nodesr!   r!   r"   rf   !  s   zDefinition.parse_typesrd   c                 C   s|   i }|j dtdD ]2}zt| |}W n ty, } ztt| W Y d}~q	d}~ww |||jj	< t
d|jj	 q	|S )a>  

        Definition::

            <definitions .... >
                <message name="nmtoken"> *
                    <part name="nmtoken" element="qname"? type="qname"?/> *
                </message>
            </definitions>

        :param doc: The source document
        :type doc: lxml.etree._Element

        zwsdl:messagerx   NzAdding message: %s)rz   r{   r
   parse_abstract_messager   warningswarnr   r8   textr]   r^   )r   rd   resultmsg_nodemsgexcr!   r!   r"   rg   F  s   zDefinition.parse_messagesc                 C   sD   i }|j dtdD ]}t| |}|||jj< td|jj q	|S )ar  Return dict with `PortType` instances as values

        Definition::

            <wsdl:definitions .... >
                <wsdl:portType name="nmtoken">
                    <wsdl:operation name="nmtoken" .... /> *
                </wsdl:portType>
            </wsdl:definitions>

        :param doc: The source document
        :type doc: lxml.etree._Element

        zwsdl:portTyperx   zAdding port: %s)rz   r{   r
   parse_port_typer8   r   r]   r^   )r   rd   r   	port_noderw   r!   r!   r"   rh   `  s   zDefinition.parse_portsc           	      C   s   i }g }t | jjddsddlm} |j|j|j|jg}n| jjj	}|j
dtdD ]?}d}|D ]8}||rgz|| |}W n tyV } ztd| W Y d}~q/d}~ww td|jj |||jj<  nq/q)|S )	a7  Parse the binding elements and return a dict of bindings.

        Currently supported bindings are Soap 1.1, Soap 1.2., HTTP Get and
        HTTP Post. The detection of the type of bindings is done by the
        bindings themselves using the introspection of the xml nodes.

        Definition::

            <wsdl:definitions .... >
                <wsdl:binding name="nmtoken" type="qname"> *
                    <-- extensibility element (1) --> *
                    <wsdl:operation name="nmtoken"> *
                       <-- extensibility element (2) --> *
                       <wsdl:input name="nmtoken"? > ?
                           <-- extensibility element (3) -->
                       </wsdl:input>
                       <wsdl:output name="nmtoken"? > ?
                           <-- extensibility element (4) --> *
                       </wsdl:output>
                       <wsdl:fault name="nmtoken"> *
                           <-- extensibility element (5) --> *
                       </wsdl:fault>
                    </wsdl:operation>
                </wsdl:binding>
            </wsdl:definitions>

        :param doc: The source document
        :type doc: lxml.etree._Element
        :returns: Dictionary with binding name as key and Binding instance as
          value
        :rtype: dict

        binding_classesNr   )r*   zwsdl:bindingrx   zIgnoring binding: %szAdding binding: %s)rl   r   r   	zeep.wsdlr*   Soap11BindingSoap12BindingHttpGetBindingHttpPostBindingr   rz   r{   matchr
   NotImplementedErrorr]   r^   r8   r   )	r   rd   r   r   r*   binding_noderA   binding_classr   r!   r!   r"   ri   v  s6   $

zDefinition.parse_bindingc                 C   sB   t  }|jdtdD ]}t| |}|||j< td|j q
|S )a  

        Definition::

            <wsdl:definitions .... >
                <wsdl:service .... > *
                    <wsdl:port name="nmtoken" binding="qname"> *
                       <-- extensibility element (1) -->
                    </wsdl:port>
                </wsdl:service>
            </wsdl:definitions>

        :param doc: The source document
        :type doc: lxml.etree._Element

        zwsdl:servicerx   zAdding service: %s)r   rz   r{   r
   rj   r8   r]   r^   )r   rd   r   service_noderK   r!   r!   r"   rj     s   
zDefinition.parse_servicer$   )rP   N)rT   rU   rV   rW   r#   rc   r/   ra   r'   re   rf   r   r[   rg   rX   Dictr   r   rh   rY   r   ri   r   rj   r!   r!   r!   r"   r&      s"    	

&%
"Dr&   )%rW   loggingrC   r   rX   r   collectionsr   lxmlr   zeep.exceptionsr   zeep.loaderr   r   r   zeep.settingsr   
zeep.utilsr	   r   r
   zeep.wsdl.definitionsr   r   r   zeep.xsdr   TYPE_CHECKINGzeep.transportsr   r{   	getLoggerrT   r]   r   r&   r!   r!   r!   r"   <module>   s*    
~