ó
‚¾^Yc           @   s  d  Z  d d l Z d d l Z d d f e j k oD d d f k  n rX d d l Z n= d d f e j k o~ d d f k  n r• d d l j Z n  d	 e f d
 „  ƒ  YZ d „  Z	 d „  Z
 d „  Z y
 e Z Wn e k
 ré e Z n Xe e f Z d d „ Z d „  Z d S(   sâ   

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?
>

iÿÿÿÿNi   i   i   i   i   i    t   URIVariablec        
   B   sw   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 d „ Z RS(   s  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.

    t   +t   #t   .t   /t   ;t   ?t   &t   |t   !t   @s   :/?#[]@!$&'()*+,;=c         C   sN   | |  _  d |  _ d |  _ g  |  _ g  |  _ i  |  _ |  j ƒ  |  j ƒ  d  S(   Nt    (   t   originalt   operatort   safet	   variablest   variable_namest   defaultst   parset
   post_parse(   t   selft   var(    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   __init__2   s    						
c         C   s   d |  S(   Ns   URIVariable(%s)(    (   R   (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   __repr__C   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   __str__F   s    c   	      C   s‡  |  j  } |  j  d t j k r? |  j  d |  _ |  j  d } n  |  j t j d  k rd t j |  _ n  | j d ƒ } xè | D]à } d } | } d | k r¹ t | j d d ƒ ƒ \ } } n  t	 } | j
 d ƒ rá t } | d  } n  d } d | k r t | j d d ƒ ƒ \ } } t | ƒ } n  | r6| |  j | <n  |  j j | i | d	 6| d
 6f ƒ qz Wg  |  j D] \ } } | ^ qh|  _ d S(   s©   Parse the variable.

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

        i    i   i   t   ,t   =t   *iÿÿÿÿt   :t   explodet   prefixN(   R   R    t	   operatorsR   t   reservedR   t   splitt   Nonet   tuplet   Falset   endswitht   Truet   intR   R   t   appendR   (	   R   t   var_listR   t   default_valt   nameR   R   t   varnamet   _(    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyR   I   s2    
	!	c         C   s³   d |  _  |  j |  _ |  _ |  j d k r7 d |  _ n  |  j d k rR d |  _ n  |  j d k rm d |  _ n  |  j d k r‘ d |  _ d |  _ n  |  j d	 k r¯ t j |  _  n  d S(
   sÍ   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   N(   R   R   R   (   R   R   (   R   R   t   startt   join_strR    R    (   R   (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyR   v   s    		c            sf  | d k r d St | ƒ \ } } |  j ‰ t | ƒ r¡ | r¡ | sH d S| rq |  j j ‡  ‡ f d †  | Dƒ ƒ Sd j ‡ f d †  | Dƒ ƒ } d ˆ  | f Sn  t | ƒ s³ | r+| s½ d S| pÒ t | j ƒ  ƒ } | rû |  j j ‡ f d †  | Dƒ ƒ Sd j ‡ f d †  | Dƒ ƒ } d ˆ  | f Sn  | r^| rA| |  n | } d ˆ  t	 | ˆ ƒ f Sˆ  d S(	   s/   Expansion method for the '?' and '&' operators.c         3   s(   |  ] } d  ˆ  t  | ˆ ƒ f Vq d S(   s   %s=%sN(   t   quote(   t   .0t   v(   R+   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>˜   s    R   c         3   s   |  ] } t  | ˆ  ƒ Vq d  S(   N(   R0   (   R1   R2   (   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>›   s    s   %s=%sc         3   s7   |  ]- \ } } d  t  | ˆ  ƒ t  | ˆ  ƒ f Vq d S(   s   %s=%sN(   R0   (   R1   t   kR2   (   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>¤   s   c         3   s7   |  ]- \ } } d  t  | ˆ  ƒ t  | ˆ  ƒ f Vq d S(   s   %s,%sN(   R0   (   R1   R3   R2   (   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>ª   s   R   N(
   R"   t   is_list_of_tuplesR   t	   list_testR/   t   joint	   dict_testt   sortedt   itemsR0   (   R   R+   t   valueR   R   t   tuplesR9   (    (   R+   R   s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   _query_expansion‹   s8    			c   	         s<  |  j  } |  j ‰ ˆ d k s< t ˆ ƒ d k r@ ˆ d k r@ d St ˆ ƒ \ } } t ˆ ƒ r¤ | r¤ | st d } n  | j ‡ ‡ f d †  ˆ Dƒ ƒ } | r  | Sd St ˆ ƒ s¶ | r| pË t ˆ j	 ƒ  ƒ } d ‰  | sé d ‰  d } n  | j ‡  ‡ f d †  | Dƒ ƒ } | r| Sd S| r)ˆ |  n ˆ ‰ t
 ˆ ˆ ƒ S(	   sS   Label and path expansion method.

        Expands for operators: '/', '.'

        i    R   R   c         3   s*   |  ]  } ˆ d  k	 r t | ˆ  ƒ Vq d  S(   N(   R"   R0   (   R1   R2   (   R   R:   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>È   s    s   %s=%ss   %s,%sc         3   sC   |  ]9 \ } } | d  k	 r ˆ  t | ˆ ƒ t | ˆ ƒ f Vq d  S(   N(   R"   R0   (   R1   R3   R2   (   t
   format_strR   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>Ô   s   N(   R/   R   R"   t   lenR4   R5   R6   R7   R8   R9   R0   (	   R   R+   R:   R   R   R/   R;   R9   t   expanded(    (   R=   R   R:   s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   _label_path_expansionµ   s.    		*		c   	         sy  |  j  } |  j ‰ | d	 k r" d	 S|  j d k r: d } n  t | ƒ \ } } t | ƒ rÅ | rÅ | r• | j ‡  ‡ f d †  | Dƒ ƒ } | r‘ | Sd	 Sd j ‡ f d †  | Dƒ ƒ } d ˆ  | f Sn  t | ƒ s× | rB| pì t | j	 ƒ  ƒ } | r| j ‡ f d †  | Dƒ ƒ Sd j ‡ f d †  | Dƒ ƒ } d ˆ  | f Sn  | rR| |  n | } | rud ˆ  t
 | ˆ ƒ f Sˆ  S(
   s"   Expansion method for ';' operator.R   R   c         3   s4   |  ]* } | d k	 r d  ˆ  t | ˆ ƒ f Vq d S(   s   %s=%sN(   R"   R0   (   R1   R2   (   R+   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>í   s   R   c         3   s   |  ] } t  | ˆ  ƒ Vq d  S(   N(   R0   (   R1   R2   (   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>ó   s    s   %s=%sc         3   sC   |  ]9 \ } } | d k	 r d  t | ˆ  ƒ t | ˆ  ƒ f Vq d S(   s   %s=%sN(   R"   R0   (   R1   R3   R2   (   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>û   s   c         3   sC   |  ]9 \ } } | d k	 r d  t | ˆ  ƒ t | ˆ  ƒ f Vq d S(   s   %s,%sN(   R"   R0   (   R1   R3   R2   (   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>  s   N(   R/   R   R"   R   R4   R5   R6   R7   R8   R9   R0   (	   R   R+   R:   R   R   R/   R;   R9   R?   (    (   R+   R   s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   _semi_path_expansionÝ   s:    			c            sÔ   | d  k r d  St | ƒ \ } } t | ƒ rR | rR d j ‡ f d †  | Dƒ ƒ St | ƒ sd | r® | py t | j ƒ  ƒ } | rˆ d n d ‰  d j ‡  ‡ f d †  | Dƒ ƒ S| r¾ | |  n | } t | ˆ j ƒ S(   NR   c         3   s!   |  ] } t  | ˆ  j ƒ Vq d  S(   N(   R0   R   (   R1   R2   (   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>  s    s   %s=%ss   %s,%sc         3   s=   |  ]3 \ } } ˆ  t  | ˆ j ƒ t  | ˆ j ƒ f Vq d  S(   N(   R0   R   (   R1   R3   R2   (   R=   R   (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>  s   (	   R"   R4   R5   R6   R7   R8   R9   R0   R   (   R   R+   R:   R   R   R;   R9   (    (   R=   R   s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   _string_expansion  s    c         C   sE  g  } x|  j  D]û \ } } | j | d	 ƒ } | r` | d k r` | |  j k r` |  j | } n  | d	 k rr q n  d	 } |  j d
 k r“ |  j } n? |  j d k r® |  j } n$ |  j d k rÉ |  j } n	 |  j } | | | | d | d ƒ } | d	 k	 r | j	 | ƒ q q Wd } | r7|  j
 |  j j | ƒ } n  i | |  j 6S(   s1  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'}

        R   R   R   R   R   R   R   R   N(   R   R   (   R   R   (   R   t   getR"   R   R   R@   R<   RA   RB   R(   R.   R/   R6   R   (   R   t   var_dictt   return_valuesR+   t   optsR:   R?   t	   expansion(    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   expand#  s,    "	(
   R   R   R   R   R   R   R   R   R	   R
   N(   t   __name__t
   __module__t   __doc__R   R    R   R   R   R   R   R<   R@   RA   RB   R"   RH   (    (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyR       s   				-		*	(	0	c         C   sH   |  s4 t  |  t t f ƒ s4 t d „  |  Dƒ ƒ r> t d  f St |  f S(   Nc         s   s0   |  ]& } t  | t ƒ o' t | ƒ d  k Vq d S(   i   N(   t
   isinstanceR#   R>   (   R1   t   t(    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pys	   <genexpr>`  s    (   RL   t   listR#   t   allR$   R"   R&   (   R:   (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyR4   ]  s
    
c         C   s   t  |  t t f ƒ S(   N(   RL   RN   R#   (   R:   (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyR5   f  s    c         C   s   t  |  t t j f ƒ S(   N(   RL   t   dictt   collectionst   MutableMapping(   R:   (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyR7   j  s    s   utf-8c         C   s8   t  |  t ƒ r4 t |  d d  ƒ d  k	 r4 |  j | ƒ S|  S(   Nt   encode(   RL   t   texttypet   getattrR"   RS   (   R:   t   encoding(    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   _encodev  s    c         C   s4   t  |  t ƒ s t |  ƒ }  n  t j t |  ƒ | ƒ S(   N(   RL   t   stringlikest   strt   urllibR0   RW   (   R:   R   (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyR0   }  s    (   RK   RQ   t   syst   version_infoRZ   t   urllib.parseR   t   objectR    R4   R5   R7   t   unicodeRT   t	   NameErrorRY   t   bytesRX   RW   R0   (    (    (    s9   /tmp/pip-build-kpPAdC/uritemplate/uritemplate/variable.pyt   <module>   s$   ++ÿ C				

