Mini Shell

Direktori : /lib/python3.6/site-packages/orca/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/orca/__pycache__/label_inference.cpython-36.pyc

3

�2[-L�@s>dZdZdZdZdZdZddlZdd	lmZGd
d�d�Z	dS)zDHeuristic means to infer the functional/displayed label of a widget.z$Id$z
$Revision$z$Date$z$Copyright (C) 2011-2013 Igalia, S.L.ZLGPL�N�)�debugc@s�eZdZdd�Zd/dd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zd1dd�Z
dd�Zd2dd�Zdd�Zd3d!d"�Zd4d$d%�Zd5d'd(�Zd6d)d*�Zd7d,d-�Zd.S)8�LabelInferencecCs||_i|_i|_i|_dS)z�Creates an instance of the LabelInference class.

        Arguments:
        - script: the script with which this instance is associated.
        N)�_script�
_lineCache�
_extentsCache�_isWidgetCache)�selfZscript�r
�%/usr/lib/python3.6/label_inference.py�__init__$szLabelInference.__init__TcCs�tjtjd|�|sdgfS|rJ|j�jtj�rJtjtjd�dgfSdg}}|sx|j|�\}}tjtjd|�|s�|j|�r�|j	|�p�|\}}tjtjd|�|s�|j
|�\}}tjtjd|�|s�|j|�\}}tjtjd|�|�s|j|�\}}tjtjd|�|�s>|j
g}}tjtjd	|�|�rX|j�}|jd
d�}|�s�|j|dd
�\}}tjtjd|�|j�||fS)z�Attempt to infer the functional/displayed label of obj.

        Arguments
        - obj: the unlabeled widget
        - focusedOnly: If True, only infer if the widget has focus.

        Returns the text which we think is the label, or None.
        zINFER label for: %sNzINFER - object not focusedzINFER - Text Left: %szINFER - Text Right: %szINFER - Table: %szINFER - Text Above: %szINFER - Text Below: %szINFER - Name: %s�
� ��)�	proximityz+INFER - Text Left with proximity of 200: %s)r�println�
LEVEL_FINE�getState�contains�pyatspiZ
STATE_FOCUSED�inferFromTextLeft�_preferRight�inferFromTextRight�inferFromTable�inferFromTextAbove�inferFromTextBelow�name�strip�replace�
clearCache)r	�objZfocusedOnly�result�objectsr
r
r�infer0sB

zLabelInference.infercCsi|_i|_i|_dS)z5Dumps whatever we've stored for performance purposes.N)rrr)r	r
r
rrfszLabelInference.clearCachecCstjtjg}|j�|kS)zeReturns True if we should prefer text on the right, rather than the
        left, for the object obj.)r�ROLE_CHECK_BOX�ROLE_RADIO_BUTTON�getRole)r	r ZonRightRolesr
r
rrmszLabelInference._preferRightcCstjtjtjg}|j�|kS)zeReturns True if we should not permit inference based on text to
        the right for the object obj.)r�ROLE_COMBO_BOX�	ROLE_LIST�
ROLE_LIST_BOXr&)r	r �rolesr
r
r�
_preventRighttszLabelInference._preventRightcCstjtjtjg}|j�|kS)zZReturns True if we should prefer text above, rather than below for
        the object obj.)rr'r(r)r&)r	r r*r
r
r�
_preferTop~szLabelInference._preferTopcCstjtjg}|j�|kS)zZReturns True if we should not permit inference based on text below
        the object obj.)r�
ROLE_ENTRY�ROLE_PASSWORD_TEXTr&)r	r r*r
r
r�
_preventBelow�szLabelInference._preventBelowcCs�|sdSydd�|D�}Wn*ttfk
rDtjtjd|�dSXdd�|D�}t|�dkrddSy|j�}Wntk
r�dSX|jdd	�j	�}|j
|jj�dkr�dSdS)
z�Returns True if the given object has 'simple' contents, such as text
        without embedded objects or a single embedded object without text.FcSsg|]}|�qSr
r
)�.0Zchildr
r
r�
<listcomp>�sz2LabelInference._isSimpleObject.<locals>.<listcomp>zDead Accessible in %scSsg|]}|j�tjkr|�qSr
)r&rZ	ROLE_LINK)r0�xr
r
rr1�srTr���)
�LookupError�RuntimeErrorrrr�lenZ	queryText�NotImplementedErrorZgetTextr�countrZEMBEDDED_OBJECT_CHARACTER)r	r Zchildren�text�stringr
r
r�_isSimpleObject�s$zLabelInference._isSimpleObjectcCs.|sdStjtjg}|j�|kr$dS|j|�S)zBReturns True if the given object should not be treated as a label.T)rZROLE_HEADINGZROLE_LIST_ITEMr&�	_isWidget)r	r ZnonLabelTextRolesr
r
r�_cannotLabel�szLabelInference._cannotLabelcCs�|sdS|jjt|��}|dk	r$|Stjtjtjtjtjtj	tj
tjtjtj
tjg}|j�|k}|rz|j�jtj�rzd}||jt|�<|S)z-Returns True if the given object is a widget.FNT)r�get�hashrr$r%ZROLE_TOGGLE_BUTTONr'r(r)Z	ROLE_MENUZROLE_MENU_ITEMr-r.ZROLE_PUSH_BUTTONr&rrZSTATE_EDITABLE)r	r �rvZwidgetRolesZisWidgetr
r
rr<�s*zLabelInference._isWidgetrrc	Cs�|sdS|jjt|�||f�}|r&|Sd}|jjj|�}|rptjtjg}|j	�|krp|dkrb|j
}|j||d�}|do~|ds�|j�j
d�}|j|j|j|jf}||jt|�||f<|S)z�Returns (x, y, width, height) of the text at the given offsets
        if the object implements accessible text, or just the extents of
        the object if it doesn't implement accessible text.rr��)rrrr)rrrrr3)rr>r?r�	utilitiesZqueryNonEmptyTextrr-r.r&ZcharacterCountZgetRangeExtentsZqueryComponentZ
getExtentsr2�y�width�height)	r	r ZstartOffsetZ	endOffsetr@�extentsr9ZskipTextExtentsZextr
r
r�_getExtents�s$zLabelInference._getExtentscCsx|j|�sdgfS|j|�r$dgfS|jjj|dd�}dd�|D�}tt|j|��r\dgfSdd�|D�}dj|�|fS)z>Gets the functional label text associated with the object obj.NF)ZuseCachecSsg|]}|d�qS)rr
)r0�contentr
r
rr1�sz;LabelInference._createLabelFromContents.<locals>.<listcomp>cSsg|]}|d�qS)rBr
)r0rIr
r
rr1s�)	r;r=rrCZgetObjectContentsAtOffset�list�filterr<�join)r	r �contentsr"�stringsr
r
r�_createLabelFromContents�s

z'LabelInference._createLabelFromContentscCsd|jjt|��}|r|St|�}|j|�rB|jjj|�\}}|j}|jjj||dd�}||j|<|S)zeGet the (obj, startOffset, endOffset, string) tuples for the line
        containing the object, obj.TF)	rr>r?r<rrCZgetHyperlinkRange�parentZgetLineContentsAtOffset)r	r �startr@�key�endr
r
r�_getLineContentss

zLabelInference._getLineContentscCs@|j�}|dks|jS|j|d}|r<|jr<||jd}|S)zGets the object prior to obj.rr)ZgetIndexInParentrQ�
childCount)r	r �index�prevObjr
r
r�_getPreviousObjects
z!LabelInference._getPreviousObject�Kc
sp|j��}|j��}�fdd�|D�}y|j|d�}Wntk
rTt|�}YnX|d|�}d}xZtt|�dd
d�D]B}	||	\}
}}}
|j|
�}|d|dks�|j|
�r||	d}Pq|W||d�}|o�|ds�dgfS|d\}
}}}|j|
||�}|d|d|d}d|k�o,|kn�rhdd�|D�}dj|�j�}|�rh|d	d�|D�fSdgfS)
aLAttempt to infer the functional/displayed label of obj by
        looking at the contents of the current line, which are to the
        left of this object

        Arguments
        - obj: the unlabeled widget
        - proximity: pixels expected for a match

        Returns the text which we think is the label, or None.
        csg|]}|d�kr|�qS)rr
)r0�o)r r
rr10sz4LabelInference.inferFromTextLeft.<locals>.<listcomp>rrNrAcSsg|]}|d�qS)rBr
)r0rIr
r
rr1GsrJcSsg|]}|d�qS)rr
)r0rIr
r
rr1Jsr3r3r3)	rHrUrW�
IndexErrorr6�ranger=rMr)r	r rrGrNrIrWZonLeftrR�iZlObjZlStartZlEndZlStringZlExtentsrTr:�distancerOr!r
)r rr"s6


z LabelInference.inferFromTextLeft�c
sr|j��rdgfS|j��}|j��}�fdd�|D�}y|j|d�}Wntk
rft|�}YnX|tt|�|d�d�}t|�}x>t|�D]2\}	}
|j|
d�r�|j	��s�dgfS|	d}Pq�W|d|�}|o�|ds�dgfS|d\}}}}
|j|||�}|d|d|d}||k�s6|j	���rjdd�|D�}dj
|�j�}|�rj|d	d�|D�fSdgfS)
aMAttempt to infer the functional/displayed label of obj by
        looking at the contents of the current line, which are to the
        right of this object

        Arguments
        - obj: the unlabeled widget
        - proximity: pixels expected for a match

        Returns the text which we think is the label, or None.
        Ncsg|]}|d�kr|�qS)rr
)r0r[)r r
rr1_sz5LabelInference.inferFromTextRight.<locals>.<listcomp>rrrAcSsg|]}|d�qS)rBr
)r0rIr
r
rr1vsrJcSsg|]}|d�qS)rr
)r0rIr
r
rr1ys)r+rHrUrWr\r6�min�	enumerater=rrMr)r	r rrGrNrIrWZonRightrTr^�itemZrObjrRr:ZrExtentsr_rOr!r
)r rrNs:



z!LabelInference.inferFromTextRight�cs:|j��}�fdd�|D�}y|j|d�}Wntk
rFdgfSX|dkrXdgfS|jjj|dd|ddd�\}}|j||�}t|�dkr�dgfS|d\}}	}
}|j|�r�dgfS|j��r2|j	||	|
�\}}
}}|j	��\}}}}||
|}d|k�o|kn�r2||k�r2|j�|gfSdgfS)aHAttempt to infer the functional/displayed label of obj by
        looking at the contents of the line above the line containing
        the object obj.

        Arguments
        - obj: the unlabeled widget
        - proximity: pixels expected for a match

        Returns the text which we think is the label, or None.
        csg|]}|d�kr|�qS)rr
)r0r[)r r
rr1�sz5LabelInference.inferFromTextAbove.<locals>.<listcomp>rNrT)
rUrWr\rrCZpreviousContextr6r=rrH)r	r r�thisLinerIrWrXZ
prevOffsetZprevLinerRrTr:r2rDrErF�objX�objY�objWidth�	objHeightr_r
)r rr}s.



&z!LabelInference.inferFromTextAbovecsF|j��rdgfS|j��}�fdd�|D�}y|j|d�}Wntk
rXdgfSX|dkrjdgfS|jjj|dd|d	ddd�\}}|j||�}t|�dkr�dgfS|d\}}	}
}|j|�r�dgfS|j	��r>|j
||	|
�\}}
}}|j
��\}}}}|
||}d|k�o(|kn�r>|j	�|gfSdgfS)
aHAttempt to infer the functional/displayed label of obj by
        looking at the contents of the line above the line containing
        the object obj.

        Arguments
        - obj: the unlabeled widget
        - proximity: pixels expected for a match

        Returns the text which we think is the label, or None.
        Ncsg|]}|d�kr|�qS)rr
)r0r[)r r
rr1�sz5LabelInference.inferFromTextBelow.<locals>.<listcomp>rrrATr3r3)r/rUrWr\rrCZnextContextr6r=rrH)r	r rrerIrWZnextObjZ
nextOffsetZnextLinerRrTr:r2rDrErFrfrgrhrir_r
)r rr�s2


"

z!LabelInference.inferFromTextBelow�2c&sJdd�}tjj�|�}|j|�s(dgfS|�j�jjgkrBdgfSdd�}tjj||�}|sddgfSy|j��Wntk
r�dgfSX|jjj	|�}�j
|�}�j|��|j��\}}	}
}�dko�|j
���r�j|�d�}|j|�\}
}|
�r|
j�|fS��jk�r�|j���r��j|�d�}|j|�\}}}}|||
}||k�sb|j
���r�|j|�\}
}|
�r�|
j�|fSd}}}}|dk�r؈j|d��}|j|�\}}|�r�|j���r�|j�|fS|�jk�r|j���r�j|d��}|j|�\}}|�rz|�rz|j|�\}}}}|j|�\}}} }!|	||}"||	|}#|"|#k�rn|j�|fS|j�|fS|�r�|j�|fS|�r�|j�|fS�fdd�t�j�D�}$|$�s�tt|j|$���r�dgfS��fd	d�td�j�D�}%d
d�|%D�}%�fdd�|%D��rdgfS|j|$��\}
}|
�rB|
j�|fSdgfS)anAttempt to infer the functional/displayed label of obj by looking
        at the contents of the surrounding table cells. Note that this approach
        assumes a simple table in which the widget is the sole occupant of its
        cell.

        Arguments
        - obj: the unlabeled widget

        Returns the text which we think is the label, or None.
        cSs|j�tjkS)N)r&rZROLE_TABLE_CELL)r2r
r
r�<lambda>�sz/LabelInference.inferFromTable.<locals>.<lambda>NcSs|j�tjkS)N)r&rZ
ROLE_TABLE)r2r
r
rrk�srrcsg|]}�jd|��qS)r)�getAccessibleAt)r0r^)�tabler
rr1sz1LabelInference.inferFromTable.<locals>.<listcomp>csg|]}�j|���qSr
)rl)r0r^)�colrmr
rr1"scSsg|]}|dk	r|�qS)Nr
)r0r2r
r
rr1#scs*g|]"}|jr|dj��j�kr|�qS)r)rVr&)r0r2)r r
rr1$s)rZutilsZfindAncestorr;rQZ
queryTabler7rrCZ	cellIndexZ
getRowAtIndexZgetColumnAtIndexrHrrlrPrZnColumnsr+r,ZnRowsr/r]rKrLr<)&r	r ZproximityForRightZpredZcellZgridrW�rowrfrgrhri�	candidateZlabelZsourcesr2rDrErFr_Z	cellAboveZ	cellBelowZ
labelAboveZ
labelBelowZsourcesAboveZsourcesBelowZaboveXZaboveYZ
aboveWidthZaboveHeightZbelowXZbelowYZ
belowWidthZbelowHeightZdAboveZdBelowZfirstRowZcellsr
)rnr rmrr�s~





zLabelInference.inferFromTableN)Tr3)rr3)r)rZ)r`)rd)rd)rj)�__name__�
__module__�__qualname__rr#rrr+r,r/r;r=r<rHrPrUrYrrrrrr
r
r
rr"s&
6

	


,
/
(
+r)
�__doc__Z__id__�__version__Z__date__Z
__copyright__Z__license__rrJrrr
r
r
r�<module>s