
    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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  G d d      Zy)a  
Utilities for obtaining information about accessible hypertext and hyperlinks
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$zCopyright (c) 2024 Igalia, S.L.LGPL    N)urlparseAtspiz2.0)r      )debug)AXObjectc                       e Zd ZdZed        Zed        Zed        Zed        Zedd       Z	ed        Z
ed        Zedd	       Zed
        Zed        Zy)AXHypertextzNUtilities for obtaining information about accessible hypertext and hyperlinks.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 number of hyperlinks in obj.r   z+AXHypertext: Exception in _get_link_count: TNzAXHypertext:zreports z hyperlinks)
r   supports_hypertextr   	Hypertextget_n_links	Exceptionr   printMessage
LEVEL_INFOprintTokens)objcounterrormsgtokenss        3/usr/lib/python3/dist-packages/orca/ax_hypertext.py_get_link_countzAXHypertext._get_link_count3   s     **3/	OO//4E !#%'DE%**FD9  	?wGCu//d;	s   A' '	B$0*BB$c                     t        j                  |       sy	 t        j                  j	                  | |      }|S # t
        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)z4Returns the hyperlink object at the specified index.Nz.AXHypertext: Exception in _get_link_at_index: T)	r   r   r   r   get_linkr   r   r   r   )r   indexlinkr   r   s        r   _get_link_at_indexzAXHypertext._get_link_at_indexE   sp     **3/	??++C7D   	B5'JCu//d;	s    : 	A7*A22A7c                    g }t        t        j                  |             D ]k  }t        j                  | |      }|t        j	                  |      cxk  r|k  s$n |t        j                  |      cxk  r|k  sXn [|j                  |       m dt        |       d| d| d| g}t        j                  t        j                  |d       |S )zIReturns all the hyperlinks in obj who started within the specified range.AXHypertext:  hyperlinks found inzbetween start: z
 and end: T)ranger
   r   r   get_link_start_offsetget_link_end_offsetappendlenr   r   r   )r   start_offset
end_offsetlinksir   r   s          r   get_all_links_in_rangez"AXHypertext.get_all_links_in_rangeU   s     {22378 	#A11#q9D{@@FSS!@!@!FT*TT"		# "#e*-ABC#L>J<HJ%**FD9    c                    g }t        t        j                  |             D ],  }t        j                  | |      }||j	                  |       . dt        |       d| g}t        j                  t        j                  |d       |S )z,Returns a list of all the hyperlinks in obj.r    r!   T)	r"   r
   r   r   r%   r&   r   r   r   )r   r)   r*   r   r   s        r   get_all_linkszAXHypertext.get_all_linkse   s     {22378 	#A11#q9DT"	#
 "#e*-ABCH%**FD9r,   c                 f   	 t         j                  j                  |       }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 URI associated with obj at the specified index.z(AXHypertext: Exception in get_link_uri: TN zAXHypertext: URI ofz	at index z is )
r   
Accessibleget_hyperlink	Hyperlinkget_urir   r   r   r   r   )r   r   r   urir   r   r   s          r   get_link_urizAXHypertext.get_link_uris   s    	##11#6D//))$6C (	%SE.JK%**FD9
  	<UGDCu//d;	s   ?A3 3	B0<*B++B0c                    t        | t        j                        r| }nt        j                  j	                  |       }|*d| g}t        j                  t
        j                  |d       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)z7Returns the start offset of obj in the associated text.N'AXHypertext: Couldn't get hyperlink forTz1AXHypertext: Exception in get_link_start_offset: zAXHypertext: Start offset ofis )
isinstancer   r3   r1   r2   r   r   r   get_start_indexr   r   r   r   r   offsetr   r   s         r   r#   z!AXHypertext.get_link_start_offset   s     c5??+D##11#6D<?EFe..=	__44T:F 1#VH~F%**FD9  	EeWMCu//d;	   *B8 8	C5*C00C5c                    t        | t        j                        r| }nt        j                  j	                  |       }|*d| g}t        j                  t
        j                  |d       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)z5Returns the end offset of obj in the associated text.Nr8   Tr9   z/AXHypertext: Exception in get_link_end_offset: zAXHypertext: End offset ofr:   )r;   r   r3   r1   r2   r   r   r   get_end_indexr   r   r=   s         r   r$   zAXHypertext.get_link_end_offset   s     c5??+D##11#6D<?EFe..=	__2248F /s6(^D%**FD9  	CE7KCu//d;	r?   c                 r   t         j                  | |      }|syt        |      }t        j                  j                  |j                        }|r9t        j                  j                  |      d   }t        j                  dd|      }d| d| dg}t        j                  t        j                  |d       |S )	z>Strip directory and suffix off of the URL associated with obj.r0   r   z[-_] zAXHypertext: Basename for linkzis ''T)r
   r6   r   ospathbasenamesplitextresubr   r   r   )r   r   remove_extensionr5   
parsed_urirG   r   s          r   get_link_basenamezAXHypertext.get_link_basename   s     &&sE2c]
GG$$Z__5ww''1!4HvvgsH5H2C4z9KL%**FD9r,   c                 R   t        j                  |       sy	 t        j                  j	                  | |      }|dk  ryt        j                  | |      }|y	 t        j                  j                  |d      }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# t
        $ r4}d| }t        j                  t        j                  |d       Y d}~yd}~ww xY w)zAReturns the embedded-object child of obj at the specified offset.Nz/AXHypertext: Exception in get_child_at_offset: Tr   zAXHypertext: Child at offset z inis)r   r   r   r   get_link_indexr   r   r   r   r
   r   r3   
get_objectr   )r   r>   r   r   r   r   childr   s           r   get_child_at_offsetzAXHypertext.get_child_at_offset   s    **3/	OO223?E 19--c59<	OO..tQ7E 2&=sD%P%**FD9+  	CE7KCu//d;	  	CE7KCu//d;	s/    B)  C) )	C&2*C!!C&)	D&2*D!!D&c                 ~    t        j                  t        j                  |             syt        j	                  |       S )zHReturns the offset of the embedded-object obj in the text of its parent.r9   )r   supports_text
get_parentr
   r#   )r   s    r   get_character_offset_in_parentz*AXHypertext.get_character_offset_in_parent   s1     %%h&9&9#&>?0055r,   N)r   )r   F)__name__
__module____qualname____doc__staticmethodr   r   r+   r.   r6   r#   r$   rM   rS   rW    r,   r   r
   r
   0   s    X "          0  0  "  > 6 6r,   r
   )r[   __id____version____date____copyright____license__rE   rI   urllib.parser   girequire_versiongi.repositoryr   r0   r   	ax_objectr   r
   r]   r,   r   <module>rh      sX   * 1 	 	 ! 	   7E "   ~6 ~6r,   