o
    ˷e                     @   s   d Z ddlZddlmZ ddlmZ g dZdd Zdd	 Ze	d
Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )zUtility functions for handling XRIs.

@see: XRI Syntax v2.0 at the U{OASIS XRI Technical Committee<http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=xri>}
    N)reduce)	codecutil)!=@+$(c                 C   s"   |  ds| r| d tv rdS dS )zWDetermine if this identifier is an XRI or URI.

    @returns: C{"XRI"} or C{"URI"}
    xri://r   XRIURI)
startswithXRI_AUTHORITIES)
identifier r   G/var/www/ideatree/venv/lib/python3.10/site-packages/openid/yadis/xri.pyidentifierScheme   s   r   c                 C   s   |  ds	d|  } t| S )z$Transform an XRI to IRI-normal form.r
   )r   escapeForIRIxrir   r   r   toIRINormal   s   
r   z	\((.*?)\)c                 C   s0   |   }|dd}|dd}|dd}|S )zKEscape things that need to be escaped if they're in a cross-reference.
    /z%2F?z%3F#z%23)groupreplace)
xref_matchxrefr   r   r   _escape_xref%   s
   r   c                 C   s   |  dd} tt| } | S )zBEscape things that need to be escaped when transforming to an IRI.%z%25)r   _xref_resubr   r   r   r   r   r   /   s   r   c                 C   s   t t| S )z$Transform an XRI to URI normal form.)iriToURIr   r   r   r   r   toURINormal6   s   r#   c                 C   s(   t | trt| dd} | jddd S )z.Transform an IRI to a URI by escaping unicode.zutf-8)encodingasciioid_percent_escape)errors)
isinstancebytesstrencodedecode)irir   r   r   r"   ;   s   
r"   c                 C   s   | d}|d| }|| kS )zKIs this provider ID authoritative for this XRI?

    @returntype: bool
    r   N)rindex)
providerIDcanonicalIDlastbangparentr   r   r   providerIsAuthoritativeC   s   
r3   c                 C   s   |  dr| dd } | ddd }|d dkr(|d|dd  }t|S |d tv r6|d }t|S |d	}ttjd
d |D }|d }t|S )zReturn the root authority for an XRI.

    Example::

        rootAuthority("xri://@example") == "xri://@"

    @type xri: unicode
    @returntype: unicode
    r
      Nr      r   r	   )r   c                 S   s   g | ]}| d qS )*)split).0sr   r   r   
<listcomp>h   s    z!rootAuthority.<locals>.<listcomp>)r   r8   indexr   r   list__add__r   )r   	authorityrootsegmentsr   r   r   rootAuthorityN   s   



rB   c                 C   s   |  ds	d|  } | S )a)  An XRI object allowing comparison of XRI.

    Ideally, this would do full normalization and provide comparsion
    operators as per XRI Syntax.  Right now, it just does a bit of
    canonicalization by ensuring the xri scheme is present.

    @param xri: an xri string
    @type xri: unicode
    r
   )r   r   r   r   r   r   n   s   

r   )__doc__re	functoolsr   openidr   r   r   r   compiler    r   r   r#   r"   r3   rB   r   r   r   r   r   <module>   s   

 