³ò
Je|Mc           @   s  d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k l Z d d k l	 Z	 y d d k l
 Z
 Wn# e j
 o d d k l
 Z
 n Xe d „ Z e d „ Z d „  Z d	 „  Z d
 e f d „  ƒ  YZ d e	 f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s  Utility functions used by various web helpers.

This module contains support functions used by other helpers, and functions for
URL manipulation. Most of these helpers predate the 0.6 reorganization; they
would have been put in other subpackages if they have been created later.
iÿÿÿÿN(   t	   DictMixin(   t   XMLGenerator(   t   parse_qsc   	      K   s  t  i |  ƒ \ } } d | j o( | i d d ƒ \ } } t | ƒ } n h  } xM | i ƒ  D]? \ } } | d j	 o | | | <q] | | j o | | =q] q] W| o | | | f Sn t i | t ƒ } | o d | } n | o d | } n d | | | f S(   s  Update query parameters in a URL.

    ``_url`` is any URL, with or without a query string.

    ``\*\*params`` are query parameters to add or replace. Each value may be a
    string, a list of strings, or None. Passing a list generates multiple
    values for the same parameter. Passing None deletes the corresponding
    parameter if present.

    Return the new URL.

    *Debug mode:* if a pseudo-parameter ``_debug=True`` is passed,
    return a tuple: ``[0]`` is the URL without query string or fragment,
    ``[1]`` is the final query parameters as a dict, and ``[2]`` is the
    fragment part of the original URL or the empty string.

    Usage:

    >>> update_params("foo", new1="NEW1")
    'foo?new1=NEW1'
    >>> update_params("foo?p=1", p="2")
    'foo?p=2'
    >>> update_params("foo?p=1", p=None)
    'foo'
    >>> update_params("http://example.com/foo?new1=OLD1#myfrag", new1="NEW1")
    'http://example.com/foo?new1=NEW1#myfrag'
    >>> update_params("http://example.com/foo?new1=OLD1#myfrag", new1="NEW1", _debug=True)
    ('http://example.com/foo', {'new1': 'NEW1'}, 'myfrag')
    >>> update_params("http://www.mau.de?foo=2", brrr=3)
    'http://www.mau.de?foo=2&brrr=3'
    >>> update_params("http://www.mau.de?foo=A&foo=B", foo=["C", "D"])
    'http://www.mau.de?foo=C&foo=D'

    t   ?i   t   #s   %s%s%sN(	   t   urlparset	   urldefragt   splitR   t	   iteritemst   Nonet   urllibt	   urlencodet   True(	   t   _urlt   _debugt   paramst   urlt   fragmentt   qst   queryt   keyt   value(    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   update_params   s&    # c         C   sœ   d |  j o |  i  d d ƒ }  n d |  j o |  i  d d ƒ }  n d |  j o |  i  d d ƒ }  n | o( |  i  d d ƒ }  |  i  d	 d
 ƒ }  n |  S(   sÀ  Replace special characters '&', '<' and '>' by SGML entities.

    This is a slightly more efficient version of the cgi.escape by
    using 'in' membership to test if the replace is needed.

    This function returns a plain string. Programs using the HTML builder
    should call ``webhelpers.html.builder.escape()`` instead of this to prevent
    double-escaping.

    Changed in WebHelpers 1.2: escape single-quote as well as double-quote.

    t   &s   &amp;t   <s   &lt;t   >s   &gt;t   "s   &quot;t   's   &apos;(   t   replace(   t   st   quote(    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt
   cgi_escapeL   s    c         C   sŽ   |  d j o d Sn t |  t ƒ p0 t |  d ƒ o t |  ƒ }  qU t |  ƒ }  n t |  t ƒ }  t |  t ƒ o |  i d d ƒ }  n |  S(   sÚ  HTML-escape a string or object.
    
    This converts any non-string objects passed into it to strings
    (actually, using ``unicode()``).  All values returned are
    non-unicode strings (using ``&#num;`` entities for all non-ASCII
    characters).
    
    None is treated specially, and returns the empty string.
    
    This function returns a plain string. Programs using the HTML builder
    should wrap the result in ``literal()`` to prevent double-escaping.

    t    t   __unicode__t   asciit   xmlcharrefreplaceN(	   R	   t
   isinstancet
   basestringt   hasattrt   unicodet   strR   R   t   encode(   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   html_escaped   s    c         C   s(   |  d j o |  Sn t i |  d d ƒS(   s  
    Convert an IRI portion to a URI portion suitable for inclusion in a URL.

    (An IRI is an Internationalized Resource Identifier.)

    This is the algorithm from section 3.1 of RFC 3987.  However, since 
    we are assuming input is either UTF-8 or unicode already, we can 
    simplify things a little from the full method.

    Returns an ASCII string containing the encoded result.

    t   safes   /#%[]=:;$&()+,!?N(   R	   R
   R   (   t   iri(    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt
   iri_to_uri   s    t   Partialc           B   s    e  Z d  Z d „  Z d „  Z RS(   so   
    A partial function object.

    Equivalent to functools.partial, which was introduced in Python 2.5.

    c          O   s3   |  d } |  d |  d | | _  | _ | _ d  S(   Ni    i   i   (   t   fnt   argst   kw(   R0   R1   t   self(    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   __init__Ÿ   s    
c         O   sX   | o* |  i  o  |  i  i ƒ  } | i | ƒ n | p |  i  } |  i |  i | | Ž  S(   N(   R1   t   copyt   updateR/   R0   (   R2   R0   R1   t   d(    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   __call__£   s
    (   t   __name__t
   __module__t   __doc__R3   R7   (    (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR.   –   s   	t   SimplerXMLGeneratorc           B   s   e  Z d  Z d d d „ Z RS(   s(   A subclass of Python's SAX XMLGenerator.c         C   sV   | d j o
 h  } n |  i | | ƒ | d j	 o |  i | ƒ n |  i | ƒ d S(   s    Add an element with no children.N(   R	   t   startElementt
   characterst
   endElement(   R2   t   namet   contentst   attrs(    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   addQuickElement°   s    
N(   R8   R9   R:   R	   RB   (    (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR;   «   s   t   UnicodeMultiDictc           B   s
  e  Z d  Z d d d e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z e Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e Z d „  Z d „  Z d „  Z d „  Z RS(   s'  
    A MultiDict wrapper that decodes returned values to unicode on the fly. 
    
    Decoding is not applied to assigned values.

    The key/value contents are assumed to be ``str``/``strs`` or
    ``str``/``FieldStorages`` (as is returned by the :func:`paste.request.parse`
    functions).

    Can optionally also decode keys when the ``decode_keys`` argument is
    True.

    ``FieldStorage`` instances are cloned, and the clone's ``filename``
    variable is decoded. Its ``name`` variable is decoded when ``decode_keys``
    is enabled.

    t   strictc         C   sE   | |  _  | d  j o t i ƒ  } n | |  _ | |  _ | |  _ d  S(   N(   t   multiR	   t   syst   getdefaultencodingt   encodingt   errorst   decode_keys(   R2   RE   RH   RI   RJ   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR3   Í   s    			c         C   s*   |  i  o | i |  i |  i ƒ } n | S(   N(   RJ   t   decodeRH   RI   (   R2   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   _decode_keyÖ   s    
c         C   s¦   t  | t i ƒ o] t i | ƒ } |  i o" | i i |  i |  i ƒ | _ n | i	 i |  i |  i ƒ | _	 n3 y | i |  i |  i ƒ } Wn t
 j
 o n X| S(   s    
        Decode the specified (``str`` or `FieldStorage``) value to unicode. 
        
        ``FieldStorage`` objects are specially handled.
        
        (   R$   t   cgit   FieldStorageR4   RJ   R?   RK   RH   RI   t   filenamet   AttributeError(   R2   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   _decode_valueÛ   s    
""c         C   s   |  i  |  i i | ƒ ƒ S(   N(   RQ   RE   t   __getitem__(   R2   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRR   ï   s    c         C   s   |  i  i | | ƒ d  S(   N(   RE   t   __setitem__(   R2   R   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRS   ò   s    c         C   s   |  i  i | | ƒ d S(   s:   Add the key and value, not overwriting any previous value.N(   RE   t   add(   R2   R   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRT   õ   s    c         C   s4   g  } |  i  i | ƒ D] } | |  i | ƒ q ~ S(   sB   Return list of all values matching the key (may be an empty list).(   RE   t   getallRQ   (   R2   R   t   _[1]t   v(    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRU   ù   s    c         C   s   |  i  |  i i | ƒ ƒ S(   sC   Return one value matching key.  Raise KeyError if multiple matches.(   RQ   RE   t   getone(   R2   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRX   ý   s    c         C   s   h  } xƒ |  i  i ƒ  i ƒ  D]l \ } } t | t ƒ o. g  } | D] } | |  i | ƒ qC ~ } n |  i | ƒ } | | |  i | ƒ <q W| S(   sb  Return dict where values are single values or a list of values.

        The value is a single value if key appears just once.  It is 
        a list of values when a key/value appears more than once in this 
        dictionary.  This is similar to the kind of dictionary often 
        used to represent the variables in a web request.
        
        (   RE   t   mixedR   R$   t   listRQ   RL   (   R2   t   unicode_mixedR   R   RV   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRY     s    	 .c         C   sm   h  } x` |  i  i ƒ  i ƒ  D]I \ } } g  } | D] } | |  i | ƒ q3 ~ } | | |  i | ƒ <q W| S(   s?   Return dict where each key is associated with a list of values.(   RE   t   dict_of_listsR   RQ   RL   (   R2   t   unicode_dictR   R   RV   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR\     s     *c         C   s   |  i  i | ƒ d  S(   N(   RE   t   __delitem__(   R2   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR^     s    c         C   s   |  i  i | ƒ S(   N(   RE   t   __contains__(   R2   R   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR_     s    c         C   s   |  i  i ƒ  d  S(   N(   RE   t   clear(   R2   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR`   #  s    c         C   s   t  |  i i ƒ  |  i |  i ƒ S(   N(   RC   RE   R4   RH   RI   (   R2   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR4   &  s    c         C   s   |  i  |  i i | | ƒ ƒ S(   N(   RQ   RE   t
   setdefault(   R2   R   t   default(    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRa   )  s    c         G   s   |  i  |  i i | | Œ ƒ S(   N(   RQ   RE   t   pop(   R2   R   R0   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRc   ,  s    c         C   s1   |  i  i ƒ  \ } } |  i | ƒ |  i | ƒ f S(   N(   RE   t   popitemRL   RQ   (   R2   t   kRW   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRd   /  s    c         C   sH   d i  g  } |  i ƒ  D] } | d | q ~ ƒ } d |  i i | f S(   Ns   , s   (%r, %r)s   %s([%s])(   t   joint   itemst	   __class__R8   (   R2   RV   RW   Rg   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   __repr__3  s    4c         C   s   |  i  i ƒ  S(   N(   RE   t   __len__(   R2   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRj   7  s    c         C   s1   g  } |  i  i ƒ  D] } | |  i | ƒ q ~ S(   N(   RE   t   iterkeysRL   (   R2   RV   Re   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   keys>  s    c         c   s,   x% |  i  i ƒ  D] } |  i | ƒ Vq Wd  S(   N(   RE   Rk   RL   (   R2   Re   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRk   A  s     c         C   sF   g  } |  i  i ƒ  D]+ \ } } | |  i | ƒ |  i | ƒ f q ~ S(   N(   RE   R   RL   RQ   (   R2   RV   Re   RW   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRg   G  s    c         c   sA   x: |  i  i ƒ  D]) \ } } |  i | ƒ |  i | ƒ f Vq Wd  S(   N(   RE   R   RL   RQ   (   R2   Re   RW   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyR   K  s     c         C   s1   g  } |  i  i ƒ  D] } | |  i | ƒ q ~ S(   N(   RE   t
   itervaluesRQ   (   R2   RV   RW   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyt   valuesO  s    c         c   s,   x% |  i  i ƒ  D] } |  i | ƒ Vq Wd  S(   N(   RE   Rm   RQ   (   R2   RW   (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRm   R  s     N(    R8   R9   R:   R	   t   FalseR3   RL   RQ   RR   RS   RT   RU   RX   RY   R\   R^   R_   t   has_keyR`   R4   Ra   Rc   Rd   Ri   Rj   Rl   Rk   t   __iter__Rg   R   Rn   Rm   (    (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pyRC   ¹   s:   																							(   R:   RM   R4   RF   R
   R   t   UserDictR    t   xml.sax.saxutilsR   R   t   ImportErrorRo   R   R   R*   R-   t   objectR.   R;   RC   (    (    (    s_   /afs/athena.mit.edu/user/x/a/xavid/Public/bazki/lib/WebHelpers-1.2-py2.5.egg/webhelpers/util.pys   <module>   s$   7		