a
    xd                      @   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   J/var/www/html/Ranjet/env/lib/python3.9/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 nP ttttfyB    Y nT t	yv } zt
d}||_|W Y d}~n(d}~0 0 t|}t|st
d|S 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   )textZ
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   s    r   c                 C   s
   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   s    r   c                 C   s
   t t| S )zvbasestring -> basestring

    Create a tag name in the XRDS XML namespace suitable for using
    with ElementTree
    )r   XRDS_NSr0   r   r   r   	mkXRDSTag`   s    r2   ZXRDSZServiceZXRDTypeURIZExpiresZ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   r8   xrdr   r   r   r   {   s    r   c                 C   s:   |  t}|du r|S |j}t|d}t|dd  S 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   )Zxrd_elementdefaultZexpires_elementZexpires_stringZexpires_timer   r   r   getXRDExpiration   s    

rB   c           	      C   s   | t}|  zt|d  td j}W n tyD   Y dS 0 | }|dd D ]D}|	ddd }t|
t}|| krtd||f |}qZt| }t||st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)r:   r;   reverser   ZXRIcanonicalID_tagr(   
IndexErrorlowerrsplitfindtextr   ZrootAuthorityZproviderIsAuthoritative)	Zinamer8   Zxrd_listZcanonicalIDZchildIDr=   Zparent_soughtparentr9   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   s   t || jS Nr   	__class__selfotherr   r   r   __lt__   s    z_Max.__lt__c                 C   s   t || jS rM   rN   rP   r   r   r   __eq__   s    z_Max.__eq__N)r   r   r   r   rS   rT   r   r   r   r   rL      s   rL   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)   Zprio_strZprio_valr   r   r   r      s    
r   c                 C   s&   z
t | W S  ty    t Y S 0 dS )ztGet the priority of this element

    Returns Max if no priority is specified or the priority value is invalid.
    N)r   rX   rY   )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   )elementsZ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	   r:   service_tagr<   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   s   g | ]
}|j qS r   r(   ).0Zuri_elementr   r   r   
<listcomp>  s   zsortedURIs.<locals>.<listcomp>)r	   r:   uri_tagservice_elementr   r   r   
sortedURIs  s    rf   c                 C   s   dd |  tD S )zKGiven a Service element, return a list of the contents of all
    Type tagsc                 S   s   g | ]
}|j qS r   r`   )ra   Ztype_elementr   r   r   rb     s   zgetTypeURIs.<locals>.<listcomp>)r:   type_tagrd   r   r   r   getTypeURIs  s    rh   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)rf   rh   append)re   urisexpandeduriZ	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   )Zservice_elementsrk   re   r   r   r   r   .  s    r   )N)/r   __all__sysr[   	functoolsr   timer   Zopenid.oidutilr   r   r'   r    Zopenid.yadisr   r&   r   r   r   r/   r1   r   r   r2   r7   r_   r;   rg   rc   r@   rF   r   r   rB   r   total_orderingobjectrL   rY   r   r   r	   r
   rf   rh   r   r   r   r   r   r   <module>   sP   	

%
	