a
    ¦ˆce*  ã                   @   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mZ dZ	dZ
dZ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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dZdd„ ZdS )ÚSentinelRecordzM Sentinel record to separate groups of chained change event dispatches.

    © c                 C   s   dS )NÚ
r   ©Úselfr   r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/util/event_tracer.pyÚ__str__*   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 ©Nr   )r
   r   r   r   r   r   r   r   r   r   Ú__init__5   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   C   s    
ùzChangeMessageRecord.__str__N©r   r   r   r   r   r   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 )ÚCallingMessageRecordz0 Message record for a change handler call.

    ©r   r   ÚhandlerÚsourcec                 C   s   || _ || _|| _|| _d S r   r%   )r
   r   r   r&   r'   r   r   r   r   W   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   a   s    ûzCallingMessageRecord.__str__Nr#   r   r   r   r   r$   P   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 r   r-   )r
   r   r   r&   r.   r   r   r   r   s   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   }   s    
ûzExitMessageRecord.__str__Nr#   r   r   r   r   r,   l   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 r   )Ú_recordsr	   r   r   r   r   �   s    zRecordContainer.__init__c                 C   s   | j  |¡ dS )z- Add the record into the container.

        N)r2   Úappend)r
   Úrecordr   r   r   r4   ’   s    zRecordContainer.recordc                 C   sL   t |ddd��*}| jD ]}| t|ƒ¡ qW d  ƒ n1 s>0    Y  dS )z( Save the records into a file.

        Úwzutf-8)ÚencodingN)Úopenr2   ÚwriteÚstr)r
   ÚfilenameÚfhr4   r   r   r   Úsave_to_file™   s    
zRecordContainer.save_to_fileN)r   r   r   r   r   r4   r<   r   r   r   r   r1   ˆ   s   r1   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  ¡ | _i | _d S r   )Ú	threadingÚLockÚ_creation_lockÚ_record_containersr	   r   r   r   r   ¬   s    
z#MultiThreadRecordContainer.__init__c                 C   sR   | j �8 | j |¡}|du r,tƒ }|| j|< |W  d  ƒ S 1 sD0    Y  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)r@   rA   Úgetr1   )r
   Úthread_nameÚ	containerr   r   r   Úget_change_event_collector°   s    
z5MultiThreadRecordContainer.get_change_event_collectorc                 C   s`   | j �F | j}| ¡ D ]&\}}tj |d |¡¡}| |¡ qW d  ƒ n1 sR0    Y  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)r@   rA   ÚitemsÚosÚpathÚjoinr!   r<   )r
   Zdirectory_nameZ
containersrC   rD   r:   r   r   r   Úsave_to_directory¾   s    
ÿz,MultiThreadRecordContainer.save_to_directoryN)r   r   r   r   r   rE   rJ   r   r   r   r   r=   ¢   s   	r=   c                   @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
ÚChangeEventRecordera�   A single thread trait change event recorder.

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

    Attributes
    ----------
    container : MultiThreadRecordContainer
        A container to store the records for each trait change.
    indent : int
        Indentation level when reporting chained events.
    c                 C   s   d| _ || _d S )Né   )r   rD   ©r
   rD   r   r   r   r   Þ   s    zChangeEventRecorder.__init__c           	   
   C   sh   | j }t ¡  d¡}| j}| t||||||jjd�¡ | t	|||jt
 |¡d�¡ |  j d7  _ dS )zF Record a string representation of the trait change dispatch

        ú r   r%   rL   N)r   r   ÚutcnowÚ	isoformatrD   r4   r   Ú	__class__r   r$   ÚinspectÚgetsourcefile)	r
   Úobjr   r   r   r&   r   r   rD   r   r   r   Ú
pre_tracerâ   s,    úÿüÿzChangeEventRecorder.pre_tracerNc                 C   sl   t  ¡  d¡}|  jd8  _| j}|r2d |¡}	nd}	| j}
|
 t|||j|	d�¡ |dkrh|
 t	ƒ ¡ dS )zD Record a string representation of the trait change return

        rN   rL   z [EXCEPTION: {}]Ú r-   N)
r   rO   rP   r   r!   rD   r4   r,   r   r   )r
   rT   r   r   r   r&   r.   r   r   Zexception_msgrD   r   r   r   Úpost_tracerþ   s"    üÿ	zChangeEventRecorder.post_tracer)N)r   r   r   r   r   rU   rW   r   r   r   r   rK   Î   s   rK   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 )ÚMultiThreadChangeEventRecordera_   A thread aware trait change recorder.

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

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

    Attributes
    ----------
    container : MultiThreadChangeEventRecorder
        The container of RecordContainers to keep the trait change records
        for each thread.
    tracers : dict
        Mapping from threads to ChangeEventRecorder instances.
    c                 C   s   i | _ t ¡ | _|| _d S r   )Útracersr>   r?   Ú_tracer_lockrD   rM   r   r   r   r   .  s    
z'MultiThreadChangeEventRecorder.__init__c                 C   s0   | j � i | _W d  ƒ n1 s"0    Y  dS )z& Close and stop all logging.

        N)rZ   rY   r	   r   r   r   Úclose3  s    z$MultiThreadChangeEventRecorder.closec                 C   s   |   ¡ }| |||||¡ dS )zu The traits pre event tracer.

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

        N)Ú_get_tracerrU   )r
   rT   r   r   r   r&   Útracerr   r   r   rU   :  s    z)MultiThreadChangeEventRecorder.pre_tracerNc                 C   s"   |   ¡ }|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)r\   rW   )r
   rT   r   r   r   r&   r.   r]   r   r   r   rW   C  s    z*MultiThreadChangeEventRecorder.post_tracerc                 C   sŠ   | j �p t ¡ j}|| jvrP| j}| |¡}t|ƒ}|| j|< |W  d   ƒ S | j| W  d   ƒ S W d   ƒ n1 s|0    Y  d S r   )rZ   r>   Úcurrent_threadr   rY   rD   rE   rK   )r
   ÚthreadrD   Zthread_containerr]   r   r   r   r\   L  s    



z*MultiThreadChangeEventRecorder._get_tracer)N)	r   r   r   r   r   r[   rU   rW   r\   r   r   r   r   rX     s   	
	rX   c                  c   sT   t ƒ } t| d�}tj|j|jd� z| V  W t ¡  | ¡  nt ¡  | ¡  0 dS )aH   Multi-threaded trait change event tracer.

    Example
    -------

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

        >>> 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')

    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.

    )rD   )rU   rW   N)r=   rX   r   Zset_change_event_tracersrU   rW   Zclear_change_event_tracersr[   )rD   Zrecorderr   r   r   Úrecord_eventsY  s    
ÿ
ÿr`   )r   rR   rG   r>   Ú
contextlibr   r   Ztraitsr   r    r+   r0   r*   Úobjectr   r   r$   r,   r1   r=   rK   rX   r`   r   r   r   r   Ú<module>   s,   ÿÿ",K@