3
8ßL]F*  ã               @   s   d 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Zddlm	Z	 dZ
dZdZd	ZejG d
d„ deƒƒZejG dd„ deƒƒZejG dd„ deƒƒZejG dd„ deƒƒ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d„ ƒZdS )zH Record trait change events in single and multi-threaded environments.

é    N)Úcontextmanager)Údatetime)Útrait_notifierszc{time} {direction:-{direction}{length}} {name!r} changed from {old!r} to {new!r} in {class_name!r}
z0{time} {action:>{gap}}: {handler!r} in {source}
zE{time} {direction:-{direction}{length}} EXIT: {handler!r}{exception}
é	   c               @   s   e Zd ZdZf Zdd„ ZdS )ÚSentinelRecordzM Sentinel record to separate groups of chained change event dispatches.

    c             C   s   dS )NÚ
© )Úselfr   r   ú</tmp/pip-install-5sebvq2j/traits/traits/util/event_tracer.pyÚ__str__0   s    zSentinelRecord.__str__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r   r   r
   r   (   s   r   c               @   s$   e Zd ZdZdZdd	„ Zd
d„ ZdS )ÚChangeMessageRecordz2 Message record for a change event dispatch.

    ÚtimeÚindentÚnameÚoldÚnewÚ
class_namec             C   s(   || _ || _|| _|| _|| _|| _d S )N)r   r   r   r   r   r   )r	   r   r   r   r   r   r   r   r   r
   Ú__init__<   s    zChangeMessageRecord.__init__c          	   C   s,   | j d }tj| jd| j| j| j| j|d�S )Né   ú>)r   Ú	directionr   r   r   r   Úlength)r   Ú	CHANGEMSGÚformatr   r   r   r   r   )r	   r   r   r   r
   r   J   s    
zChangeMessageRecord.__str__N)r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r
   r   4   s   r   c               @   s$   e Zd ZdZdZdd„ Zdd	„ Zd
S )ÚCallingMessageRecordz0 Message record for a change handler call.

    r   r   ÚhandlerÚsourcec             C   s   || _ || _|| _|| _d S )N)r   r   r    r!   )r	   r   r   r    r!   r   r   r
   r   _   s    zCallingMessageRecord.__init__c             C   s(   | j d t }tj| jd| j| j|d�S )Nr   ZCALLING)r   Úactionr    r!   Úgap)r   Ú#SPACES_TO_ALIGN_WITH_CHANGE_MESSAGEÚ
CALLINGMSGr   r   r    r!   )r	   r#   r   r   r
   r   i   s    zCallingMessageRecord.__str__N)r   r   r    r!   )r   r   r   r   r   r   r   r   r   r   r
   r   W   s   
r   c               @   s$   e Zd ZdZdZdd„ Zdd	„ Zd
S )ÚExitMessageRecordzA Message record for returning from a change event dispatch.

    r   r   r    Ú	exceptionc             C   s   || _ || _|| _|| _d S )N)r   r   r    r'   )r	   r   r   r    r'   r   r   r
   r   |   s    zExitMessageRecord.__init__c             C   s$   | j d }tj| jd| j| j|d�S )Nr   ú<)r   r   r    r'   r   )r   ÚEXITMSGr   r   r    r'   )r	   r   r   r   r
   r   †   s    
zExitMessageRecord.__str__N)r   r   r    r'   )r   r   r   r   r   r   r   r   r   r   r
   r&   t   s   
r&   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚRecordContainerzi A simple record container.

     This class is commonly used to hold records from a single thread.

    c             C   s
   g | _ d S )N)Ú_records)r	   r   r   r
   r   ˜   s    zRecordContainer.__init__c             C   s   | j j|ƒ dS )z- Add the record into the container.

        N)r+   Úappend)r	   Úrecordr   r   r
   r-   ›   s    zRecordContainer.recordc             C   s:   t |dƒ�&}x| jD ]}|jtj|ƒƒ qW W dQ R X dS )z( Save the records into a file.

        ÚwN)Úopenr+   ÚwriteÚsixÚ	text_type)r	   ÚfilenameÚfhr-   r   r   r
   Úsave_to_file¢   s    zRecordContainer.save_to_fileN)r   r   r   r   r   r-   r5   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S )	ÚMultiThreadRecordContainera   A container of record containers that are used by separate threads.

    Each record container is mapped to a thread name id. When a RecordContainer
    does not exist for a specific thread a new empty RecordContainer will be
    created on request.


    c             C   s   t jƒ | _i | _d S )N)Ú	threadingÚLockÚ_creation_lockÚ_record_containers)r	   r   r   r
   r   µ   s    
z#MultiThreadRecordContainer.__init__c          
   C   s:   | j �* | jj|ƒ}|dkr,tƒ }|| j|< |S Q R X dS )z¨ Return the dedicated RecordContainer for the thread.

        If no RecordContainer is found for `thread_name` then a new
        RecordContainer is created.

        N)r9   r:   Úgetr*   )r	   Úthread_nameÚ	containerr   r   r
   Úget_change_event_collector¹   s    
z5MultiThreadRecordContainer.get_change_event_collectorc             C   sR   | j �B | j}x4tj|ƒD ]&\}}tjj|dj|ƒƒ}|j|ƒ qW W dQ R X dS )z› Save records files into the directory.

        Each RecordContainer will dump its records on a separate file named
        <thread_name>.trace.

        z	{0}.traceN)	r9   r:   r1   Ú	iteritemsÚosÚpathÚjoinr   r5   )r	   Zdirectory_nameZ
containersr<   r=   r3   r   r   r
   Úsave_to_directoryÇ   s    z,MultiThreadRecordContainer.save_to_directoryN)r   r   r   r   r   r>   rC   r   r   r   r
   r6   «   s   r6   c               @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
ÚChangeEventRecorderz3 A single thread trait change event recorder.

    c             C   s   d| _ || _dS )z¶ Class constructor

        Parameters
        ----------
        container : MultiThreadRecordContainer
           An container to store the records for each trait change.

        é   N)r   r=   )r	   r=   r   r   r
   r   Ü   s    	zChangeEventRecorder.__init__c       	   	   C   sh   | j }tjƒ jdƒ}| j}|jt||||||jjd�ƒ |jt	|||jt
j|ƒd�ƒ |  j d7  _ dS )zF Record a string representation of the trait change dispatch

        ú )r   r   r   r   r   r   )r   r   r    r!   rE   N)r   r   ÚutcnowÚ	isoformatr=   r-   r   Ú	__class__r   r   ÚinspectÚgetsourcefile)	r	   Úobjr   r   r   r    r   r   r=   r   r   r
   Ú
pre_tracerè   s$    zChangeEventRecorder.pre_tracerNc             C   sl   t jƒ jdƒ}|  jd8  _| j}|r2dj|ƒ}	nd}	| j}
|
jt|||j|	d�ƒ |dkrh|
jt	ƒ ƒ dS )zD Record a string representation of the trait change return

        rF   rE   z [EXCEPTION: {}]Ú )r   r   r    r'   N)
r   rG   rH   r   r   r=   r-   r&   r   r   )r	   rL   r   r   r   r    r'   r   r   Zexception_msgr=   r   r   r
   Úpost_tracer  s    
zChangeEventRecorder.post_tracer)N)r   r   r   r   r   rM   rO   r   r   r   r
   rD   ×   s   rD   c               @   s:   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ ZdS )ÚMultiThreadChangeEventRecorderzª A thread aware trait change recorder.

    The class manages multiple ChangeEventRecorders which record trait change
    events for each thread in a separate file.

    c             C   s   i | _ tjƒ | _|| _dS )zã Object constructor

        Parameters
        ----------
        container : MultiThreadChangeEventRecorder
            The container of RecordContainers to keep the trait change records
            for each thread.

        N)Útracersr7   r8   Ú_tracer_lockr=   )r	   r=   r   r   r
   r   '  s    

z'MultiThreadChangeEventRecorder.__init__c          	   C   s   | j � i | _W dQ R X dS )z& Close and stop all logging.

        N)rR   rQ   )r	   r   r   r
   Úclose5  s    z$MultiThreadChangeEventRecorder.closec             C   s   | j ƒ }|j|||||ƒ dS )zu The traits pre event tracer.

        This method should be set as the global pre event tracer for traits.

        N)Ú_get_tracerrM   )r	   rL   r   r   r   r    Útracerr   r   r
   rM   <  s    z)MultiThreadChangeEventRecorder.pre_tracerNc             C   s"   | j ƒ }|j||||||d� dS )zw The traits post event tracer.

        This method should be set as the global post event tracer for traits.

        )r'   N)rT   rO   )r	   rL   r   r   r   r    r'   rU   r   r   r
   rO   E  s    z*MultiThreadChangeEventRecorder.post_tracerc          
   C   sZ   | j �J tjƒ j}|| jkrB| j}|j|ƒ}t|ƒ}|| j|< |S | j| S W d Q R X d S )N)rR   r7   Úcurrent_threadr   rQ   r=   r>   rD   )r	   Úthreadr=   Zthread_containerrU   r   r   r
   rT   N  s    



z*MultiThreadChangeEventRecorder._get_tracer)N)	r   r   r   r   r   rS   rM   rO   rT   r   r   r   r
   rP     s   	
	rP   c           
   c   sD   t ƒ } t| d�}tj|j|jd� z
| V  W dtjƒ  |jƒ  X dS )aJ   Multi-threaded trait change event tracer.

    Usage
    -----
    ::

        >>> from trace_recorder import record_events
        >>> with record_events() as change_event_container:
        ...     my_model.some_trait = True
        >>> change_event_container.save_to_directory('C:\dev\trace')

    This will install a tracer that will record all events that occur from
    setting of some_trait on the my_model instance.

    The results will be stored in one file per running thread in the
    directory 'C:\dev\trace'.  The files are named after the thread being
    traced.

    )r=   )rM   rO   N)r6   rP   r   Zset_change_event_tracersrM   rO   Zclear_change_event_tracersrS   )r=   Zrecorderr   r   r
   Úrecord_events[  s    

rX   )r   rJ   r@   r7   Ú
contextlibr   r   r1   Ztraitsr   r   r%   r)   r$   Úpython_2_unicode_compatibleÚobjectr   r   r   r&   r*   r6   rD   rP   rX   r   r   r   r
   Ú<module>   s0   ",H<