
    g
f$                         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	 e	j                  dd	       dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  G d d      Zy)a  
Utilities for obtaining document-related information about accessible objects.
These utilities are app-type- and toolkit-agnostic. Utilities that might have
different implementations or results depending on the type of app (e.g. terminal,
chat, web) or toolkit (e.g. Qt, Gtk) should be in script_utilities.py file(s).

N.B. There are currently utilities that should never have custom implementations
that live in script_utilities.py files. These will be moved over time.
z$Id$z
$Revision$z$Date$zGCopyright (c) 2024 Igalia, S.L.Copyright (c) 2024 GNOME Foundation Inc.LGPL    NAtspiz2.0)r      )debug)messages)AXCollection)AXObject)AXTable)AXUtilitiesc                   (   e Zd ZdZi Z ej                         Zed        Z	ed        Z
ed        Zed        Zed        Zed        Zed        Zed	        Zed
        Zed        Zed        Zed        Zed        Zed        Zedd       Zy)
AXDocumentzNUtilities for obtaining document-related information about accessible objects.c                      	 t        j                  d       d} t        j                  t        j                  | d       t
        j                  j                          [)z*Clears any data we have cached for objectsT<   z!AXDocument: Clearing local cache.)timesleepr   printMessage
LEVEL_INFOr   LAST_KNOWN_PAGEclear)msgs    2/usr/lib/python3/dist-packages/orca/ax_document.py_clear_stored_datazAXDocument._clear_stored_data<   sF     JJrN5Cu//d;&&,,.	     c                  z    t        j                  t        j                        } d| _        | j                          y)z3Starts thread to periodically clear cached details.)targetTN)	threadingThreadr   r   daemonstart)threads    r   start_cache_clearing_threadz&AXDocument.start_cache_clearing_threadF   s*     !!)F)FGr   c                    t        j                  |       syt        j                  j	                  t        |             }|t        j                  |       k7  }|r-d| d| g}t        j                  t        j                  |d       |S )z)Returns True if the current page changed.FzAXDocument: Previous page ofzwas T)
r	   supports_documentr   r   gethash_get_current_pager   printTokensr   )documentold_pageresulttokenss       r   did_page_changezAXDocument.did_page_changeN   sw     ))(3--11$x.AZ99(CC4h$xj@QRFe..=r   c                 L   t        j                  |       sy	 t        j                  j	                  |       }d| d| g}t        j                  t        j                  |d       |S # t
        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)%Returns the current page of document.r   z,AXDocument: Exception in _get_current_page: TNzAXDocument: Current page ofis )
r	   r#   r   Documentget_current_page_number	Exceptionr   r   r   r'   )r(   pageerrorr   r+   s        r   r&   zAXDocument._get_current_page]   s     ))(3	>>99(CD 0S<H%**FD9  	@HCu//d;	   A& &	B#/*BB#c                     t        j                  |       syt        j                  |       }|t        j                  t        |       <   |S )r.   r   )r	   r#   r   r&   r   r%   )r(   r3   s     r   get_current_pagezAXDocument.get_current_pageo   s>     ))(3++H559
""4>2r   c                 L   t        j                  |       sy	 t        j                  j	                  |       }d| d| g}t        j                  t        j                  |d       |S # t
        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)z#Returns the page count of document.r   z)AXDocument: Exception in get_page_count: TNzAXDocument: Page count ofr/   )
r	   r#   r   r0   get_page_countr2   r   r   r   r'   )r(   countr4   r   r+   s        r   r9   zAXDocument.get_page_countz   s     ))(3	NN11(;E .x3ugG%**FD9  	=eWECu//d;	r5   c                 N   t        j                  |       sy	 t        j                  j	                  |       }d| d| dg}t        j                  t        j                  |d       |S # t
        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)zReturns the locale of document. z%AXDocument: Exception in get_locale: TNzAXDocument: Locale ofzis '')
r	   r#   r   r0   
get_localer2   r   r   r   r'   r(   r*   r4   r   r+   s        r   r>   zAXDocument.get_locale   s     ))(3	^^..x8F *8tF815EF%**FD9  	9%ACu//d;	s   A' '	B$0*BB$c                 N   t        j                  |       si S 	 t        j                  j	                  |       }d| d|g}t        j                  t        j                  |d       |S # t
        $ r6}d| }t        j                  t        j                  |d       i cY d}~S d}~ww xY w)z8Returns a dict with the document-attributes of document.z/AXDocument: Exception in _get_attributes_dict: TNzAXDocument: Attributes ofzare:)
r	   r#   r   r0   get_document_attributesr2   r   r   r   r'   r?   s        r   _get_attributes_dictzAXDocument._get_attributes_dict   s     ))(3I	^^;;HEF .xH%**FD9  	CE7KCu//d;I	s   A% %	B$.+BB$B$c                     t        j                  |       syt        j                  |       }|j	                  d|j	                  dd            S )Returns the uri of document.r<   DocURLURIr	   r#   r   rB   r$   r(   
attributess     r   get_urizAXDocument.get_uri   sA     ))(344X>
~~h
ub(ABBr   c                 |    t        j                  |       syt        j                  |       }|j	                  dd      S )rD   r<   MimeTyperG   rH   s     r   get_mime_typezAXDocument.get_mime_type   s6     ))(344X>
~~j"--r   c                 2    t         j                  |       dk(  S )z2Returns True if document is a plain-text document.z
text/plain)r   rM   )r(   s    r   is_plain_textzAXDocument.is_plain_text   s     ''1\AAr   c                     t         j                  |       }|dk(  ry|dk(  r$t         j                  |       j                  d      S y)z+Returns True if document is a PDF document.zapplication/pdfTz	text/htmlz.pdfF)r   rM   rJ   endswith)r(   	mime_types     r   is_pdfzAXDocument.is_pdf   sG     ,,X6	))#%%h/88@@r   c                 ~    t         j                  j                  t        j	                  |             }|j
                  S )z/Returns the fragment portion of document's uri.)urllibparseurlparser   rJ   fragment)r(   r*   s     r   get_document_uri_fragmentz$AXDocument.get_document_uri_fragment   s-     &&z'9'9('CDr   c                 J   ddddddd}t         j                  j                  t         j                  j                  t         j                  j                  t         j                  j
                  t         j                  j                  g}t        j                  |      }t        j                  | |      }|D ]  }t        j                  |      r|dxx   dz  cc<   &t        j                  |      r|dxx   dz  cc<   It        j                  |      r#t        j                  |      s|dxx   dz  cc<   t        j                   |      r1t        j"                  |      r|dxx   dz  cc<   |d	xx   dz  cc<   t        j$                  |      s|d
xx   dz  cc<    |S )zAReturns a dictionary of object counts used in a document summary.r   )forms	landmarksheadingstablesunvisited_linksvisited_links)rolesr]   r   r[   r^   r`   r_   r\   )r   RoleHEADINGLINKTABLEFORMLANDMARKr   create_match_ruleget_all_matchesr   
is_headingis_formis_tabler
   is_layout_tableis_link
is_visitedis_landmark)r(   r*   ra   rulematchesobjs         r   _get_object_countszAXDocument._get_object_counts   s]     %&#$& ##!!$$	& --E:..x> 	)C%%c*z"a'"$$S)w1$%%c*73J3J33Ox A% $$S)))#.?+q0+,-2-((-{#q(#	) r   c                 "   g }t         j                  |       }|j                  t        j                  |j                  dd      |             |j                  t        j                  |j                  dd      |             |j                  t        j                  |j                  dd      |             |j                  t        j                  |j                  dd      |             |j                  t        j                  |j                  dd      |             |j                  t        j                  |j                  dd      |             t        t        d |            }|sy	t        j                  d
j                  |      z  S )zNReturns a string summarizing the document's structure and objects of interest.r\   r   r]   r[   r^   r`   r_   c                     | S )N )xs    r   <lambda>z1AXDocument.get_document_summary.<locals>.<lambda>  s    q r   r<   z, )r   rt   appendr   landmarkCountr$   headingCount	formCount
tableCountvisitedLinkCountunvisitedLinkCountlistfilterPAGE_SUMMARY_PREFIXjoin)r(   only_if_foundr*   countss       r   get_document_summaryzAXDocument.get_document_summary  s&    ..x8h,,VZZQ-GWXh++FJJz1,E}UVh((GQ)?OPh))&**Xq*A=QRh//

?A0NP]^_h11&**=NPQ2RTabcf[&12++dii.???r   N)T)__name__
__module____qualname____doc__r   r   Lock_lockstaticmethodr   r!   r,   r&   r7   r9   r>   rB   rJ   rM   rO   rS   rY   rt   r   rw   r   r   r   r   6   sI   XOINNE/ /      "    "  "  " C C . . B B
     " "H @ @r   r   )r   __id____version____date____copyright____license__r   r   rU   girequire_versiongi.repositoryr   r<   r   r   ax_collectionr   	ax_objectr	   ax_tabler
   ax_utilitiesr   r   rw   r   r   <module>r      sh   , ;    	   7E "    '   %[@ [@r   