
    -                        d Z ddlZ	 ddlmZ ej                  j                  dk(  rddl	Z	nej                  j                  dk(  rddl
mZ	  G d de      Zd Zd Zd	 Z	 eZeefZdd
Zd Zy# e$ r ddlZY kw xY w# e$ r eZY $w xY w)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                   V    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZddZy)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                     || _         d| _        d| _        g | _        g | _        i | _        | j                          | j                          y )N )originaloperatorsafe	variablesvariable_namesdefaultsparse
post_parse)selfvars     'lib/third_party/uritemplate/variable.py__init__zURIVariable.__init__9   s@    	 

    c                     d| z  S )NzURIVariable(%s) r   s    r   __repr__zURIVariable.__repr__J   s     4''r   c                     | j                   S N)r   r"   s    r   __str__zURIVariable.__str__M   s    }}r   c                    | j                   }| j                   d   t        j                  v r#| j                   d   | _        | j                   dd }| j                  t        j                  dd v rt        j                  | _        |j                  d      }|D ]  }d}|}d|v rt        |j                  dd            \  }}d}|j                  d      rd	}|dd
 }d}d|v r)t        |j                  dd            \  }}t        |      }|r|| j                  |<   | j                  j                  |||df        | j                  D cg c]  \  }}|	 c}}| _        yc c}}w )zParse the variable.

        This finds the:
            - operator,
            - set of safe characters,
            - variables, and
            - defaults.

        r      Nr   ,=F*T:)explodeprefix)r   r   	operatorsr   reservedr   splittupleendswithintr   r   appendr   )	r   var_listr   default_valnamer.   r/   varname_s	            r   r   zURIVariable.parseP   sQ    ===={444 MM!,DM}}QR(H==K11"155#,,DI>>#&CKDcz$)#))C*;$<!kG}}S!CRyFd{$TZZQ%78fV&1d#NN!!7f=>' . <@>>J><GQw>JJs   
E c                 F   d| _         | j                  x| _        | _        | j                  dk(  rd| _        | j                  dv rd| _        | j                  dk(  rd| _        | j                  dk(  rd| _        d| _        | j                  dv rt        j
                  | _         y	y	)
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   r1   r"   s    r   r   zURIVariable.post_parse}   s     	%)]]2
T]==CDJ==N*DM==CDJ==CDJDM==J&#,,DI 'r   c                 \   |yt        |      \  }}| j                  t        |      rX|sV|sy|r%| j                  j	                  fd|D              S dj	                  fd|D              }dj                  |      S t        |      s|rr|sy|xs t        |j                               }|r$| j                  j	                  fd|D              S dj	                  fd|D              }dj                  |      S |r%|r|d| n|}dj                  t        |            S dz   S )	z/Expansion method for the '?' and '&' operators.Nc              3   V   K   | ]   }d j                  t        |             " yw{}={}Nformatquote.0vr9   r   s     r   	<genexpr>z/URIVariable._query_expansion.<locals>.<genexpr>   s&      *BGQGNN4q$8%s   &)r)   c              3   6   K   | ]  }t        |        y wr%   rE   rG   rH   r   s     r   rI   z/URIVariable._query_expansion.<locals>.<genexpr>         ?Aq$   rB   c              3   p   K   | ]-  \  }}d j                  t        |      t        |             / ywrA   rC   rG   krH   r   s      r   rI   z/URIVariable._query_expansion.<locals>.<genexpr>   s9      * #($!Q NNaa"'   36c              3   p   K   | ]-  \  }}d j                  t        |      t        |             / yw){},{}NrC   rP   s      r   rI   z/URIVariable._query_expansion.<locals>.<genexpr>   s9      ! #($!Q NNaa"'rR   r*   )
is_list_of_tuplesr   	list_testr>   joinrD   	dict_testsorteditemsrE   )r   r9   valuer.   r/   tuplesrZ   r   s    `     @r   _query_expansionzURIVariable._query_expansion   s4   =)%0yyUF}})) *BG*    ? ??~~dE22Uv2VEKKM2E}})) * #(*    ! #(! 
 ~~dE22&,E'6N%E>>$eT(:;;czr   c                    | j                   }| j                  |t        |      dk(  r|dk7  ryt        |      \  }}t	        |      r7|s5|sd}|D cg c]  }|t        |       }	}|	r|j                  |	      S dS t        |      s|rF|xs t        |j                               }d|sdd}|j                  fd|D              }
|
r|
S dS |r|d| n|}t        |      S c c}w )zSLabel and path expansion method.

        Expands for operators: '/', '.'

        Nr   r   r)   %s=%s%s,%sc              3   ^   K   | ]$  \  }}|t        |      t        |      fz   & y wr%   rK   )rG   rQ   rH   
format_strr   s      r   rI   z4URIVariable._label_path_expansion.<locals>.<genexpr>   s>      % $daq} !TNE!TN #s   *-)
r>   r   lenrU   rV   rE   rW   rX   rY   rZ   )r   r9   r[   r.   r/   r>   r\   rZ   rH   	fragmentsexpandedrb   r   s              @@r   _label_path_expansionz!URIVariable._label_path_expansion   s    ==yy=SZ1_")%0UF16HA!-q$IH/88==+BdBUv2VEKKM2E J$
}} % $% H
  (81T1"(gveUD!!% Is   C)C)c                 h  	 | j                   }| j                  	|y| j                  dk(  rd}t        |      \  }}t	        |      rQ|sO|r!|j                  	fd|D              }|r|S dS dj                  	fd|D              }dj                  |      S t        |      s|re|xs t        |j                               }|r|j                  	fd|D              S dj                  	fd	|D              }dj                  |      S |r|d| n|}|rdj                  t        |	            S S )
z"Expansion method for ';' operator.Nr   r   c              3   Z   K   | ]"  }|dj                  t        |             $ y wNrB   rC   rF   s     r   rI   z3URIVariable._semi_path_expansion.<locals>.<genexpr>   s3      )  %! NNeAtn$s   (+r)   c              3   6   K   | ]  }t        |        y wr%   rK   rL   s     r   rI   z3URIVariable._semi_path_expansion.<locals>.<genexpr>   rM   rN   rB   c              3   t   K   | ]/  \  }}|(dj                  t        |      t        |             1 y wri   rC   rP   s      r   rI   z3URIVariable._semi_path_expansion.<locals>.<genexpr>   s=      % #($!Q1= NNaa"'   58c              3   t   K   | ]/  \  }}|(dj                  t        |      t        |             1 y w)NrT   rC   rP   s      r   rI   z3URIVariable._semi_path_expansion.<locals>.<genexpr>  s=      $ #($!Q1= NNaa"'rl   )r>   r   r   rU   rV   rW   rD   rX   rY   rZ   rE   )
r   r9   r[   r.   r/   r>   r\   rZ   re   r   s
    `       @r   _semi_path_expansionz URIVariable._semi_path_expansion   s=   ==yy===CH)%0UF#== )  %) 
 $,x55 ? ??~~dE22Uv2VEKKM2E}} % #(%   88 $ #($ 
 ~~dH55"(gve>>$eT(:;;r   c                 J    |y t        |      \  }}t        |      r|sdj                   fd|D              S t        |      s|r>|xs t	        |j                               }|rdnddj                   fd|D              S |r|d | n|}t        | j                        S )Nr)   c              3   J   K   | ]  }t        |j                          y wr%   rE   r   )rG   rH   r   s     r   rI   z0URIVariable._string_expansion.<locals>.<genexpr>  s     ?AE!TYY/s    #r_   r`   c              3      K   | ]6  \  }}t        |j                        t        |j                        fz   8 y wr%   rq   )rG   rQ   rH   rb   r   s      r   rI   z0URIVariable._string_expansion.<locals>.<genexpr>  sD       $da !TYY'q$)))< #s   <?)rU   rV   rW   rX   rY   rZ   rE   r   )r   r9   r[   r.   r/   r\   rZ   rb   s   `      @r   _string_expansionzURIVariable._string_expansion  s    =)%0UF88????Uv2VEKKM2E$+J88  $   #)gveUDII&&r   Nc                    g }| j                   D ]  \  }}|j                  |d      }|s"|dk7  r|| j                  v r| j                  |   }|?d}| j                  dv r| j                  }nC| j                  dv r| j
                  }n(| j                  dk(  r| j                  }n| j                  } ||||d   |d         }||j                  |        d}|r(| j                  | j                  j                  |      z   }| 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   rf   r]   rn   rs   r6   r=   r>   rW   r   )r   var_dictreturn_valuesr9   optsr[   re   	expansions           r   expandzURIVariable.expand(  s   6 ..JD$LLt,EUb[TT]]-Bd+}H}}
* 66	*, 11	#% 55	 22	 ud9otH~NH#$$X.+ ). JJ!3!3M!BBEu%%r   r%   )__name__
__module____qualname____doc__r0   r1   r   r#   r&   r   r   r]   rf   rn   rs   rz   r!   r   r   r   r   "   sK    $ CI#H"(+KZ-*(T$"L.`',7&r   r   c                 `    | r(t        | t        t        f      rt        d | D              syd| fS )Nc              3   \   K   | ]$  }t        |t              xr t        |      d k(   & yw)r   N)
isinstancer3   rc   )rG   ts     r   rI   z$is_list_of_tuples.<locals>.<genexpr>e  s(     H%QJq%(8SVq[8%s   *,)FNT)r   listr3   allr[   s    r   rU   rU   b  s.    54-0H%HH;r   c                 .    t        | t        t        f      S r%   )r   r   r3   r   s    r   rV   rV   k  s    edE]++r   c                 B    t        | t        t        j                  f      S r%   )r   dictcollections_abcMutableMappingr   s    r   rX   rX   o  s    edO$B$BCDDr   c                 b    t        | t              rt        | dd       | j                  |      S | S )Nencode)r   texttypegetattrr   )r[   encodings     r   _encoder   {  s/    5(#E8T*6||H%%Lr   c                 v    t        | t              st        |       } t        j                  t        |       |      S r%   )r   stringlikesstrurllibrE   r   )r[   r   s     r   rE   rE     s*    e[)E
<<--r   )zutf-8)r~   syscollections.abcabcr   ImportErrorcollectionsversion_infomajorr   urllib.parser   objectr   rU   rV   rX   unicoder   	NameErrorr   bytesr   r   rE   r!   r   r   <module>r      s   " 
*- Qq !}&& }&@
,EH .S  *)*x
  Hs#   A.  A= .	A:9A:=BB