Ρς
Α£vLc           @   s   d  e  f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d e f d	     YZ d
   Z d e  f d     YZ d S(   t   Valuec           B   sq   e  Z d  Z d Z d   Z d   Z d d  Z d   Z d   Z	 d   Z
 d   Z d   Z d	   Z d
   Z RS(   sJ  Python descriptor for marshalling GET/POST state into Python
    variables and back.

    The descriptor is given a name (see __init__) which it uses to
    read its state from a dictionary (containing GET/POST variables).

    The descriptor stores its state within the object from which it is
    invoked using its own name prefix with an underscore (so if x is a
    descripor in class Foo, and foo is an instance of Foo, then the
    actual value of x is stored in foo._x). If its state is missing
    from its host object (i.e. if _x is not an attribute of foo), the
    descriptor will return its default value (so foo.x will give a
    default value).

    This is a base class which will marshal strings from and to a
    GET/POST dictionary.

    How-To Guide for descriptors: http://users.rcn.com/python/download/Descriptor.htm
    c         C   s   | |  _  d  S(   N(   t   var_name(   t   selfR   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   __init__,   s    c         C   s   d |  i  S(   s   Return the name that we'll use to store this descriptor's
        data under in its host object. A decriptor foo will have its
        data stored under _foo.t   _(   R   (   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   _member_name/   s    c         C   s   t  | |  i   |  i  S(   N(   t   getattrR   t   default_value(   R   t   objt   type(    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   __get__5   s    c         C   s   t  | |  i   |  d  S(   N(   t   setattrR   (   R   R   t   value(    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   __set__8   s    c         C   s   t  | |  i    d  S(   N(   t   delattrR   (   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt
   __delete__;   s    c         C   s=   |  i  |  } | |  i j o |  i |  | |  i <n d S(   s   If the descriptor's value is not equal to its default
        value, then encode it to a string and store it in the GET/POST
        dictionary C{dct}.N(   R
   R   t   _encodeR   (   R   R   t   dctR   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   add_to_dict>   s    c         C   s>   y$ |  i  | |  i | |  i   Wn t j
 o n Xd S(   s   Read the value of this descriptor from the GET/POST
        dictionary C{params}, decode it and use it to set the
        descriptor value.N(   R   t   _decodeR   t   KeyError(   R   R   t   params(    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   read_from_paramsF   s    $c         C   s   | S(   N(    (   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   O   s    c         C   s   | S(   N(    (   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   R   s    c         C   s   d |  i  S(   Ns	   Value<%s>(   R   (   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   __repr__U   s    N(   t   __name__t
   __module__t   __doc__t   NoneR   R   R   R
   R   R   R   R   R   R   R   (    (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR       s   									t   BooleanValuec           B   s    e  Z e Z d    Z d   Z RS(   c         C   s   | d j o t  St Sd  S(   Nt   True(   R   t   False(   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   [   s    c         C   s   | o d Sd Sd  S(   NR   R   (    (   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   a   s    (   R   R   R   R   R   R   (    (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   X   s   	t   IntValuec           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s  Read a GET/POST variable into an integer. The descriptor's
    default value is supplied when initializing an IntValue. This
    value is used when the descriptor has no state stored in its host
    object or when the GET/POST variable doesn't decode to an integer.

        >>> class Foo(object):
        ...     bar = IntValue('bar', 1)
        >>> foo = Foo()
        >>> foo.bar
        1
        >>> foo.__class__.bar._decode('10')
        10
        >>> foo.__class__.bar._decode('a')
        1
        >>> foo.__class__.bar._encode(20)
        '20'
        >>> foo.__class__.bar.read_from_params(foo, {'bar': '5'})
        >>> foo.bar
        5
        >>> get_vars = {}
        >>> foo.__class__.bar.add_to_dict(foo, get_vars)
        >>> get_vars
        {'bar': '5'}
    c         C   s#   t  t |   i |  | |  _ d  S(   N(   t   superR   R   R   (   R   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR      s    c         C   s,   y t  |  SWn t j
 o |  i SXd  S(   N(   t   intt
   ValueErrorR   (   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR      s    c         C   s!   t  | t  p t  t |  S(   N(   t
   isinstanceR!   t   AssertionErrort   str(   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR      s    (   R   R   R   R   R   R   (    (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   g   s   		t   ChoiceValuec           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   sΞ  Read a string GET/POST variable ensuring that it matches one of
    the choices specified when constructing this object. If not, set
    this descriptor value to the first choice.

        >>> class Foo(object):
        ...     bar = ChoiceValue('bar', ('chocolate', 'strawberry', 'caramel'))
        >>> foo = Foo()
        >>> foo.__class__.bar._decode('strawberry')
        'strawberry'
        >>> foo.__class__.bar._decode('banana')
        'chocolate'
    c         C   s0   t  t |   i |  | d |  _ | |  _ d  S(   Ni    (   R    R&   R   R   t   _choices(   R   R   t   choices(    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR      s    c         C   s    | |  i  j o | S|  i Sd  S(   N(   R'   R   (   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   ‘   s    c         C   s    | |  i  j o | S|  i Sd  S(   N(   R'   R   (   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   §   s    (   R   R   R   R   R   R   (    (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR&      s   		t	   ListValuec           B   s&   e  Z d  Z g  Z d   Z d   Z RS(   s0  Read a comma separated GET/POST variable into a Python list of
    strings.

        >>> class Foo(object):
        ...     bar = ListValue('bar')
        >>> foo = Foo()
        >>> foo.__class__.bar._decode('a,b,c')
        ['a', 'b', 'c']
        >>> foo.__class__.bar._encode(['a', 'b', 'c'])
        'a,b,c'
        >>> foo.__class__.bar.read_from_params(foo, {'bar': 'x,y,z'})
        >>> foo.bar
        ['x', 'y', 'z']
        >>> get_vars = {}
        >>> foo.__class__.bar.add_to_dict(foo, get_vars)
        >>> get_vars
        {'bar': 'x,y,z'}
    c         C   s#   | d j o g  S| i d  Sd  S(   Nt    t   ,(   R*   N(   R   t   split(   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   Β   s    c         C   s   d i  d   | D  S(   NR+   c         s   s   x |  ] } t  |  Vq Wd  S(   N(   R%   (   t   .0t   item(    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pys	   <genexpr>Ι   s   	 (   t   join(   R   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   Θ   s    (   R   R   R   R   R   R   (    (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR)   ­   s   	c         C   sh   xA |  i  D]6 } | | j o# | i |  t | | |  } q
 q
 W| i d   |  i i   D  | S(   s   Enumerate a class and all its subclasses in a depth-first post
    order traversal and collect all the Python descriptors into the
    list 'descriptors'c         s   s-   x& |  ] } t  | t  o	 | Vq q Wd  S(   N(   R#   R    (   R-   t
   descriptor(    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pys	   <genexpr>Σ   s   	 (   t	   __bases__t   addt   get_descriptorst   extendt   __dict__t
   itervalues(   t   clst   descriptorst   visitedt   base(    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR3   Λ   s    
  t   Statec           B   s5   e  Z d  Z d   Z d   Z h  d  Z d   Z RS(   sΔ   Base class for classes which, using any of the *Value classes
    as descriptors, will read GET or POST variables intelligently.

    To see an example implementation, look for TranslatePageState.c         C   s   t  |  i g  t    S(   sΙ  Return a list of all the *Value descriptors (all defined
        above) that are defined in the class of the current object as
        well as its superclasses.

        Thus, if we have the following subclass:

            >>> class FooState(State):
            ...     bar = IntValue('bar', 0)
            ...     baz = ListValue('baz')
            >>> state = FooState()
            >>> sorted(state.get_descriptors())
            ['bar', 'baz']
        (   R3   t	   __class__t   set(   R   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR3   έ   s    c         c   s5   x. |  i    D]  } | i t |  | i  f Vq Wd S(   s  Iterate through all the *Value descriptors returning
        (descriptor name, descriptor value) pairs.

            >>> class FooState(State):
            ...     bar = IntValue('bar', 0)
            ...     baz = ListValue('baz')
            >>> state = FooState(bar=1, baz=['a', 'b', 'c'])
            >>> sorted(list(state.iter_items()))
            [('bar', 1), ('baz', ['a', 'b', 'c'])]
        N(   R3   R   R   (   R   t   member(    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt
   iter_itemsν   s     c         K   sX   x$ |  i    D] } | i |  |  q Wx* | i   D] \ } } t |  | |  q4 Wd S(   s  Initialize a state object possibly reading initial state
        from C{data} (which is in raw text) and overriding those
        values with the keyword parameters C{initial} (these
        parameters are not raw, so for an IntValue, you'd pass an
        integer).
        N(   R3   R   t	   iteritemsR   (   R   t   datat   initialR>   t   keyR   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR   ό   s      c         C   s1   h  } x$ |  i    D] } | i |  |  q W| S(   sM   Encode all the state members in this object to a GET/POST
        dictionary.(   R3   R   (   R   t   resultR>   (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   encode  s
     (   R   R   R   R3   R?   R   RE   (    (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyR;   Χ   s
   		N(   t   objectR    R   R   R&   R)   R3   R;   (    (    (    s2   /var/www/Pootle/local_apps/pootle_app/url_state.pyt   <module>   s   B'	