o
    {j6hF2                  
   @   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   c/var/www/html/chefvision.cloud.itp360.com/venv/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   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 )
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_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 |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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|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#   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"
