a
    ͝GdF2                     @   s   d Z ddlZddlZddlZejee	e
ef Zejeje ejeef ejeef ef Zejeef ZG dd dZejejeejejejeef   f dddZejeddd	Zejedd
dZdejeedddZejeedddZdS )z

uritemplate.variable
====================

This module contains the URIVariable class which powers the URITemplate class.

What treasures await you:

- URIVariable class

You see a hammer in front of you.
What do you do?
>

    Nc                   @   s  e Zd ZdZdZdZedddZeddd	Zedd
dZ	ddddZ
ddddZeeeeje eje dddZeeeeje eje dddZeeeeje eje dddZeeeeje eje dddZdeje ejeef dddZdS )URIVariablea  This object validates everything inside the URITemplate object.

    It validates template expansions and will truncate length as decided by
    the template.

    Please note that just like the :class:`URITemplate <URITemplate>`, this
    object's ``__str__`` and ``__repr__`` methods do not return the same
    information. Calling ``str(var)`` will return the original variable.

    This object does the majority of the heavy lifting. The ``URITemplate``
    object finds the variables in the URI and then creates ``URIVariable``
    objects.  Expansions of the URI are handled by each ``URIVariable``
    object. ``URIVariable.expand()`` returns a dictionary of the original
    variable and the expanded value. Check that method's documentation for
    more information.

    )
+#./;?&|!@z:/?#[]@!$&'()*+,;=)varc                 C   s8   || _ d| _d| _g | _g | _i | _|   |   d S )N )originaloperatorsafe	variablesvariable_namesdefaultsparse
post_parse)selfr    r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/uritemplate/variable.py__init__6   s    zURIVariable.__init__)returnc                 C   s   d|  S )NzURIVariable(%s)r   r   r   r   r   __repr__I   s    zURIVariable.__repr__c                 C   s   | j S N)r   r   r   r   r   __str__L   s    zURIVariable.__str__Nc           	      C   s  | j }| j d tjv r0| j d | _| j dd }| jtjdd v rLtj| _|d}|D ]}d}|}d|v rt|dd\}}d}|drd	}|dd
 }d}d|v rt|dd\}}t	|}|r|| j
|< | j|||df qZdd | jD | _dS )zParse the variable.

        This finds the:
            - operator,
            - set of safe characters,
            - variables, and
            - defaults.

        r      N   ,=F*T:)explodeprefixc                 S   s   g | ]\}}|qS r   r   ).0varname_r   r   r   
<listcomp>z       z%URIVariable.parse.<locals>.<listcomp>)r   r   	operatorsr   reservedr   splittupleendswithintr   r   appendr   )	r   Zvar_list_strZvar_listr   Zdefault_valnamer'   r(   Z
prefix_strr   r   r   r   O   s4    



zURIVariable.parsec                 C   sp   d| _ | j | _| _| jdkr$d| _| jdv r4d| _| jdkrDd| _| jdkrZd| _d| _| jdv rltj| _ d	S )
zSet ``start``, ``join_str`` and ``safe`` attributes.

        After parsing the variable, we need to set up these attributes and it
        only makes sense to do it in a more easily testable way.
        r   r   )r   r   r   r"   r   r   r	   )r   r   N)r   r   startjoin_strr   r/   r   r   r   r   r   |   s    




zURIVariable.post_parse)r5   valuer'   r(   r   c                    sP  |du rdS t |\}}| jt|r|s|s2dS ttjt |}|rd| j fdd|D S dfdd|D }  d| S t	|s|r
|sdS ttj
ttf |}|pt| }|r| jfdd|D S dfdd|D }  d| S |rHttj|}|r0|d| n|}  dt| S  d S )	z/Expansion method for the '?' and '&' operators.Nc                 3   s"   | ]}  d t | V  qdS r#   Nquoter)   vr5   r   r   r   	<genexpr>   s   z/URIVariable._query_expansion.<locals>.<genexpr>r"   c                 3   s   | ]}t | V  qd S r   r:   r<   r   r   r   r?      r-   r#   c                 3   s,   | ]$\}}t |  d t |  V  qdS r9   r:   r)   kr=   r@   r   r   r?      s   c                 3   s,   | ]$\}}t |  d t |  V  qdS )r"   Nr:   rA   r@   r   r   r?      s   )is_list_of_tuplesr   	list_testtcastSequenceScalarVariableValuer7   join	dict_testMappingstrsorteditemsTextr;   r   r5   r8   r'   r(   tuplesrN   r   r>   r   _query_expansion   s@    zURIVariable._query_expansionc           
         s"  | j }| j|du s2t|ttttfs6t|dkr6dS t|\}}t	|r|s|sVd}t
t
jt |}fdd|D }|r||S dS t|s|rt
t
jttf |}|pt| }d |sd d}| fdd	|D }	|	r|	S dS t
t
j|}|r|d| n|}t|S )
zSLabel and path expansion method.

        Expands for operators: '/', '.'

        Nr   r"   c                    s   g | ]}|d urt | qS r   r:   r<   r@   r   r   r,      r-   z5URIVariable._label_path_expansion.<locals>.<listcomp>%s=%s%s,%sc                 3   s2   | ]*\}}|d ur t |t |f V  qd S r   r:   rA   
format_strr   r   r   r?      s   z4URIVariable._label_path_expansion.<locals>.<genexpr>)r7   r   
isinstancerL   r3   floatcomplexlenrC   rD   rE   rF   rG   rH   rI   rJ   rK   rM   rN   rO   r;   )
r   r5   r8   r'   r(   r7   rQ   rN   	fragmentsexpandedr   rU   r   _label_path_expansion   s:    
z!URIVariable._label_path_expansionc           	         sX  | j }| j|du rdS | jdkr&d}t|\}}t|r|sttjt |}|rz|	 fdd|D }|rv|S dS d	fdd|D }  d| S t
|s|rttjttf |}|pt| }|r|	fd	d|D S d	fd
d|D }  d| S ttj|}|r6|d| n|}|rT  dt| S  S )z"Expansion method for ';' operator.Nr   r	   c                 3   s*   | ]"}|d ur  dt | V  qd S Nr#   r:   r<   r>   r   r   r?     s   z3URIVariable._semi_path_expansion.<locals>.<genexpr>r"   c                 3   s   | ]}t | V  qd S r   r:   r<   r@   r   r   r?     r-   r#   c                 3   s4   | ],\}}|d urt |  dt |  V  qd S r^   r:   rA   r@   r   r   r?     s   c                 3   s4   | ],\}}|d urt |  dt |  V  qd S )Nr"   r:   rA   r@   r   r   r?     s   )r7   r   r   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   r;   )	r   r5   r8   r'   r(   r7   rQ   rN   r\   r   r>   r   _semi_path_expansion   s@    
z URIVariable._semi_path_expansionc                    s   |d u rd S t |\}}t|rN|sNttjt |}dfdd|D S t|sZ|rttjt	tf |}|p~t
| }|rdnd d fdd|D S ttj|}|r|d | n|}t|jS )Nr"   c                 3   s   | ]}t | jV  qd S r   r;   r   r<   r   r   r   r?   7  r-   z0URIVariable._string_expansion.<locals>.<genexpr>rS   rT   c                 3   s.   | ]&\}} t |jt |jf V  qd S r   r`   rA   rV   r   r   r   r?   >  s   )rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   r;   r   rP   r   ra   r   _string_expansion)  s     zURIVariable._string_expansion)var_dictr   c                 C   s   g }|du r| j | j iS | jD ]\}}||d}|sR|dkrR|| jv rR| j| }|du r\qd}| jdv rr| j}n*| jdv r| j}n| jdkr| j}n| j}||||d |d }|dur|	| qd}|r| j
| j| }| j |iS )a1  Expand the variable in question.

        Using ``var_dict`` and the previously parsed defaults, expand this
        variable and subvariables.

        :param dict var_dict: dictionary of key-value pairs to be used during
            expansion
        :returns: dict(variable=value)

        Examples::

            # (1)
            v = URIVariable('/var')
            expansion = v.expand({'var': 'value'})
            print(expansion)
            # => {'/var': '/value'}

            # (2)
            v = URIVariable('?var,hello,x,y')
            expansion = v.expand({'var': 'value', 'hello': 'Hello World!',
                                  'x': '1024', 'y': '768'})
            print(expansion)
            # => {'?var,hello,x,y':
            #     '?var=value&hello=Hello%20World%21&x=1024&y=768'}

        Nr   )r   r   )r   r	   r   r'   r(   )r   r   getr   r   r]   rR   r_   rb   r4   r6   r7   rI   )r   rc   Zreturn_valuesr5   optsr8   r\   Z	expansionr   r   r   expandG  s0    



zURIVariable.expand)N)__name__
__module____qualname____doc__r.   r/   rL   r   r   r   r   r   VariableValueboolrE   Optionalr3   rR   r]   r_   rb   VariableValueDictrK   rf   r   r   r   r   r      sJ   -149 r   )r8   r   c                 C   s0   | r$t | ttfr$tdd | D s(dS d| fS )Nc                 s   s$   | ]}t |tot|d kV  qdS )r!   N)rW   r1   rZ   )r)   rE   r   r   r   r?     r-   z$is_list_of_tuples.<locals>.<genexpr>)FNT)rW   listr1   allr8   r   r   r   rC     s    rC   c                 C   s   t | ttfS r   )rW   ro   r1   rq   r   r   r   rD     s    rD   c                 C   s   t | ttjjfS r   )rW   dictcollectionsabcMutableMappingrq   r   r   r   rJ     s    rJ   utf-8)r8   encodingr   c                 C   s   t | tr| |S | S r   )rW   rL   encode)r8   rw   r   r   r   _encode  s    

ry   )r8   r   r   c                 C   s(   t | ttfst| } tjt| |S r   )rW   rL   bytesurllibr   r;   ry   )r8   r   r   r   r   r;     s    r;   )rv   )rj   collections.abcrs   typingrE   urllib.parser{   Unionr3   rX   rY   rL   rH   rG   rK   Tuplerk   Dictrn   r   Anyrl   rm   rC   rD   rJ   AnyStrrz   ry   r;   r   r   r   r   <module>   s,     i"