
    g
fI                        d Z dZdZdZdZdZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlZ ej                  d	d
       ddlmZ ddlmZ ddlmZ dZdZdZdZdZdZdZdZdZeZdZ eZ!dZ"dgZ#g dZ$g Z%g Z&g Z'dZ(dZ)d Z*d Z+d Z,d,dZ-d,dZ.d-dZ/d.d Z0d/d!Z1d0d"Z2d1d#Z3d2d$Z4ddl5Z5d% Z6d& Z7d' Z8d( Z9d) Z:d* Z;d3d+Z<y)4zProvides debug utilities for Orca.  Debugging is managed by a debug
level, which is held in the debugLevel field.  All other methods take
a debug level, which is compared to the current debug level to
determine if the content should be output.z$Id$z
$Revision$z$Date$z-Copyright (c) 2005-2008 Sun Microsystems Inc.LGPL    N)datetimeAtspiz2.0)r      )AXObject)AXUtilitiesi'  i  i  i   i  iX  i  i  orca)
	traceback	linecachelocalegettextloggingUserDict	encodings	posixpathgenericpathreTc                 x    | t         k\  r1t        |        t        j                  dt               t        |        yy)zrPrints out information regarding the current exception.

    Arguments:
    - level: the accepted debug level
    d   N)
debugLevelprintlnr
   	print_exc	debugFilelevels    ,/usr/lib/python3/dist-packages/orca/debug.pyprintExceptionr      s/     
C+     c                 z    | t         k\  r2t        |        t        j                  ddt               t        |        yy)zXPrints out the current stack.

    Arguments:
    - level: the accepted debug level
    Nr   )r   r   r
   print_stackr   r   s    r   
printStackr!      s1     
dC3 r   c                 &   t        | t        j                        r?t        j                  |       }t        j
                  |       }|r	|d| dz  }|sd}d| dS t        | t        j                        r~| j                   dt        | j                         dt        t        j                  | j                               d| j                   d	| j                   d	t        | j                         d
S t        | t        j                  t        j                  t        j                   t        j"                  t        j$                  f      r| j&                  S t        | t        j(                        r7d| j*                   d| j,                   d| j.                   d| j0                   d
	S t        | t2              r#dd	j5                  t7        t        |              dS t        | t8              rt;        |       dkD  r
| dd  d} | S t        | t<        j>                        rctA        | d      r<| jB                   d| jD                  jF                  jH                   d| jH                   S | jB                   d| jH                   S t        | t<        jJ                        rHtA        | d      r/| jD                  jF                  jH                   d| jH                   S | jH                   S t        | t<        jL                        rDtO        jP                  | jR                  jT                        }| d| jR                  jV                   S t        | tN        jX                        r0tO        jP                  | jZ                        }| d| j\                   S t9        |       S )Nz: ''DEAD[]z for z in z (z, )z(x:z, y:z, width:z	, height:r   r   z (...)__self__.)/
isinstancer   
Accessibler   get_role_nameget_nameEventtype	_asStringsourceget_applicationdetail1detail2any_dataRole	StateTypeCollectionMatchTypeTextBoundaryType
ScrollType
value_nickRectxywidthheightlistjoinmapstrlentypesFunctionTypehasattr
__module__r(   	__class____name__
MethodType	FrameTypeinspectgetmodulenamef_codeco_filenameco_name	FrameInfofilenamefunction)objresultnamemodule_names       r   r0   r0      s   #u''('',  %D6m#FF6(!}#u{{#xxji

34D11#**=>? @}Bs{{m2i.E-FaI	
 #

EOOU5N5N..0@0@B C~~#uzz"SUUG4whsyyk3::,aPP#t499SC012!44#sC3QsF#
#u))*3
#nn%Qs||'='='F'F&GqWW..!3<<.11#u''(3
#ll,,556a~FF,, #u'++CJJ,B,BCa

 2 2344#w(()++CLL9a~..s8Or   c                     | t         k  ry dj                  t        t        |            }t	        j
                  dd|      }t	        j
                  dd|      }t        | |||       y )N z
[ \u00A0]+z (?=[,.:)])(?![\n]) )r   rB   rC   r0   r   subr   )r   tokens	timestampstacktexts        r   printTokensrb      sU    z88C	6*+D66-d+D66("d3DE4E*r   c                 4    | t         k  ry t        | |||       y N)r   r   r   ra   r_   r`   s       r   printMessagerf     s    zE4E*r   c                    g }t        j                  t        j                               }t        j                         }t	        dt        |            D ]e  }||   }t        j                  |d         }||k(  r&|j                  dk(  r6||j                  E|j                  |       t        |      | k\  se n |j                          dj                  t        t        |            S )Nr   r   mainz > )rN   	getmodulecurrentframer`   rangerE   rU   rK   appendreverserB   rC   r0   )
max_framescallerscurrent_moduler`   iframemodules          r   _stackAsStringrt     s    G&&w';';'=>NMMOE1c%j! a""58,^#>>V#>V__4uw<:% OO::c)W-..r   c                    | t         k\  r|j                  dd      }|r=|j                  ddd       }t        j                         j	                  d       d| }|r|dt                z  }t        r	 t        j                  |dg       y
	 t        j                  j                  |dg       y
y
# t        $ r d}t        j                  |dg       Y y
t        $ r d	}t        j                  |dg       Y y
w xY w# t        $ r& d}t        j                  j                  |dg       Y y
t        $ r& d	}t        j                  j                  |dg       Y y
w xY w)zPrints the text to stderr unless debug is enabled.

    If debug is enabled the text will be redirected to the
    file debugFile.

    Arguments:
    - level: the accepted debug level
    - text: the text to print (default is a blank line)
    u   ￼z[OBJ]
                  z%H:%M:%S.%fz - r[   z#TypeError when trying to write textz#Exception when trying to write textN)r   replacer   nowstrftimert   r   
writelines	TypeError	Exceptionsysstderrre   s       r   r   r     s>    
||Hg.<<8*o6Dlln--m<=SGDa()**D3$$dD\24

%%tTl3'   3<$$dD\2 3<$$dD\23  4<

%%tTl3 4<

%%tTl34s0   4B0 !C; 0"C8!C87C8;,E)+EEc                    | t         k  ryt        j                         }|d   |d   }}|d   }t        j                  |      }t	        |j
                  j                               D ]  \  }}t        |      |j
                  |<    t        j                  t        j                  | dd      }	dt        j                  |d          d|d	    d
|d    dt        j                  |d          d|d	    |	 }
|
 d| }t        | |        y)zmPrints the return result, along with information about the
    method, arguments, and any errors encountered.Nr      r   r#   r\   zCALL:   r)      z (line z) -> z	
RESULT: )r   rN   r`   getargvaluesrA   localsitemsrD   rx   formatargvaluesrO   r   )r   rW   r`   currentprevrr   argskeyvaluefArgs
callStringstrings               r   printResultr   E  s    zMMOE!HeAhTGAJE &D4;;,,./ &
Uu:C&KK//6R@E 7((a12!DG9GDG9 M$$WQZ0171:,ug	G  |:fX.FEfXr   c                     t         r t         j                  |j                        syt        | t              } d|g}t        | ||       |rt        | d | |       yy)at  Prints out an Python Event object.  The given level may be
    overridden if the eventDebugLevel is greater.  Furthermore, only
    events with event types matching the eventDebugFilter regular
    expression will be printed.

    Arguments:
    - level: the accepted debug level
    - event: the Python Event to print
    - sourceInfo: additional string to print out
    NzOBJECT EVENT:rw   )eventDebugFiltermatchr/   maxeventDebugLevelrb   r   )r   event
sourceInfor_   r^   s        r   printObjectEventr   ]  s[      0 6 6uzz B'Eu%Fvy)(J<0)< r   c           	      P    | t         k\  r|rt        | t        | |||      |       yyy)a:  Lists the details of the given accessible with the given
    indentation.

    Arguments:
    - level: the accepted debug level
    - indent: a string containing spaces for indentation
    - accessible: the accessible whose details are to be listed
    - includeApp: if True, include information about the app
    N)r   r   getAccessibleDetails)r   indent
accessible
includeAppr_   s        r   printDetailsr   s  s1     
z$UJ
K	  *r   c                    | t         k  ry|r|dt        j                  |       dz   }n|}t        j                  |      r|dz  }|S dj	                  dd      t        j
                  |      z  }dj	                  dd      |t        j                  |      fz  }d	t        j                  |       d
}| dt        j                  |       }| dt        j                  |       d
}	| dt        j                  |       d
}
| dt        j                  |       d
}| dt        j                  |       d
}| dt        j                  |       d
}|| d| d| d|	 d|
 d| d| d| d| dz  }|S )zReturns a string, suitable for printing, that describes the
    given accessible.

    Arguments:
    - indent: A string to prefix the output with
    - includeApp: If True, include information about the app
                  for this accessible.
    r\   zapp='z' z(exception fetching data)z	name='%s'rv   z\nz%sdescription='%s'zrole='r#   zpath=zstates='zrelations='z	actions='zinterfaces='zattributes='r[   )r   r   application_as_stringis_deadrx   r-   get_descriptionr,   get_pathstate_set_as_stringrelations_as_stringactions_as_stringsupported_interfaces_as_stringattributes_as_string)r   accr   r   r   name_stringdesc_stringrole_stringpath_stringstate_string
rel_stringactions_stringiface_stringattr_strings                 r   r   r     s    zE("@"@"E!FbII--%%dE2X5F5Fs5KKK&..tU;	))#./0K811#67q9KHE("3"3C"8!9:KXXh&B&B3&G%HJL8;x'C'CC'H&IKJxy)C)CC)H(IKNX\(*Q*QRU*V)WWXYLHL)F)Fs)K(LANK
-qR-r.,b
".-r-r		F Mr   c                     d\  }}	 | j                   d   }| j                   d   }|j                  d      s|j                  d      r|d d }||fS # t        $ r Y ||fS w xY w)N)NN__file__rK   z.pycz.pyo)	f_globalsendswithr}   )rr   rT   rs   s      r   _getFileAndModuler     s    !Hf%??:., f%):):6)B}HV  
 Vs   A 	AAc                  z   t         st         S t         j                  } t        r1t	        j
                  |       }t	        j                  |      t        vryt        rt	        j                  |       t        vryt        r:t        t         j                  j                  t              D cg c]  }|s|	 c}syyc c}w )NFT)objEventTRACE_ONLY_PROCESSING_EVENTSr1   
TRACE_APPSr   r2   r-   TRACE_ROLESget_roleTRACE_EVENTSrC   r/   
startswith)eventSourceappr=   s      r   _shouldTraceItr     s    /////K&&{3S!3x((5[H8==33\BH!aAH Is   'B8/B8c                    t               syt        |       \  }}|r|st        S |t        v rt        S t        r |j                  d      d   t        vrt        S |dvrt        S | j                  }t        j                  ||      j                         }d| d| d| }|dk(  rqt        j                  |       }|d   D 	cg c]
  }	|	d	k7  s	|	 }
}		 t        t        |d
   j                  |
            }t%        |
      D ]  \  }}|d| d||    z  } |j'                         j                         }|r|d   dk(  r|dk(  rt        S | d| d}t)        t*        |       t        S c c}	w # t        $ rR t!        |
      dk(  r9t#        |
d   t              r&t        t        |d
   j                  |
d               }nt        cY S Y w xY w)a  Line tracing utility to output all lines as they are executed by
    the interpreter.  This is to be used by sys.settrace and is for
    debugging purposes.

    Arguments:
    - frame: is the current stack frame
    - event: 'call', 'line', 'return', 'exception', 'c_call', 'c_return',
             or 'c_exception'
    - arg:   depends on the event type (see docs for sys.settrace)
    Nr)   r   )calllinereturnzTRACE :z: r   selfr   r   z
  ARG =r   r   z (rv: r'   )r   r   traceitTRACE_IGNORE_MODULESTRACE_MODULESsplitf_linenor   getlinerstriprN   r   rA   rC   getr|   rE   r*   	enumeratestripr   	LEVEL_ALL)rr   r   argrT   rs   linenor   outputargvalsr=   keysvaluesrq   r   lineElementss                  r   r   r     s    (/Hf%%c*1-]B..^^FXv.557DfXQvhb/F&&u-"1:5af55	#gajnnd34F  o 	3FAsQvayk22F	3 ::<%%'LQ83F?N86#a(IvN' 6  	4yA~*T!Wd";c'!*..$q':; 	s   >
E$	E$"E) )AGGc           	          t        j                  ddddt        |       g      }|j                  d      j	                  d      }t        t        d |            }t        |      S )Nlsofz-wz-Ffz-pzUTF-8rv   c                 D    | xr | d   dk(  xr | dd  j                         S )Nr   fr   )isdigit)ss    r   <lambda>z getOpenFDCount.<locals>.<lambda>  s$    !"G!"G!" r   )
subprocesscheck_outputrD   decoder   rA   filterrE   )pidprocsfiless      r   getOpenFDCountr   
  sV    ##fdE4S$JKELL!''-EGOPEu:r   c                     	 t        j                  d|  d      }|j                         }|j                          |j                  dd      }|S # t        $ r d}Y !w xY w)Nz
cat /proc/z/cmdlinez(Could not obtain cmdline) r[   )ospopenreadcloser}   rx   )r   openFilecmdlines      r   
getCmdliner     s`    /88jX67--/ oofc*GN	  /./s   9A AAc                     t        j                  d|  dt         j                        j                  }|j	                         }|j                          |j                         D cg c]  }t        |       c}S c c}w )Nzpgrep T)shellstdout)r   PopenPIPEr   r   r   r   int)procNamer   pidsps       r   pidOfr     sb    &
 3&*'188>  ==?DNN JJL)qCF)))s   &A;c           
      L   | rt         }nt        }t        j                         }t	        |dt        j                  |      z  d       t        t        j                  |            D ]d  \  }}t        j                  |      }t        |      }t        |      }t        j                  |      }|dk(  rd}t	        |d|dz   ||||fz  d       f dg}	|	D ]Q  }t        |      }
|
st	        |d| d       !|
D ],  }t        |      }t        |      }t	        |d	||||fz  d       . S y )
NzINFO: Desktop has %i apps:Tr\   z"WARNING: Possible hang or dead appz)%3i. %s (pid: %s) %s file descriptors: %ir   apportzINFO: no pid for z*INFO: %s (pid: %s) %s file descriptors: %i)	LEVEL_OFFr   r   get_desktopr   r   get_child_countr   iter_childrenget_process_idr   r   r-   r   )forcer   desktoprq   r   r   cmdfdsrX   	otherAppsr   s              r   examineProcessesr
  $  sB   %%'GE/(2J2J72SSUYZH227;< 83%%c*oS!  %2:7DBsD#sC0126	88 
I 
6SzE.se4d; 	6CS/C %CEGS#./046	6
6r   )FF)   )r\   FFrd   )NF)TF)r\   T)F)=__doc____id____version____date____copyright____license__rN   r
   r   r   r   r~   rF   r   girequire_versiongi.repositoryr   	ax_objectr   ax_utilitiesr   r   LEVEL_SEVERELEVEL_WARNING
LEVEL_INFOLEVEL_CONFIGURATION
LEVEL_FINELEVEL_FINERLEVEL_FINESTr   r   r   r   r   r   r   r   r   r   r   r   r   r!   r0   rb   rf   rt   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   <module>r     sJ  (.
 ?   	 	  
   	   7E "   % 	
 
  
   
   	
 	    -  
    $ 

2h++/($4L 0=, *b &2h	*6r   