o
    ˷e                      @   sf  d Z g dZddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 e	 Ze
 ZddlmZ G dd	 d	eZG d
d deZdd ZdZdZdd Zdd Zdd ZedZedZedZedZedZedZedZdd Zdd  Z d7d!d"Z!d#d$ Z"ej#G d%d& d&e$Z%e% Z&d'd( Z'd)d* Z(d+d, Z)d-d. Z*d/d0 Z+d1d2 Z,d3d4 Z-d5d6 Z.dS )8z+
ElementTree interface to an XRD document.
)nsTagmkXRDTagisXRDS	parseXRDSgetCanonicalIDgetYadisXRDgetPriorityStrictgetPriorityprioSortiterServicesexpandServiceexpandServices    N)datetime)strptime)importElementTreeimportSafeElementTree)xric                   @   s   e Zd ZdZdZdS )	XRDSErrorz An error with the XRDS document.N)__name__
__module____qualname____doc__reason r   r   I/var/www/ideatree/venv/lib/python3.10/site-packages/openid/yadis/etxrd.pyr   $   s    r   c                   @   s   e Zd ZdZdS )	XRDSFraudzbRaised when there's an assertion in the XRDS that it does not have
    the authority to make.
    N)r   r   r   r   r   r   r   r   r   +   s    r   c                 C   s   zt | tr| dn| }t|}W n  ttttfy     t	y3 } z	t
d}||_|d}~ww t|}t|sAt
d|S )zParse the given text as an XRDS document.

    @return: ElementTree containing an XRDS document

    @raises XRDSError: When there is a parse error or the document does
        not contain an XRDS.
    utf8zError parsing document as XMLNzNot an XRDS document)
isinstancestrencodeSafeElementTreeXML
SystemExitMemoryErrorAssertionErrorImportError	Exceptionr   r   ElementTreer   )text
bytestringelementwhyexctreer   r   r   r   1   s   
r   zxri://$xrd*($v*2.0)zxri://$xrdsc                 C   s   d| |f S )Nz{%s}%sr   )nstr   r   r   r   S      r   c                 C   
   t t| S )zybasestring -> basestring

    Create a tag name in the XRD 2.0 XML namespace suitable for using
    with ElementTree
    )r   
XRD_NS_2_0r/   r   r   r   r   W      
r   c                 C   r1   )zvbasestring -> basestring

    Create a tag name in the XRDS XML namespace suitable for using
    with ElementTree
    )r   XRDS_NSr3   r   r   r   	mkXRDSTag`   r4   r6   XRDSServiceXRDTypeURIExpiresCanonicalIDc                 C   s   |   }|jtkS )z"Is this document an XRDS document?)getroottagroot_tag)xrd_treerootr   r   r   r   u   s   
r   c                 C   s(   d}|  tD ]}q|du rtd|S )z=Return the XRD element that should contain the Yadis servicesNzNo XRD present in tree)findallxrd_tagr   rA   xrdr   r   r   r   {   s   r   c                 C   s6   |  t}|du r|S |j}t|d}t|dd  S )a  Return the expiration date of this XRD element, or None if no
    expiration was specified.

    @type xrd_element: ElementTree node

    @param default: The value to use as the expiration if no
        expiration was specified in the XRD.

    @rtype: datetime.datetime

    @raises ValueError: If the xrd:Expires element is present, but its
        contents are not formatted according to the specification.
    Nz%Y-%m-%dT%H:%M:%SZr      )findexpires_tagr(   r   r   )xrd_elementdefaultexpires_elementexpires_stringexpires_timer   r   r   getXRDExpiration   s   

rO   c           	      C   s   | t}|  zt|d  td j}W n
 ty"   Y dS w | }|dd D ]"}|	ddd }t|
t}|| krMtd||f |}q-t| }t||sctd||f |S )a  Return the CanonicalID from this XRDS document.

    @param iname: the XRI being resolved.
    @type iname: unicode

    @param xrd_tree: The XRDS output from the resolver.
    @type xrd_tree: ElementTree

    @returns: The XRI CanonicalID or None.
    @returntype: unicode or None
    r   N   !z%r can not come from %sz%r can not come from root %r)rC   rD   reverser   XRIcanonicalID_tagr(   
IndexErrorlowerrsplitfindtextr   rootAuthorityproviderIsAuthoritative)	inamerA   xrd_listcanonicalIDchildIDrF   parent_soughtparentrB   r   r   r   r      s$   

r   c                   @   s    e Zd ZdZdd Zdd ZdS )_Maxz
    Value that compares greater than any other value.

    Should only be used as a singleton. Implemented for use as a
    priority value for when a priority is not specified.
    c                 C      t || jS Nr   	__class__selfotherr   r   r   __lt__   r0   z_Max.__lt__c                 C   rb   rc   rd   rf   r   r   r   __eq__   r0   z_Max.__eq__N)r   r   r   r   ri   rj   r   r   r   r   ra      s    ra   c                 C   s2   |  d}|durt|}|dkr|S tdtS )zGet the priority of this element.

    Raises ValueError if the value of the priority is invalid. If no
    priority is specified, it returns a value that compares greater
    than any other value.
    priorityNr   z-Priority values must be non-negative integers)getint
ValueErrorMax)r*   prio_strprio_valr   r   r   r      s   
r   c                 C   s"   zt | W S  ty   t Y S w )ztGet the priority of this element

    Returns Max if no priority is specified or the priority value is invalid.
    )r   rn   ro   )r*   r   r   r   r      s
   
r   c                 C   s   t |  t| td}|S )z5Sort a list of elements that have priority attributes)key)randomshufflesortedr   )elementssorted_elemsr   r   r   r	      s   
r	   c                 C   s   t | }t|tS )zUReturn an iterable over the Service elements in the Yadis XRD

    sorted by priority)r   r	   rC   service_tagrE   r   r   r   r
     s   r
   c                 C   s   dd t | tD S )z]Given a Service element, return a list of the contents of all
    URI tags in priority order.c                 S      g | ]}|j qS r   r(   ).0uri_elementr   r   r   
<listcomp>  s    zsortedURIs.<locals>.<listcomp>)r	   rC   uri_tagservice_elementr   r   r   
sortedURIs  s   r   c                 C   s   dd |  tD S )zKGiven a Service element, return a list of the contents of all
    Type tagsc                 S   ry   r   rz   )r{   type_elementr   r   r   r}     s    zgetTypeURIs.<locals>.<listcomp>)rC   type_tagr   r   r   r   getTypeURIs  s   r   c                 C   s<   t | }|s	dg}g }|D ]}t| }|||| f q|S )zeTake a service element and expand it into an iterator of:
    ([type_uri], uri, service_element)
    N)r   r   append)r   urisexpandeduri	type_urisr   r   r   r     s   r   c                 C   s    g }| D ]	}| t| q|S )a  Take a sorted iterator of service elements and expand it into a
    sorted iterator of:
    ([type_uri], uri, service_element)

    There may be more than one item in the resulting list for each
    service element if there is more than one URI or type for a
    service, but each triple will be unique.

    If there is no URI or Type for a Service element, it will not
    appear in the result.
    )extendr   )service_elementsr   r   r   r   r   r   .  s   r   rc   )/r   __all__sysrs   	functoolsr   timer   openid.oidutilr   r   r'   r    openid.yadisr   r&   r   r   r   r2   r5   r   r   r6   r@   rx   rD   r   r~   rI   rT   r   r   rO   r   total_orderingobjectra   ro   r   r   r	   r
   r   r   r   r   r   r   r   r   <module>   sR   	

%
	