U
    Â½„bF2  ã                   @   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   ú8/tmp/pip-unpacked-wheel-enezgtog/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kr0| j d | _| j dd… }| jtjdd… krLtj| _| d¡}|D ]’}d}|}d|kr‚t| dd¡ƒ\}}d}| d¡r d	}|dd
… }d}d|krÈt| dd¡ƒ\}}t	|ƒ}|rÖ|| j
|< | j |||dœf¡ qZd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   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kr4d| _| jdkrDd| _| jdkrZd| _d| _| jdkrltj| _ 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_parse)r4   Úvaluer'   r(   r   c                    sP  |dk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
|s dS t tj
ttf |¡}|pÄt| ¡ ƒ}|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©r4   r   r   r   Ú	<genexpr>¤   s    z/URIVariable._query_expansion.<locals>.<genexpr>r"   c                 3   s   | ]}t |ˆ ƒV  qd S r   r9   r;   ©r   r   r   r>   ¨   s     r#   c                 3   s,   | ]$\}}t |ˆ ƒ› d t |ˆ ƒ› �V  qdS r8   r9   ©r)   Úkr<   r?   r   r   r>   ±   s    c                 3   s,   | ]$\}}t |ˆ ƒ› d t |ˆ ƒ› �V  qdS )r"   Nr9   r@   r?   r   r   r>   µ   s    )Úis_list_of_tuplesr   Ú	list_testÚtÚcastÚSequenceÚScalarVariableValuer6   ÚjoinÚ	dict_testÚMappingÚstrÚsortedÚitemsÚTextr:   ©r   r4   r7   r'   r(   ÚtuplesrM   r   r=   r   Ú_query_expansion‘   s@    ÿÿÿzURIVariable._query_expansionc           
         s"  | j }| j‰|dk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˜|rôt
 t
jttf |¡}|p¼t| ¡ ƒ}d‰ |sÎd‰ 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 k	rt |ˆ ƒ‘qS r   r9   r;   r?   r   r   r,   Ü   s      z5URIVariable._label_path_expansion.<locals>.<listcomp>ú%s=%sú%s,%sc                 3   s2   | ]*\}}|d k	rˆ t |ˆƒt |ˆƒf V  qd S r   r9   r@   ©Ú
format_strr   r   r   r>   ç   s   þz4URIVariable._label_path_expansion.<locals>.<genexpr>)r6   r   Ú
isinstancerK   r2   ÚfloatÚcomplexÚlenrB   rC   rD   rE   rF   rG   rH   rI   rJ   rL   rM   rN   r:   )
r   r4   r7   r'   r(   r6   rP   rM   Z	fragmentsÚexpandedr   rT   r   Ú_label_path_expansionÀ   s:    ÿ
þþz!URIVariable._label_path_expansionc           	         sX  | j }| j‰|dkrdS | jdkr&d}t|ƒ\}}t|ƒr |s t 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 |¡}|pÒt| ¡ ƒ}|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 k	rˆ › dt |ˆƒ› �V  qd S ©Nr#   r9   r;   r=   r   r   r>     s     z3URIVariable._semi_path_expansion.<locals>.<genexpr>r"   c                 3   s   | ]}t |ˆ ƒV  qd S r   r9   r;   r?   r   r   r>     s     r#   c                 3   s4   | ],\}}|d k	rt |ˆ ƒ› dt |ˆ ƒ› �V  qd S r\   r9   r@   r?   r   r   r>     s   þc                 3   s4   | ],\}}|d k	rt |ˆ ƒ› dt |ˆ ƒ› �V  qd S )Nr"   r9   r@   r?   r   r   r>     s   þ)r6   r   r   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r:   )	r   r4   r7   r'   r(   r6   rP   rM   rZ   r   r=   r   Ú_semi_path_expansionò   s@    
ÿþþz URIVariable._semi_path_expansionc                    sÔ   |d krd S t |ƒ\}}t|ƒrN|sNt tjt |¡}d ‡fdd„|D ƒ¡S t|ƒsZ|r¦t tjt	tf |¡}|p~t
| ¡ ƒ}|rˆd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  s     z0URIVariable._string_expansion.<locals>.<genexpr>rR   rS   c                 3   s.   | ]&\}}ˆ t |ˆjƒt |ˆjƒf V  qd S r   r^   r@   ©rU   r   r   r   r>   >  s   ÿ)rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r:   r   rO   r   r_   r   Ú_string_expansion)  s     þzURIVariable._string_expansion)Úvar_dictr   c                 C   sê   g }|dkr| j | j iS | jD ]¦\}}| |d¡}|sR|dkrR|| jkrR| j| }|dkr\qd}| jdkrr| j}n*| jdkr„| j}n| jdkr–| j}n| j}||||d |d ƒ}|dk	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[   rQ   r]   r`   r3   r5   r6   rH   )r   ra   Zreturn_valuesr4   Úoptsr7   rZ   Z	expansionr   r   r   ÚexpandG  s0    



zURIVariable.expand)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r.   rK   r   r   r   r   r   ÚVariableValueÚboolrD   ÚOptionalr2   rQ   r[   r]   r`   ÚVariableValueDictrJ   rd   r   r   r   r   r      sJ   -ú1ú4ú9ú ÿþ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)rV   r0   rY   )r)   rD   r   r   r   r>   ‹  s     z$is_list_of_tuples.<locals>.<genexpr>)FNT)rV   Úlistr0   Úall©r7   r   r   r   rB   …  s    ÿþýrB   c                 C   s   t | ttfƒS r   )rV   rm   r0   ro   r   r   r   rC   ’  s    rC   c                 C   s   t | ttjjfƒS r   )rV   ÚdictÚcollectionsÚabcÚMutableMappingro   r   r   r   rI   –  s    rI   úutf-8)r7   Úencodingr   c                 C   s   t | tƒr|  |¡S | S r   )rV   rK   Úencode)r7   ru   r   r   r   Ú_encodeš  s    

rw   )r7   r   r   c                 C   s(   t | ttfƒst| ƒ} tj t| ƒ|¡S r   )rV   rK   ÚbytesÚurllibr   r:   rw   )r7   r   r   r   r   r:      s    r:   )rt   )rh   Úcollections.abcrq   ÚtypingrD   Úurllib.parsery   ÚUnionr2   rW   rX   rK   rG   rF   rJ   ÚTupleri   ÚDictrl   r   ÚAnyrj   rk   rB   rC   rI   ÚAnyStrrx   rw   r:   r   r   r   r   Ú<module>   s,   ýÿ  i"þ