
    -                        S r SSKr SSKJr  \R                  R                  S:X  a  SSK	r	O \R                  R                  S:X  a  SSK
Jr	   " S S\5      rS rS rS	 r \r\\4rSS
 jrS rg! \ a    SSKr Nof = f! \ a    \r N'f = f)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                   `    \ rS rSrSrSrSrS rS rS r	S r
S	 rS
 rS rS rS rSS jrSrg)URIVariable#   a  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                     Xl         SU l        SU l        / U l        / U l        0 U l        U R                  5         U R                  5         g )N )originaloperatorsafe	variablesvariable_namesdefaultsparse
post_parse)selfvars     /platform/bq/third_party/uritemplate/variable.py__init__URIVariable.__init__:   s>    	 

    c                     SU -  $ )NzURIVariable(%s) r   s    r   __repr__URIVariable.__repr__K   s     4''r!   c                     U R                   $ N)r   r$   s    r   __str__URIVariable.__str__N   s    }}r!   c                    U R                   nU R                   S   [        R                  ;   a#  U R                   S   U l        U R                   SS nU R                  [        R                  SS ;   a  [        R                  U l        UR                  S5      nU H  nSnUnSU;   a  [        UR                  SS5      5      u  pCSnUR                  S5      (       a  S	nUSS
 nSnSU;   a(  [        UR                  SS5      5      u  pF[        U5      nU(       a  X0R                  U'   U R                  R                  XEUS.45        M     U R                   VVs/ s H  u  pxUPM	     snnU l        gs  snnf )zyParse 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namer2   r3   varname_s	            r   r   URIVariable.parseQ   sJ    ===={444 MM!,DM}}QR(H==K11"155#,,DI>>#&CKDcz$)#))C*;$<!G}}S!!CRyFd{$TZZQ%78V&1d#NN!!f=>' . <@>>J><Gw>JJs   E2c                 T   SU l         U R                  =U l        U l        U R                  S:X  a  SU l        U R                  S;   a  SU l        U R                  S:X  a  SU l        U R                  S:X  a  SU l        SU l        U R                  S;   a  [        R
                  U l         g	g	)
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   r5   r$   s    r   r   URIVariable.post_parse~   s     	%)]]2
T]==CDJ==N*DM==CDJ==CDJDM==J&#,,DI 'r!   c                   ^^ Uc  g[        U5      u  pVU R                  m[        U5      (       ai  U(       db  U(       d  gU(       a&  U R                  R	                  UU4S jU 5       5      $ SR	                  U4S jU 5       5      nSR                  TU5      $ [        U5      (       d  U(       a  U(       d  gU=(       d    [        UR                  5       5      nU(       a%  U R                  R	                  U4S jU 5       5      $ SR	                  U4S jU 5       5      nSR                  TU5      $ U(       a*  U(       a  USU OUnSR                  T[        UT5      5      $ TS-   $ )	z/Expansion method for the '?' and '&' operators.Nc              3   \   >#    U  H!  nS R                  T[        UT5      5      v   M#     g7f{}={}Nformatquote.0vr=   r   s     r   	<genexpr>/URIVariable._query_expansion.<locals>.<genexpr>   s(      *BGQGNN4q$88%s   ),r-   c              3   <   >#    U  H  n[        UT5      v   M     g 7fr(   rK   rM   rN   r   s     r   rO   rP            ?Aq$   rH   c              3   t   >#    U  H-  u  pS R                  [        UT5      [        UT5      5      v   M/     g7frG   rI   rM   krN   r   s      r   rO   rP      s;      * #($! NNaa "'   58c              3   t   >#    U  H-  u  pS R                  [        UT5      [        UT5      5      v   M/     g7f){},{}NrI   rW   s      r   rO   rP      s;      ! #($! NNaa "'rY   r.   )
is_list_of_tuplesr   	list_testrC   joinrJ   	dict_testsorteditemsrK   )r   r=   valuer2   r3   tuplesra   r   s    `     @r   _query_expansionURIVariable._query_expansion   s8   =)%0yyUF}})) *BG*    ? ??~~dE22Uv2VEKKM2E}})) * #(*    ! #(! 
 ~~dE22&,E'6N%E>>$eT(:;;czr!   c                 N  ^^ U R                   nU R                  mUb  [        U5      S:X  a  US:w  a  g[        U5      u  pg[	        U5      (       aJ  U(       dC  U(       d  SnU Vs/ s H  oc  M  [        UT5      PM     n	nU	(       a  UR                  U	5      $ S$ [        U5      (       d  U(       aV  U=(       d    [        UR                  5       5      nSmU(       d  SmSnUR                  UU4S jU 5       5      n
U
(       a  U
$ S$ U(       a  USU OUn[        UT5      $ s  snf )zCLabel and path expansion method.

Expands for operators: '/', '.'

Nr   r   r-   %s=%s%s,%sc              3   h   >#    U  H'  u  pUc  M
  T[        UT5      [        UT5      4-  v   M)     g 7fr(   rR   )rM   rX   rN   
format_strr   s      r   rO   4URIVariable._label_path_expansion.<locals>.<genexpr>   s;      % $daq
!TNE!TN #s   	2"2)
rC   r   lenr\   r]   rK   r^   r_   r`   ra   )r   r=   rb   r2   r3   rC   rc   ra   rN   	fragmentsexpandedrj   r   s              @@r   _label_path_expansion!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   %D"/D"c                   ^^	 U R                   nU R                  m	Uc  gU R                  S:X  a  Sn[        U5      u  pg[	        U5      (       ab  U(       d[  U(       a'  UR                  UU	4S jU 5       5      nU(       a  U$ S$ SR                  U	4S jU 5       5      nSR                  TU5      $ [        U5      (       d  U(       aq  U=(       d    [        UR                  5       5      nU(       a  UR                  U	4S jU 5       5      $ SR                  U	4S	 jU 5       5      nSR                  TU5      $ U(       a  USU OUnU(       a  SR                  T[        UT	5      5      $ T$ )
z"Expansion method for ';' operator.Nr   r   c              3   d   >#    U  H%  oc  M  SR                  T[        UT5      5      v   M'     g 7fNrH   rI   rL   s     r   rO   3URIVariable._semi_path_expansion.<locals>.<genexpr>   s5      )  %!GNNeAtn $s   0#0r-   c              3   <   >#    U  H  n[        UT5      v   M     g 7fr(   rR   rS   s     r   rO   rt      rT   rU   rH   c              3   ~   >#    U  H2  u  pUc  M
  SR                  [        UT5      [        UT5      5      v   M4     g 7frs   rI   rW   s      r   rO   rt      s?      % #($!1GNNaa "'   	=-=c              3   ~   >#    U  H2  u  pUc  M
  SR                  [        UT5      [        UT5      5      v   M4     g 7f)Nr[   rI   rW   s      r   rO   rt     s?      $ #($!1GNNaa "'rw   )rC   r   r   r\   r]   r^   rJ   r_   r`   ra   rK   )
r   r=   rb   r2   r3   rC   rc   ra   rn   r   s
    `       @r   _semi_path_expansion URIVariable._semi_path_expansion   sA   ==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                   ^ ^ Uc  g [        U5      u  pV[        U5      (       a"  U(       d  SR                  U 4S jU 5       5      $ [        U5      (       d  U(       aI  U=(       d    [	        UR                  5       5      nU(       a  SOSmSR                  UU 4S jU 5       5      $ U(       a  US U OUn[        UT R                  5      $ )Nr-   c              3   P   >#    U  H  n[        UTR                  5      v   M     g 7fr(   rK   r   )rM   rN   r   s     r   rO   0URIVariable._string_expansion.<locals>.<genexpr>  s     ?AE!TYY//s   #&rg   rh   c              3      >#    U  H6  u  pT[        UTR                  5      [        UTR                  5      4-  v   M8     g 7fr(   r}   )rM   rX   rN   rj   r   s      r   rO   r~      sA       $da !TYY'q$)))< #s   >A)r\   r]   r^   r_   r`   ra   rK   r   )r   r=   rb   r2   r3   rc   ra   rj   s   `      @r   _string_expansionURIVariable._string_expansion  s    =)%0UF88????Uv2VEKKM2E$+J88  $   #)gveUDII&&r!   Nc                 H   / nU R                    H  u  p4UR                  US5      nU(       d$  US:w  a  X0R                  ;   a  U R                  U   nUc  MG  SnU R                  S;   a  U R                  nOFU R                  S;   a  U R
                  nO)U R                  S:X  a  U R                  nOU R                  nU" X5US   US   5      nUc  M  UR                  U5        M     SnU(       a(  U R                  U R                  R                  U5      -   nU R                  U0$ )a  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   r2   r3   )r   getr   r   ro   rd   ry   r   r:   rB   rC   r^   r   )r   var_dictreturn_valuesr=   optsrb   rn   	expansions           r   expandURIVariable.expand)  s   6 ..JDLLt,EUb[T]]-Bd+}H}}
* 66	*, 11	#% 55	 22	 d9otH~NH#$$X.+ ). JJ!3!3M!BBEu%%r!   )r   rC   r   r   r   rB   r   r   r(   )__name__
__module____qualname____firstlineno____doc__r4   r5   r   r%   r)   r   r   rd   ro   ry   r   r   __static_attributes__r#   r!   r   r   r   #   sK    $ CI#H"(+KZ-*(T$"L.`',7&r!   r   c                 ~    U (       a2  [        U [        [        45      (       a  [        S U  5       5      (       d  gSU 4$ )Nc              3   l   #    U  H*  n[        U[        5      =(       a    [        U5      S :H  v   M,     g7f)r   N)
isinstancer7   rl   )rM   ts     r   rO   $is_list_of_tuples.<locals>.<genexpr>f  s'     H%QJq%(8SVq[8%s   24)FNT)r   listr7   allrb   s    r   r\   r\   c  s4    54-00H%HHH;r!   c                 .    [        U [        [        45      $ r(   )r   r   r7   r   s    r   r]   r]   l  s    edE]++r!   c                 B    [        U [        [        R                  45      $ r(   )r   dictcollections_abcMutableMappingr   s    r   r_   r_   p  s    edO$B$BCDDr!   c                 n    [        U [        5      (       a  [        U SS 5      b  U R                  U5      $ U $ )Nencode)r   texttypegetattrr   )rb   encodings     r   _encoder   |  s2    5(##E8T*6||H%%Lr!   c                     [        U [        5      (       d  [        U 5      n [        R                  " [        U 5      U5      $ r(   )r   stringlikesstrurllibrK   r   )rb   r   s     r   rK   rK     s-    e[))E
<<--r!   )zutf-8)r   syscollections.abcabcr   ImportErrorcollectionsversion_infomajorr   urllib.parser   objectr   r\   r]   r_   unicoder   	NameErrorr   bytesr   r   rK   r#   r!   r   <module>r      s   " 
*- Qq !}&& }&@
,EH .S  *)*x
  Hs#   A1 "B 1
A>=A>BB