U
    [e                      @   sz  d Z ddddddddd	d
ddg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 d6d'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*d0d1 Z+d2d3 Z,d4d Z-d5d Z.dS )7z+
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   T/var/www/html/services/stratfitenv/lib/python3.8/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k
rD    Y nR t	k
rv } zt
d}||_|W 5 d}~X Y n X 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   )text
bytestringelementwhyexctreer   r   r   r   1   s    
zxri://$xrd*($v*2.0)zxri://$xrdsc                 C   s   d| |f S )Nz{%s}%sr   )nstr   r   r   r   S   s    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    c                 C   s
   t t| S )zvbasestring -> basestring

    Create a tag name in the XRDS XML namespace suitable for using
    with ElementTree
    )r   XRDS_NSr1   r   r   r   	mkXRDSTag`   s    r3   ZXRDSZServiceZXRDTypeURIZExpiresZCanonicalIDc                 C   s   |   }|jtkS )z"Is this document an XRDS document?)Zgetroottagroot_tag)xrd_treerootr   r   r   r   u   s    c                 C   s(   d}|  tD ]}q|dk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    c                 C   s:   |  t}|dk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_stringexpires_timer   r   r   getXRDExpiration   s    

rC   c           	      C   s   | t}|  zt|d  td j}W n tk
rF   Y dS X | }|dd D ]D}|	ddd }t|
t}|| krtd||f |}q\t| }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rsplitZfindtextr   ZrootAuthorityZproviderIsAuthoritative)	Zinamer8   Zxrd_listZcanonicalIDZchildIDr=   Zparent_soughtparentr9   r   r   r   r      s"    

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k	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    
c                 C   s(   z
t | W S  tk
r"   t Y S X 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    
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    
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    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    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    )N)/r   __all__sysr[   	functoolsr   timer   Zopenid.oidutilr   r   r'   r    Zopenid.yadisr   r&   r   r   r   r0   r2   r   r   r3   r7   r_   r;   rg   rc   r@   rG   r   r   rC   r   total_orderingobjectrL   rY   r   r   r	   r
   rf   rh   r   r   r   r   r   r   <module>   sh   	

%
	