o
    Xiq!                     @  s   U d Z ddlmZ ddlZddlmZ g dZddlZddlZddl	Z	ddl
Z
ddlmZmZ ddlmZ ddlmZ dad	ed
< ejddG dd dZdddZdddZG dd dZdS )z)Journaling system for ONNX IR operations.    )annotationsN)Any)JournalJournalEntryget_current_journal)CallableSequence)Self)	_wrappersJournal | None_current_journalT)frozenc                   @  sj   e Zd ZU dZded< ded< ded< ded< d	ed
< ded< ded< ded< edddZdddZdS )r   a  A single journal entry recording an operation on the IR.

    Attributes:
        timestamp: The time at which the operation was performed.
        operation: The name of the operation performed.
        class_: The class of the object on which the operation was performed.
        class_name: The name of the class of the object.
        ref: A weak reference to the object on which the operation was performed.
            To access the object, call ``ref()``. Note that ``ref`` may be ``None``,
            and ``ref()`` may return ``None`` if the object has been garbage-collected.
        obj: The referenced object, or None if it has been garbage-collected or not recorded.
            This is the same as calling ``entry.ref() if entry.ref is not None else None``.
        object_id: The unique identifier of the object (id()).
        stack_trace: The stack trace at the time of the operation.
        details: Additional details about the operation.
    float	timestampstr	operationtypeclass_
class_namezweakref.ref | Nonerefint	object_idlist[traceback.FrameSummary]stack_trace
str | Nonedetailsreturn
Any | Nonec                 C  s   | j du rdS |   S )zTGet the referenced object, or None if it has been garbage-collected or not recorded.N)r   self r    R/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/journaling/_journaling.pyobj5   s   
zJournalEntry.objNonec              	   C  s  t j | jd}tdd d td|  td| j  td| j d| j d	 | jd
u r6d}n|   }d
urCt	|}nd}td |
dD ]	}td|  qN| jrotd | j
dD ]	}td|  qe| jrd
}t| jD ]}|jdd}d|vsd|v r|} nqytd |d
urtd|j d|j d|j d |jrtd|j d ntd td | jD ]}td|j d|j d|j d |jrtd|j d qtdd d d
S )z:Display the journal entry in a detailed multi-line format.%Y-%m-%d %H:%M:%S.%fz[1mzP================================================================================[0mz[1mTimestamp:[0m z[1mOperation:[0m z[1mClass:[0m z (id=)N<no ref>	<deleted>z[1mObject:[0m
z  z[1mDetails:[0m\/onnx_ironnx_ir/passesz[1mUser Code Location:[0mz  [90m: in z  [90m>>> z  [90m<unknown>[0mz[1mFull Stack Trace:[0mz	    [90m)datetimefromtimestampr   strftimeprintr   r   r   r   reprsplitr   r   reversedfilenamereplacelinenonameline)r   r   object_reprr"   r;   
user_framefr7   r    r    r!   display<   sV   


"zJournalEntry.displayN)r   r   r   r#   )__name__
__module____qualname____doc____annotations__propertyr"   r?   r    r    r    r!   r      s   
 r   r   c                   C  s   t S )z Get the current journal, if any.)r   r    r    r    r!   r   s   s   r   r   c                   C  s   t  d d S )N)	tracebackextract_stackr    r    r    r!   _get_stack_tracex   s   rJ   c                   @  sf   e Zd ZdZdddZd ddZdd	d
Zed!ddZd"d#ddZ	d$ddZ
dddZdddZdS )%r   a  A journaling system to record operations on the ONNX IR.

    It can be used as a context manager to automatically record operations within a block.

    Example::

        from onnx_ir.journaling import Journal

        journal = Journal()

        with Journal() as journal:
            # Perform operations on the ONNX IR
            pass

        for entry in journal.entries:
            print(f"Operation: {entry.operation} on {entry.class_name}")


    You can also filter the entries by operation or class name using the `filter` method::

        filtered_entries = [
            entry for entry in journal.entries
            if entry.operation == "set_name" and entry.class_name == "Node"
        ]
    r   r#   c                 C  s   g | _ d | _g | _i | _d S N)_entries_previous_journal_hooks_original_methodsr   r    r    r!   __init__   s   
zJournal.__init__r	   c                 C  s   t | _| a t| | _| S rK   )r   rM   r
   wrap_ir_classesrO   r   r    r    r!   	__enter__   s   zJournal.__enter__c                 C  s   t | j | jad S rK   )r
   restore_ir_classesrO   rM   r   )r   exc_type	exc_valueexc_tbr    r    r!   __exit__   s   
zJournal.__exit__Sequence[JournalEntry]c                 C  s   | j S )z!Get all recorded journal entries.)rL   r   r    r    r!   entries   s   zJournal.entriesNr"   r   r   r   r   r   c              
   C  s^   t t ||j|jj|durt|ndt|t |d}| j	| | j
D ]}|| q&dS )zRecord a new journal entry.N)r   r   r   r   r   r   r   r   )r   time	__class__rA   weakrefr   idrJ   rL   appendrN   )r   r"   r   r   entryhookr    r    r!   record   s   


zJournal.recordr`   Callable[[JournalEntry], None]c                 C  s   | j | dS )zHAdd a hook that will be called whenever a new journal entry is recorded.N)rN   r^   )r   r`   r    r    r!   add_hook   s   zJournal.add_hookc                 C  s   | j   dS )zClear all hooks.N)rN   clearr   r    r    r!   clear_hooks   s   zJournal.clear_hooksc                 C  s|  | j D ]}|jrd|j dnd}tj|jd}|jrPd}t|jD ]}|j	dd}d|vs6d	|v r:|} nq%|durM|j d
|j
 d|j }nd}nd}t  td| d| d |jdu rhd}n!|  }	durt|		dd}t|dkr|dd d }nd}|	dd}
t|
dkr|
dd d }
td|j d|j d|j d| d	 |rtd|
 d qdS )zDisplay all journal entries.z [] r$   Nr*   r+   r,   r-   r.   r/   z	<unknown>[z] [90mr%   r'   r)   z\nd   _   z[...]r(   zClass: z(id=z). Operation: z
. Object: .z[90mDetails: )rL   r   r0   r1   r   r2   r   r6   r7   r8   r9   r:   r3   r   r4   lenr   r   r   )r   r_   r   r   framer>   r7   locationr<   r"   details_textr    r    r!   r?      sJ   

"zJournal.displayr@   )r   r	   )r   rX   rK   )r"   r   r   r   r   r   r   r#   )r`   rb   r   r#   )rA   rB   rC   rD   rP   rR   rW   rF   rY   ra   rc   re   r?   r    r    r    r!   r   |   s    




r   )r   r   )r   r   )rD   
__future__r   r\   typingr   __all__dataclassesr0   rZ   rH   collections.abcr   r   typing_extensionsr	   onnx_ir.journalingr
   r   rE   	dataclassr   r   rJ   r   r    r    r    r!   <module>   s$   

Y
