o
    j`0                     @   s   d dl Z e eZe dedgZe dedgZd dlmZ d dl	m
Z
mZmZmZ d dlmZmZmZmZ d dlmZmZ d d	lmZ d d
lmZ G dd deZdd add ZG dd deZdS )    N.clientserver)pformat)BODY_STYLE_EMPTYBODY_STYLE_BAREBODY_STYLE_WRAPPEDEventManager)FaultRedirectRespawnErrorInvalidRequestError)	InterfaceInterfaceDocuments)six)register_applicationc                       s   e Zd Z fddZ  ZS )MethodAlreadyExistsErrorc                    s   t t| d| d S )NzMethod key %r already exists)superr   __init__)selfwhat	__class__ Q/var/www/html/LicenciasMiG/venv/lib/python3.10/site-packages/spyne/application.pyr   $   s   z!MethodAlreadyExistsError.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r   #   s    r   c                 C   s   dS )NzInternal Errorr   er   r   r   get_fault_string_from_exception)   s   r!   c                     s   ddl   fdd} | adS )zCall this function first thing in your main function to return original
    python errors to your clients in case of unhandled exceptions.
    r   Nc                    s      S N)
format_excr   	tracebackr   r    _get_fault_string_from_exception6   s   zRreturn_traceback_in_unhandled_exceptions.<locals>._get_fault_string_from_exception)r%   r!   )r&   r   r$   r   (return_traceback_in_unhandled_exceptions.   s   r'   c                   @   sZ   e Zd ZdZdZdddddefddZdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdS )Applicationa  The Application class is the glue between one or more service
    definitions, input and output protocols.

    :param services:     An iterable of Service subclasses that defines
                         the exposed services.
    :param tns:          The targetNamespace attribute of the exposed
                         service.
    :param name:         The optional name attribute of the exposed service.
                         The default is the name of the application class
                         which is by default 'Application'.
    :param in_protocol:  A ProtocolBase instance that denotes the input
                         protocol. It's only optional for NullServer transport.
    :param out_protocol: A ProtocolBase instance that denotes the output
                         protocol. It's only optional for NullServer transport.
    :param config:       An arbitrary python object to store random global data.
    :param classes:      An iterable of Spyne classes that don't appear in any
                         of the service definitions but need to appear in the
                         interface documents nevertheless.
    :param documents_container:
                         A class that implements the InterfaceDocuments
                         interface

    Supported events:
        * ``method_call``:
            Called right before the service method is executed

        * ``method_return_object``:
            Called right after the service method is executed

        * ``method_exception_object``:
            Called when an exception occurred in a service method, before the
            exception is serialized.

        * ``method_context_created``:
            Called from the constructor of the MethodContext instance.

        * ``method_context_closed``:
            Called from the ``close()`` function of the MethodContext instance,
            which in turn is called by the transport when the response is fully
            sent to the client (or in the client case, the response is fully
            received from server).
    Nr   c	           
      C   s   t || _|| _|| _|| _|| _| jd u r | jjdd | _t	
d| j| j t| | _d | _|| _|| _| jd u rFddlm}	 |	 | _| jd u rUddlm}	 |	 | _|   t| |d| _| j|  | jj| j_| j|  | jj| j_t|  d S )Nr   z"Initializing application {%s}%s...r   )ProtocolBase)documents_container)tupleservicestnsnameconfigclassesr   r   splitloggerinfor	   event_managererror_handlerin_protocolout_protocolspyne.protocolr*   check_unique_method_keysr   	interfaceset_appREQUESTmessageRESPONSEr   )
r   r-   r.   r/   r7   r8   r0   r1   r+   r*   r   r   r   r   i   s2   




zApplication.__init__c                 C   s  z@| d |jjtu r|jg|_n	|jjtu rg |_| ||_|jjtus0t	|jj
jdkr5|jg|_|j|_| d W dS  ty } zFz|  dg|_|j|_| d W n$ ty } zt| tdt||_| d W Y d}~nd}~ww W Y d}~dS W Y d}~dS d}~w ty } z%|jdks|jd	rt| nt| ||_| d
 W Y d}~dS d}~w ty } ztj|fi ddi tdt||_| d
 W Y d}~dS d}~ww )ak  Takes a MethodContext instance. Returns the response to the request
        as a native python object. If the function throws an exception, it
        returns None and sets the exception object to ctx.out_error.

        Overriding this method would break event management. So this is not
        meant to be overridden unless you know what you're doing.
        method_call   method_return_objectNmethod_redirectServermethod_redirect_exceptionClientzClient.method_exception_objectexc_info)
fire_event
descriptor
body_styler   	in_objectr   call_wrapper
out_objectr   lenout_message
_type_infooutprot_ctxprotocolr   do_redirect	Exceptionlogger_server	exceptionr
   r!   	out_error	faultcode
startswithlogger_clientr3   critical)r   ctxr    r   r   r   process_request   sT   	



zApplication.process_requestc                 C   sJ  |j du rtd dS |jjr|jjdusJ |jj|S |jj}|jdur+|j}i }|	||}|du rDt
d| | |f |jj}t|j}|du rTd}n|jjtu rft||dkrfd}n|dd }|jjdur}|j||s}td|jjr|f| }n||f| }|jjdu r|j | }|S |jjj||d}|S )zThis method calls the call_wrapper method in the service definition.
        This can be overridden to make an application-wide custom exception
        management.
        Nz0Skipping user code call as ctx.function is None.z{%s}%s with params %rr   rA   z Invalid object state for request)args)functionr3   debugrJ   no_selfservice_classrM   parent_class__orig____respawn__r   get_namespaceget_type_name
in_messager,   rL   rK   r   rO   get_flat_type_infowhenr   no_ctx)r   r]   clsfiltersinstin_clsr_   retvalr   r   r   rM      sD   




zApplication.call_wrapperc                 C   s
   | j  S r"   )r;   _has_callbacksr   r   r   r   rr     s   
zApplication._has_callbacksc                 C   s   t  }ddlm} | jj D ]=}t||sJ |jdur2t|j|vr2|j	| |
t|j |jdurLt|j|vrL|j	| |
t|j qdS )z@This is normally called on transport instantiation by ServerBaser   )MethodDescriptorN)setspynert   r;   method_id_mapvalues
isinstanceauxid
initializeaddrc   )r   r   seenrt   dr   r   r   reinitialize"  s   zApplication.reinitializec                 C   s   t tdd | jD S )Nc                 s   s    | ]}t |V  qd S r"   )r{   ).0sr   r   r   	<genexpr>4  s    z'Application.__hash__.<locals>.<genexpr>)hashr,   r-   rs   r   r   r   __hash__3  s   zApplication.__hash__c                 C   sx   i }| j D ]4}|j D ],}||jd }|d ur3td|jjt	
|j|jjt	
|j t|j|||j< qqd S )Nz-Methods keys for "%s.%s" and "%s.%s" conflict)r-   public_methodsrx   getinternal_keyr3   errorr`   r   r   get_function_namer   )r   keysr   mdescother_mdescr   r   r   r:   6  s    



z$Application.check_unique_method_keys)r   r   r   __doc__	transportr   r   r^   rM   rr   r   r   r:   r   r   r   r   r(   ;   s    +
0K;r(   ) logging	getLoggerr   r3   joinr[   rV   pprintr   rv   r   r   r   r	   spyne.errorr
   r   r   r   spyne.interfacer   r   
spyne.utilr   spyne.util.appregr   rU   r   r!   r'   objectr(   r   r   r   r   <module>   s   
