o
    ÒèFhF2  ã                
   @   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dejdejeejejejeef   f fdd„Zdejdefd	d
„Zdejdefdd„Zddejdedefdd„Zdejdedef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defdd„Zdefdd	„Zdefd
d„Z	d dd„Z
d dd„Zdedededeje deje f
dd„Zdedededeje deje f
dd„Zdedededeje deje f
dd„Zdedededeje deje f
dd„Z	d!deje dejeef f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   úM/var/www/html/loop/nvenv/lib/python3.10/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 r| j d | _| j dd… }| jtjdd… v r&tj| _| d¡}|D ]I}d}|}d|v rAt| dd¡ƒ\}}d}| d¡rPd	}|dd
… }d}d|v rdt| dd¡ƒ\}}t	|ƒ}|rk|| j
|< | j |||dœf¡ q-dd„ | jD ƒ| _dS )z©Parse 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   s    z%URIVariable.parse.<locals>.<listcomp>)r   r   Ú	operatorsr   Úreservedr   ÚsplitÚtupleÚendswithÚintr   r   Úappendr   )	r   Úvar_list_strÚvar_listr   Údefault_valÚnamer'   r(   Ú
prefix_strr   r   r   r   O   s4   



ÿzURIVariable.parsec                 C   st   d| _ | j | _| _| jdkrd| _| jdv rd| _| jdkr"d| _| jdkr-d| _d| _| jdv r8tj| _ d	S d	S )
zÍSet ``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_parser7   Úvaluer'   r(   c                    sJ  |du rdS t |ƒ\}}| j‰t|ƒrE|sE|sdS t tjt |¡}|r2| j ‡ ‡fdd„|D ƒ¡S d ‡fdd„|D ƒ¡}ˆ › d|› �S t	|ƒsK|r„|sOdS t tj
ttf |¡}|pat| ¡ ƒ}|rq| j ‡fdd„|D ƒ¡S d ‡fdd„|D ƒ¡}ˆ › d|› �S |r¡t tj|¡}|r•|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©r7   r   r   r   Ú	<genexpr>¤   s   € 
ÿz/URIVariable._query_expansion.<locals>.<genexpr>r"   c                 3   ó   � | ]}t |ˆ ƒV  qd S r   r=   r?   ©r   r   r   rB   ¨   ó   € r#   c                 3   ó.   � | ]\}}t |ˆ ƒ› d t |ˆ ƒ› �V  qdS r<   r=   ©r)   Úkr@   rD   r   r   rB   ±   ó   € 
ÿc                 3   rF   )r"   Nr=   rG   rD   r   r   rB   µ   rI   )Úis_list_of_tuplesr   Ú	list_testÚtÚcastÚSequenceÚScalarVariableValuer:   ÚjoinÚ	dict_testÚMappingÚstrÚsortedÚitemsÚTextr>   ©r   r7   r;   r'   r(   ÚtuplesrU   r   rA   r   Ú_query_expansion‘   s@   ÿÿÿzURIVariable._query_expansionc           
         s   | j }| j‰|du st|ttttfƒst|ƒdkrdS t|ƒ\}}t	|ƒrF|sF|s+d}t
 t
jt |¡}‡fdd„|D ƒ}|rD| |¡S dS t|ƒsL|rzt
 t
jttf |¡}|p^t| ¡ ƒ}d‰ |sgd‰ d}| ‡ ‡fdd	„|D ƒ¡}	|	rx|	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?   rD   r   r   r,   Ü   s    z5URIVariable._label_path_expansion.<locals>.<listcomp>ú%s=%sú%s,%sc                 3   s4   � | ]\}}|d urˆ t |ˆƒt |ˆƒf V  qd S r   r=   rG   ©Ú
format_strr   r   r   rB   ç   s   € þýz4URIVariable._label_path_expansion.<locals>.<genexpr>)r:   r   Ú
isinstancerS   r2   ÚfloatÚcomplexÚlenrJ   rK   rL   rM   rN   rO   rP   rQ   rR   rT   rU   rV   r>   )
r   r7   r;   r'   r(   r:   rX   rU   Ú	fragmentsÚexpandedr   r\   r   Ú_label_path_expansionÀ   s8   ÿþ
z!URIVariable._label_path_expansionc           	         sR  | j }| j‰|du rdS | jdkrd}t|ƒ\}}t|ƒrP|sPt tjt |¡}|r=| 	‡ ‡fdd„|D ƒ¡}|r;|S dS d 	‡fdd„|D ƒ¡}ˆ › d|› �S t
|ƒsV|rŠt tjttf |¡}|pht| ¡ ƒ}|rw| 	‡fd	d„|D ƒ¡S d 	‡fd
d„|D ƒ¡}ˆ › d|› �S t tj|¡}|r™|d|… n|}|r§ˆ › 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?   rA   r   r   rB     s   € "ÿz3URIVariable._semi_path_expansion.<locals>.<genexpr>r"   c                 3   rC   r   r=   r?   rD   r   r   rB     rE   r#   c                 3   ó6   � | ]\}}|d urt |ˆ ƒ› dt |ˆ ƒ› �V  qd S re   r=   rG   rD   r   r   rB     ó   € þýc                 3   rf   )Nr"   r=   rG   rD   r   r   rB     rg   )r:   r   r   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r>   )	r   r7   r;   r'   r(   r:   rX   rU   rc   r   rA   r   Ú_semi_path_expansionò   s@   
ÿþþz URIVariable._semi_path_expansionc                    sÔ   |d u rd S t |ƒ\}}t|ƒr'|s't tjt |¡}d ‡fdd„|D ƒ¡S t|ƒs-|rSt tjt	tf |¡}|p?t
| ¡ ƒ}|rDdnd‰ d ‡ ‡fdd„|D ƒ¡S t tj|¡}|rb|d |… n|}t|ˆjƒS )Nr"   c                 3   s   � | ]	}t |ˆ jƒV  qd S r   ©r>   r   r?   r   r   r   rB   7  s   € z0URIVariable._string_expansion.<locals>.<genexpr>rZ   r[   c                 3   s0   � | ]\}}ˆ t |ˆjƒt |ˆjƒf V  qd S r   ri   rG   ©r]   r   r   r   rB   >  s
   € ÿ
ÿ)rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   r>   r   rW   r   rj   r   Ú_string_expansion)  s    þzURIVariable._string_expansionÚvar_dictc                 C   sê   g }|du r| j | j iS | jD ]S\}}| |d¡}|s)|dkr)|| jv r)| j| }|du r.qd}| jdv r9| j}n| jdv rB| j}n| jdkrK| j}n| j}||||d |d ƒ}|durb| 	|¡ qd}|rp| 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   rd   rY   rh   rk   r3   r9   r:   rP   )r   rl   Úreturn_valuesr7   Úoptsr;   rc   Ú	expansionr   r   r   ÚexpandG  s2   




€
zURIVariable.expand)r   Nr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r.   rS   r   r   r   r   r   ÚVariableValueÚboolrL   ÚOptionalr2   rY   rd   rh   rk   ÚVariableValueDictrR   rq   r   r   r   r   r      sv    

-þýüû
ú/þýüû
ú2þýüû
ú7þýüû
úÿÿþr   r;   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)r^   r0   ra   )r)   rL   r   r   r   rB   ‹  s   €$ z$is_list_of_tuples.<locals>.<genexpr>)FNT)r^   Úlistr0   Úall©r;   r   r   r   rJ   …  s   ÿþýrJ   c                 C   s   t | ttfƒS r   )r^   rz   r0   r|   r   r   r   rK   ’  s   rK   c                 C   s   t | ttjjfƒS r   )r^   ÚdictÚcollectionsÚabcÚMutableMappingr|   r   r   r   rQ   –  s   rQ   úutf-8Úencodingc                 C   s   t | tƒr
|  |¡S | S r   )r^   rS   Úencode)r;   r‚   r   r   r   Ú_encodeš  s   

r„   r   c                 C   s(   t | ttfƒst| ƒ} tj t| ƒ|¡S r   )r^   rS   ÚbytesÚurllibr   r>   r„   )r;   r   r   r   r   r>      s   r>   )r�   )ru   Úcollections.abcr~   ÚtypingrL   Úurllib.parser†   ÚUnionr2   r_   r`   rS   rO   rN   rR   ÚTuplerv   ÚDictry   r   ÚAnyrw   rx   rJ   rK   rQ   ÚAnyStrr…   r„   r>   r   r   r   r   Ú<module>   s2    ýÿ  hÿ"
þ