B
    EDb)                 @   s   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	 d dl
T d dlT d dlmZ d dlmZ d dlmZ d d	lZd d	lZd d	lZd d	lZd dlT d d
lmZ G dd deZd	S )    )global_service_collection)get_type_infovalidate_type)
attachmentextract_attachment_reference)TypeConverter)*)AttachmentHandler)CustomResponse)debugN)settingsc               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
Dispatchera  
    The dispatcher class handles the communication between the service interface and
    the user functionality. This happens in dispatch_request()

    1. It recieves the raw protocol data and attempts to parse it using the service
    interface's request handler. If successful a req_dict (request dictionary) is returned.

    2. The request dictionary is parsed to call_method() where the arguments are converted to
    fit the user defined method.

    3. If the call to the user defined method is successful the result is converted to a
    res_dict (response dictionary) in result_to_dict()

    4. The response dictionary is passed to the service interface's response handler and
    returned as raw protocol data.

    c             C   s    || _ || _||| _|| _d S )N)response_encodingsinstifacelogging)selfr   Zifclassr   r    r   7G:\easytimepro\master/mysite/ladon\server\dispatcher.py__init__#   s    
zDispatcher.__init__c          	   C   s  g }x|  D ]}|d |d kr^d|kr>||d g7 }nt| j | jjd|d  qt|d tkrRg }t|d d }	|	rx|d |d  D ] }
||d d |
||dg7 }qW n|d d t	krxr|d |d  D ]&}
|t
|
|| j| j | jjg7 }qW n6x4|d |d  D ] }
|||
|d d g7 }q"W ||g7 }qt|d }	|d |d  }|	r||d |||dg7 }q|d t	kr|t
||| j| j | jjg7 }q||||d g7 }qW |jj}t|t t d}t||jj }| jt@ r*|d	|jj|d
 f g7 }| jt@ rH|dt| g7 }| jt@ r\t }|jrd|i}|| t||d
 ||}nt||d
 | }| jt@ r|ddtt |   |S )z
        call_method converts the res_dict delivered from an interface
        to the type of arguments expected by the service method.
        tc is the TypeConverter associated to the service method.
        nameargsdefaultzParameter "%s" is not optionaltyper   )Z
prime_dicttcexport_dictr   zMethod:%s.%s
methodnamezRequestDict:%sZLADON_METHOD_TCzExecutionTime:%s)r   UndefinedServiceMethodr   _interface_namer   servicenamer   listr   r   r   r   Zfrom_unicode_stringsinfoZ
modulename
__import__globalslocalsgetattrr   ZLOG_REQUEST_ACCESSZLOG_REQUEST_DICTstrZLOG_EXECUTION_TIMEtimeZ_has_keywordsupdateinsert)r   methodreq_dictr   r   log_liner   argarg_list	type_infoitemvalZmnameZservice_moduleZservice_class_instancestartkwresultr   r   r   call_method)   s\    " 
zDispatcher.call_methodc             C   sh  |j j|j j| d}|j}t|}|dkr6ttgt	|r
g }	|	|d< t|d }||krhdS |rx|D ]}
|	|

||g7 }	qrW nz|d tkrxl|D ]>}
t	|
tkst| j | jjdt	|
 |	||
g7 }	qW n$xN|D ]}
|	||
|d g7 }	qW n*|tkr$|||d< n||||d< n|
|||d< | jt@ rd|dt| g7 }|S )zv
        Convert the result of a method call to it's dictionary representation.
        tc is a TypeConverter
        )r   servicenumberr*   Nr4   r   zAttachment expected got: %szResponseDict:%s)r!   r   r6   r   Z_rtyper   r    tuplecountr   __dict__r   ZAttachmentExpectedr   r   r   Zadd_attachmentZto_unicode_stringr   ZLOG_RESPONSE_DICTr&   )r   r*   r4   r   response_attachmentsr,   res_dicttypr/   Zresult_listr0   r   r   r   result_to_dictj   s<    




zDispatcher.result_to_dictc          
   C   s  g }yt  |d< d\}}y d|kr6d|kr6|dd}W n   Y nX | jj|| jd}tjrhtd| |d }| j	|}|st
| j | jjd	| t|j|j| j  d
}| |||||}W n tk
rd }	 z||dd }
t|	tr| jj|	|| j|
d}nH|d kr6| jjtt|	|| j|
d}n| jjtt|	|| j|
d}|S d }	~	X Y nX t|trv|S | j||||d |d}d|kr|d |d< d|kr|d |d< | jj|| jd}| jrtdd|  |S )Nr:   )NNs   <source>1</source>   &s   &amp;)encodingzdispatch_request=r   z.Service method "%s" is not declared in service)r?   Zallow_unsafe_conversionZonly_strings_to_unicodeZmirror)r?   
reflection)r,   r@   idz	%s	) r	   replacer   Zparse_requestr   r   DEBUGprintr   r*   r   r   r   r   Z	_encodingZ_allow_unsafe_conversionZstringify_res_dictr5   	Exceptionget
isinstanceZServiceFaultZbuild_fault_responseZClientFaultr&   ZServerFaultr
   r=   Zbuild_responser   r   join)r   Zrequest_datar   r,   r   r*   r+   r   r4   er@   Zresponser;   r   r   r   dispatch_request   sX    




	

zDispatcher.dispatch_requestN)__name__
__module____qualname____doc__r   r5   r=   rK   r   r   r   r   r      s
   A+r   )Z!mysite.ladon.ladonizer.collectionr   Zmysite.ladon.typesr   r   Zmysite.ladon.types.attachmentr   r   Z mysite.ladon.types.typeconverterr   Z"mysite.ladon.exceptions.dispatcherZmysite.ladon.exceptions.serviceZ#mysite.ladon.tools.multiparthandlerr	   Z"mysite.ladon.server.customresponser
   Zmysite.ladon.tools.logr   osre	tracebackr'   Zmysite.ladon.serverZdjango.confr   objectr   r   r   r   r   <module>   s    