o
    1ZhF2                  
   @   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   K/var/www/html/lang_env/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 )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   s    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   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 )
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_parser4   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 |rt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r4   r   r   r   	<genexpr>   s    
z/URIVariable._query_expansion.<locals>.<genexpr>r"   c                 3       | ]}t | V  qd S r   r9   r;   r   r   r   r>          r#   c                 3   .    | ]\}}t |  d t |  V  qdS r8   r9   r)   kr<   r@   r   r   r>          
c                 3   rB   )r"   Nr9   rC   r@   r   r   r>      rE   )is_list_of_tuplesr   	list_testtcastSequenceScalarVariableValuer6   join	dict_testMappingstrsorteditemsTextr:   r   r4   r7   r'   r(   tuplesrQ   r   r=   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   r9   r;   r@   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   r9   rC   
format_strr   r   r   r>      s    z4URIVariable._label_path_expansion.<locals>.<genexpr>)r6   r   
isinstancerO   r2   floatcomplexlenrF   rG   rH   rI   rJ   rK   rL   rM   rN   rP   rQ   rR   r:   )
r   r4   r7   r'   r(   r6   rT   rQ   	fragmentsexpandedr   rX   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|rt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#   r9   r;   r=   r   r   r>     s    "z3URIVariable._semi_path_expansion.<locals>.<genexpr>r"   c                 3   r?   r   r9   r;   r@   r   r   r>     rA   r#   c                 3   6    | ]\}}|d urt |  dt |  V  qd S ra   r9   rC   r@   r   r   r>         c                 3   rb   )Nr"   r9   rC   r@   r   r   r>     rc   )r6   r   r   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r:   )	r   r4   r7   r'   r(   r6   rT   rQ   r_   r   r=   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   r>   7  s    z0URIVariable._string_expansion.<locals>.<genexpr>rV   rW   c                 3   s0    | ]\}} t |jt |jf V  qd S r   re   rC   rY   r   r   r   r>   >  s
    
)rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r:   r   rS   r   rf   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   r`   rU   rd   rg   r3   r5   r6   rL   )r   rh   Zreturn_valuesr4   optsr7   r_   Z	expansionr   r   r   expandG  s2   





zURIVariable.expand)r   Nr   )__name__
__module____qualname____doc__r-   r.   rO   r   r   r   r   r   VariableValueboolrH   Optionalr2   rU   r`   rd   rg   VariableValueDictrN   rk   r   r   r   r   r      sv    

-
/
2
7
r   r7   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)rZ   r0   r]   )r)   rH   r   r   r   r>     s   $ z$is_list_of_tuples.<locals>.<genexpr>)FNT)rZ   listr0   allr7   r   r   r   rF     s   rF   c                 C   s   t | ttfS r   )rZ   rt   r0   rv   r   r   r   rG     s   rG   c                 C   s   t | ttjjfS r   )rZ   dictcollectionsabcMutableMappingrv   r   r   r   rM     s   rM   utf-8encodingc                 C   s   t | tr
| |S | S r   )rZ   rO   encode)r7   r|   r   r   r   _encode  s   

r~   r   c                 C   s(   t | ttfst| } tjt| |S r   )rZ   rO   bytesurllibr   r:   r~   )r7   r   r   r   r   r:     s   r:   )r{   )ro   collections.abcrx   typingrH   urllib.parser   Unionr2   r[   r\   rO   rK   rJ   rN   Tuplerp   Dictrs   r   Anyrq   rr   rF   rG   rM   AnyStrr   r~   r:   r   r   r   r   <module>   s2      h"
