
     Tiq!                        % S r SSKJr  SSKrSSKJr  / SQrSSKrSSKrSSK	r	SSK
r
SSKJrJr  SSKJr  SSKJr  SqS	\S
'   \R(                  " SS9 " S S5      5       rSS jrSS jr " S S5      rg)z)Journaling system for ONNX IR operations.    )annotationsN)Any)JournalJournalEntryget_current_journal)CallableSequence)Self)	_wrappersJournal | None_current_journalT)frozenc                      \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'   S\S'   S\S'   \SS j5       rSS jrSr	g)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detailsc                >    U R                   c  gU R                  5       $ )zTGet the referenced object, or None if it has been garbage-collected or not recorded.N)r   selfs    ]/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx_ir/journaling/_journaling.pyobjJournalEntry.obj5   s     88xxz    c           
     D   [         R                   R                  U R                  5      R                  S5      n[	        SS S35        [	        SU 35        [	        SU R
                   35        [	        SU R                   SU R                   S	35        U R                  c  SnO!U R                  5       =nb  [        U5      nOSn[	        S5        UR                  S5       H  n[	        SU 35        M     U R                  (       a;  [	        S5        U R                  R                  S5       H  n[	        SU 35        M     U R                  (       Ga>  S
n[        U R                  5       H/  nUR                  R                  SS5      nSU;  d  SU;   d  M-  Un  O   [	        S5        Ub^  [	        SUR                   SUR                    SUR"                   S35        UR$                  (       a  [	        SUR$                   S35        O[	        S5        [	        S5        U R                   Hb  n[	        SUR                   SUR                    SUR"                   S35        UR$                  (       d  MI  [	        SUR$                   S35        Md     [	        SS S35        g
)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fr:   s           r"   displayJournalEntry.display<   sR    %%33DNNCLL"
	 	z)*))56)$..)9:;%doo%6eDNN;K1MN 88$KXXZc,s)K%K%&%%d+DBtf+ , <<*+**404&k" 1 Jd../::--dC8H,0@H0L!"J	 0 56% !4!4 5Qz7H7H6IjooM^^ef ??N:??*;7CD2334%%
1::,azaffXWMN666L89 & 	z)*r%    N)returnz
Any | NonerE   None)
__name__
__module____qualname____firstlineno____doc____annotations__propertyr#   rB   __static_attributes__rD   r%   r"   r   r      sL    " NLO	N-- 4+r%   r   c                     [         $ )z Get the current journal, if any.)r   rD   r%   r"   r   r   s   s    r%   c                 2    [         R                  " 5       S S $ )N)	tracebackextract_stackrD   r%   r"   _get_stack_tracerU   x   s    ""$Sb))r%   c                  v    \ rS rSrSrSS jrSS jrSS jr\SS j5       r	SSS jjr
SS	 jrSS
 jrSS jrSrg)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"
    ]
c                <    / U l         S U l        / U l        0 U l        g N)_entries_previous_journal_hooks_original_methodsr    s    r"   __init__Journal.__init__   s     ,.15<>68r%   c                V    [         U l        U q [        R                  " U 5      U l        U $ rY   )r   r[   r   wrap_ir_classesr]   r    s    r"   	__enter__Journal.__enter__   s(    !1!*!:!:4!@r%   c                \    [         R                  " U R                  5        U R                  qg rY   )r   restore_ir_classesr]   r[   r   )r!   exc_type	exc_valueexc_tbs       r"   __exit__Journal.__exit__   s"    $$T%;%;<11r%   c                    U R                   $ )z!Get all recorded journal entries.)rZ   r    s    r"   entriesJournal.entries   s     }}r%   Nc                F   [        [        R                  " 5       UUR                  UR                  R                  Ub  [        R
                  " U5      OS[        U5      [        5       US9nU R                  R                  U5        U R                   H  nU" U5        M     g)zRecord a new journal entry.N)r   r   r   r   r   r   r   r   )r   time	__class__rH   weakrefr   idrU   rZ   appendr\   )r!   r#   r   r   entryhooks         r"   recordJournal.record   sw    iik==}}--$'OC g(*	
 	U#KKDK  r%   c                :    U R                   R                  U5        g)zHAdd a hook that will be called whenever a new journal entry is recorded.N)r\   rs   )r!   ru   s     r"   add_hookJournal.add_hook   s    4 r%   c                8    U R                   R                  5         g)zClear all hooks.N)r\   clearr    s    r"   clear_hooksJournal.clear_hooks   s    r%   c                   U R                    GH  nUR                  (       a  SUR                   S3OSn[        R                  R                  UR                  5      R                  S5      nUR                  (       ay  Sn[        UR                  5       H/  nUR                  R                  SS5      nSU;  d  S	U;   d  M-  Un  O   Ub)  UR                   S
UR                   SUR                   3nOSnOSn[        5         [        SU SU S35        UR                  c  SnOHUR                  5       =n	b3  [        U	5      R                  SS5      n[        U5      S:  a  USS S-   nOSnUR                  SS5      n
[        U
5      S:  a  U
SS S-   n
[        SUR                    SUR"                   SUR$                   SU S3	5        U(       d  GM  [        SU
 S35        GM     g)zDisplay all journal entries.z [] r'   Nr-   r.   r/   r0   r1   r2   z	<unknown>[z] [90mr(   r*   r,   z\nd   _   z[...]r+   zClass: z(id=z). Operation: z
. Object: .z[90mDetails: )rZ   r   r3   r4   r   r5   r   r9   r:   r;   r<   r=   r6   r   r7   lenr   r   r   )r!   rt   r   r   framerA   r:   locationr?   r#   details_texts              r"   rB   Journal.display   s   ]]E/4}}5==/+"G ))77HQQ&I   !%"3"34A zz11$<H 04D4P ! 5 $"'..!15<<.UZZLQH*H&GAi[
8*G<=yy ($#1"3i//e<{#c)"-cr"2W"<K)"??47L< 3&+CR07:%**+4/@uN__ijuivvwx w),w?@K #r%   )rZ   r\   r]   r[   rF   )rE   r
   )rE   zSequence[JournalEntry]rY   )r#   r   r   r   r   r   rE   rG   )ru   zCallable[[JournalEntry], None]rE   rG   )rH   rI   rJ   rK   rL   r^   rb   ri   rN   rl   rv   ry   r}   rB   rO   rD   r%   r"   r   r   |   s?    492
   !'Ar%   r   )rE   r   )rE   r   )rL   
__future__r   rq   typingr   __all__dataclassesr3   ro   rS   collections.abcr   r	   typing_extensionsr
   onnx_ir.journalingr   r   rM   	dataclassr   r   rU   r   rD   r%   r"   <module>r      sx    0 "  
<     . " (#' . ' d#V+ V+ $V+r
*qA qAr%   