o
    -                     @   s   d Z ddlZzddlmZ W n ey   ddlZY nw ejjdkr'ddl	Z	nejjdkr3ddl
mZ	 G dd deZdd Zd	d
 Zdd ZzeZW n eyV   eZY nw eefZdddZ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?
>

    N      c                   @   sj   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Z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:/?#[]@!$&'()*+,;=c                 C   s8   || _ d| _d| _g | _g | _i | _|   |   d S )N )originaloperatorsafe	variablesvariable_namesdefaultsparse
post_parse)selfvar r   =/tmp/google-cloud-sdk/lib/third_party/uritemplate/variable.py__init__9   s   zURIVariable.__init__c                 C   s   d|  S )NzURIVariable(%s)r   r   r   r   r   __repr__J   s   zURIVariable.__repr__c                 C   s   | j S N)r   r   r   r   r   __str__M   s   zURIVariable.__str__c                 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      Nr   ,=F*T:)explodeprefixc                 S   s   g | ]\}}|qS r   r   ).0varname_r   r   r   
<listcomp>{   s    z%URIVariable.parse.<locals>.<listcomp>)r   r   	operatorsr   reservedr   splittupleendswithintr   r   appendr   )r   var_listr   default_valnamer'   r(   r   r   r   r   P   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_parsec                    s  |du rdS t |\}}| jt|r;|s;|sdS |r)| j fdd|D S dfdd|D }d |S t|sA|rn|sEdS |pLt| }|r\| jfdd|D S dfdd|D }d |S |r|rx|d| n|}d t	|S  d	 S )
z/Expansion method for the '?' and '&' operators.Nc                 3   s"    | ]}d   t|V  qdS {}={}Nformatquoter)   vr6   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   rA          r:   c                 3   ,    | ]\}}d  t| t| V  qdS r9   r;   r)   kr?   rD   r   r   rA          
c                 3   rF   ){},{}Nr;   rG   rD   r   r   rA      rI   r#   )
is_list_of_tuplesr   	list_testr8   joinr<   	dict_testsorteditemsr=   r   r6   valuer'   r(   tuplesrP   r   r@   r   _query_expansion   s:   zURIVariable._query_expansionc           
         s   | j }| j|du st|dkr|dkrdS t|\}}t|r8|s8|s&d}fdd|D }|r6||S dS t|s>|ra|pEt| }d |sNd d}| fd	d
|D }	|	r_|	S dS |ri|d| n|}t	|S )zSLabel and path expansion method.

        Expands for operators: '/', '.'

        Nr   r   r"   c                    s   g | ]}|d urt | qS r   rC   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   rC   rG   
format_strr   r   r   rA      s    z4URIVariable._label_path_expansion.<locals>.<genexpr>)
r8   r   lenrK   rL   rM   rN   rO   rP   r=   )
r   r6   rR   r'   r(   r8   rS   rP   	fragmentsexpandedr   rW   r   _label_path_expansion   s,   
z!URIVariable._label_path_expansionc           	         s  | j }| j|du rdS | jdkrd}t|\}}t|rF|sF|r4| fdd|D }|r2|S dS dfdd|D }d |S t|sL|rt|pSt|	 }|rb|fd	d|D S dfd
d|D }d |S |r||d| n|}|rd 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   rA      s    

z3URIVariable._semi_path_expansion.<locals>.<genexpr>r"   c                 3   rB   r   rC   r>   rD   r   r   rA      rE   r:   c                 3   4    | ]\}}|d urd t| t| V  qd S r]   r;   rG   rD   r   r   rA          c                 3   r^   )NrJ   r;   rG   rD   r   r   rA     r_   )r8   r   r   rK   rL   rM   r<   rN   rO   rP   r=   )	r   r6   rR   r'   r(   r8   rS   rP   r[   r   r@   r   _semi_path_expansion   s:   
z URIVariable._semi_path_expansionc                    s   |d u rd S t |\}}t|r|sdfdd|D S t|s$|r?|p+t| }|r0dnd d fdd|D S |rG|d | n|}t|jS )Nr"   c                 3   s    | ]	}t | jV  qd S r   r=   r   r>   r   r   r   rA     s    z0URIVariable._string_expansion.<locals>.<genexpr>rU   rV   c                 3   s0    | ]\}} t |jt |jf V  qd S r   ra   rG   rX   r   r   r   rA     s    
)rK   rL   rM   rN   rO   rP   r=   r   rQ   r   rb   r   _string_expansion  s   zURIVariable._string_expansionNc                 C   s   g }| j D ]S\}}||d}|s|dkr|| jv r| j| }|du r$qd}| jdv r/| j}n| jdv r8| j}n| jdkrA| j}n| j}||||d |d }|durX|| qd}|rf| 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   getr   r   r\   rT   r`   rc   r3   r7   r8   rM   r   )r   var_dictreturn_valuesr6   optsrR   r[   	expansionr   r   r   expand(  s.   





zURIVariable.expandr   )__name__
__module____qualname____doc__r-   r.   r   r   r    r   r   rT   r\   r`   rc   ri   r   r   r   r   r   "   s    -*&0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)
isinstancer0   rY   )r)   tr   r   r   rA   e  s   $ z$is_list_of_tuples.<locals>.<genexpr>)FNT)rn   listr0   allrR   r   r   r   rK   b  s   rK   c                 C   s   t | ttfS r   )rn   rp   r0   rr   r   r   r   rL   k  s   rL   c                 C   s   t | ttjfS r   )rn   dictcollections_abcMutableMappingrr   r   r   r   rN   o  s   rN   utf-8c                 C   s(   t | trt| dd d ur| |S | S )Nencode)rn   texttypegetattrrw   )rR   encodingr   r   r   _encode{  s   

r{   c                 C   s"   t | ts	t| } tt| |S r   )rn   stringlikesstrurllibr=   r{   )rR   r   r   r   r   r=     s   
r=   )rv   )rm   syscollections.abcabcrt   ImportErrorcollectionsversion_infomajorr~   urllib.parser   objectr   rK   rL   rN   unicoderx   	NameErrorr}   bytesr|   r{   r=   r   r   r   r   <module>   s2    
  B	
