
    >gB                      n    d Z dZdZddlZg Zd Zd ZddZ G d d	      Z e       j                  a
dd
Zy)a  
.. module:: agitb
   :synopsis: More comprehensive traceback formatting for Python scripts.

Example
-------

To enable this module, do:

.. code-block:: python

   import asterisk.agitb, asterisk.agi
   asterisk.agitb.enable(display = False, logdir = '/var/log/asterisk/')

   agi = asterisk.agi.AGI()
   asterisk.agitb.enable(agi, False, '/var/log/asterisk')

at the top of your script.  The optional arguments to enable() are:

* agi         - the agi handle to write verbose messages to
* display     - if true, tracebacks are displayed on the asterisk console
  (used with the agi option)
* logdir      - if set, tracebacks are written to files in this directory
* context     - number of lines of source code to show for each stack frame

By default, tracebacks are displayed but not saved, and the context is 5 lines.

You may want to add a logdir if you call agitb.enable() before you have
an agi.AGI() handle.

Alternatively, if you have caught an exception and want agitb to display it
for you, call agitb.handler().  The optional argument to handler() is a
3-item tuple (etype, evalue, etb) just like the value of sys.exc_info().
If you do not pass anything to handler() it will use sys.exc_info().

This script was adapted from Ka-Ping Yee's cgitb.

Specification
-------------
zMatthew Nicholsonz0.1.0    Nc                 $   | |v rd||    fS | |j                   v rd|j                   |    fS d|j                   v rQ|j                   d   }t        |t        i             r| |v rd||    fS dt
        fS t        ||       rdt	        ||       fS dt
        fS )z9Find the value for a given name in the given environment.localglobal__builtins__builtinN)	f_globals
isinstancetypehasattrgetattr	__UNDEF__)nameframelocalsbuiltinss       A/opt/asterisk_venv/lib/python3.12/site-packages/asterisk/agitb.pylookupr   2   s    v~t$$u...(??>2hR)x (4.00 ? x& '(D"999?    c                    ddl }ddl}g dddt        f\  }}}}}	|j                  |       D ]  \  }
}}}}|
|j                  k(  r |S |
|j
                  k(  ri||j                  vr[|dk(  r1|t        urct        ||t              }	|j                  ||z   ||	f       n:t        |||      \  }}	|j                  |||	f       n|dk(  r||dz   z  }|	}nd\  }}|} |S )zEScan one logical line of Python and look up values of variables used.r   N .)Nr   )
tokenizekeywordr   generate_tokensNEWLINENAMEkwlistr   appendr   )readerr   r   r   r   vars	lasttokenparentprefixvaluettypetokenstartendlinewheres                   r   scanvarsr+   C   s   -/tR-J*D)VVU*2*B*B6*J &ueS$H$$$ K HMM!e7>>&AC*#FE9=EKK% ?@%eUF;uUE512c\i#o%FF%NFF	!" Kr   c           
      l  %&'( ddl }ddl}ddl}ddl}ddl'ddl}ddl(| \  }}}	t        ||j                        r|j                  }dt        j                  j                         d   z   dz   t        j                  z   }
|j                  |j                               }t        |      d|
d|ddz   }g }|j!                  |	|      }|D ]  \  }%}}}}%xr |j"                  j%                  %      xs d%|j'                  |      \  }}}}d}|dk7  rd	|z   |j)                  ||||(fd
      z   }i &|gf%&'fd	}t+        |||      }d%d|g}|8||z
  }|D ].  }d|z  }|j-                  ||j/                         z          |dz  }0 i g } }|D ]  \  }!}"}#|!|v rd||!<   |#t0        urZ|"dk(  rd|!z   }!n|"dk(  r|!}!n|"|!j                  d      d   z   }!| j-                  |!d(j2                  j5                  |#             s| j-                  |!dz           |j-                  dj7                  |              |j-                  ddj7                  |      z          t        |      dt        |      g}$t        ||j8                        rNt;        |      D ]@  }!(j2                  j5                  t=        ||!            }#|$j-                  dd|!d|#       B ddl}|dj7                  |      z   dj7                  |$      z   ddj7                  |j?                  |||	            z  z   S )z:Return a plain text document describing a given traceback.r   NzPython z: 
z
A problem occurred in a Python script.  Here is the sequence of
function calls leading up to the error, in the order they occurred.
?r   zin c                 @    dj                   j                  |       z   S )N=)textrepr)r$   pydocs    r   <lambda>ztext.<locals>.<lambda>y   s    #

X]H^B^ r   )formatvaluec                 ~    d| d   <   	 j                  | d         | dxx   dz  cc<   S # | dxx   dz  cc<   w xY w)N   r   )getline)lnumfile	highlight	linecaches    r   r   ztext.<locals>.reader}   sE    !"Id1g ((tAw7Q1Q1s   - < z%5d r7   r   zglobal r   r   z = z
 undefinedz
%s
z    zc

The above is a description of an error in a Python program.  Here is
the original traceback:

%s
) ostypestime	tracebackr<   inspectr3   r	   	ClassType__name__sysversionsplit
executablectimestrgetinnerframespathabspathgetargvaluesformatargvaluesr+   r   rstripr   r1   r2   joinInstanceTypedirr   format_exception))eparamscontextr?   r@   rA   rB   rC   etypeevalueetbpyverdateheadframesrecordsr   r9   funclinesindexargsvarargsvarkwr   callr   r    rowsir)   numdonedumpr   r*   r$   	exceptionr:   r;   r<   r3   s)                                        @@@@r   r1   r1   \   s<    E63%)))+A..5FE::diik"D Z5 9 D
 F$$S'2G18 ,2-tT4--4'.';';E'B$guf3;4<''guf4^ ( ``D 	 	 v. $'(uA qjC$++-/0Q
 d"& 	1D%t|DJI%H$$t+Dg%D 4::c?2#66Duzzu/EFGD</0	1 	DIIdO$h401Y,2\ !ZV56I&%,,-K 	EDJJOOGFD$9:EgtUCD	E "''&/!BGGI$66 : 	ggi((<=:> > >r   c                   *    e Zd ZdZ	 	 ddZd ZddZy)Hookz?A hook to replace sys.excepthook that shows tracebacks in HTML.Nc                 n    || _         || _        || _        |xs t        j                  | _        || _        y N)displaylogdirrW   rF   stderrr:   agi)selfrq   rr   rW   r:   rt   s         r   __init__zHook.__init__   s.    &CJJ	r   c                 ,    | j                  |||f       y rp   )handle)ru   rX   rY   rZ   s       r   __call__zHook.__call__   s    UFC()r   c                    |xs t        j                         }	 t        || j                        }| j                  r]| j                  r3|j                  d      D ]  }| j                  j                  |d         n| j                  j                  |dz          | j                  r| j                  j                  dd       n| j                  j                  d       | j                  dd l}dd l}|j!                  d| j                        \  }}	 |j#                  |d	      }	|	j                  |       |	j%                          d
|z  }
| j                  r| j                  j                  |
d       n| j                  j                  |
dz          	 | j                  j'                          y #  dd l}dj                   |j                  |       }Y xY w#  d|z  }
Y xY w#  Y y xY w)Nr   r   r-      z$A problem occured in a python scriptz%A problem occured in a python script
z.txt)suffixrT   wz*%s contains the description of this error.z*Tried to save traceback to %s, but failed.)rF   exc_infor1   rW   rB   rR   rU   rq   rt   rH   verboser:   writerr   r?   tempfilemkstempfdopencloseflush)ru   infodocrB   r)   r?   r   fdrM   r:   msgs              r   rx   zHook.handle   s   %s||~	=tT\\*C
 <<xxIIdO .DHH$$T1-. 		d
+88HHCQGIIOODE;;"!))T[[)IJRJyyS)

3

BTI xx  a(		d
+	IIOOE	=''4)44d;<C.JBTI	s#   F- 8G G# -$GG #G')r7   N   NNrp   )rE   
__module____qualname____doc__rv   ry   rx    r   r   rn   rn      s    I?C*)r   rn   c                 P    t        ||||       }|t        _        |j                  ay)a  Install an exception handler that formats tracebacks as HTML.

    The optional argument 'display' can be set to 0 to suppress sending the
    traceback to the browser, and 'logdir' can be set to a directory to cause
    tracebacks to be written to files there.)rq   rr   rW   rt   N)rn   rF   
excepthookrx   handler)rt   rq   rr   rW   except_hooks        r   enabler      s+     wv&C1K CN   Gr   )r   )Nr7   Nr   )r   
__author____version__rF   r   r   r+   r1   rn   rx   r   r   r   r   r   <module>r      sM   'R !
 
	"2Q>h7 7t &--!r   