ó
ÍÿŠTc           @   s(  d  Z  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 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 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s"   A schema system. Yes. Another one!t   InvalidErrorc           B   s   e  Z d  Z RS(   s&   Raised when invalid input is received.(   t   __name__t
   __module__t   __doc__(    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR       s   t   Constantc           B   s    e  Z d  Z d „  Z d „  Z RS(   s1   Something that must be equal to a constant value.c         C   s   | |  _  d  S(   N(   t   value(   t   selfR   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyt   __init__   s    c         C   s/   | |  j  k r+ t d | |  j  f ƒ ‚ n  | S(   Ns   %r != %r(   R   R    (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyt   coerce   s    (   R   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   	   s   	t   Anyc           B   s    e  Z d  Z d „  Z d „  Z RS(   ss   Something which must apply to any of a number of different schemas.

    @param schemas: Other schema objects.
    c         G   s   | |  _  d  S(   N(   t   schemas(   R   R
   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR      s    c         C   sV   x6 |  j  D]+ } y | j | ƒ SWq
 t k
 r4 q
 Xq
 Wt d | |  j  f ƒ ‚ d S(   sˆ   
        The result of the first schema which doesn't raise
        L{InvalidError} from its C{coerce} method will be returned.
        s!   %r did not match any schema in %sN(   R
   R   R    (   R   R   t   schema(    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR      s    (   R   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR	      s   	t   Boolc           B   s   e  Z d  Z d „  Z RS(   s!   Something that must be a C{bool}.c         C   s)   t  | t ƒ s% t d | f ƒ ‚ n  | S(   Ns   %r is not a bool(   t
   isinstancet   boolR    (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   ,   s    (   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   *   s   t   Intc           B   s   e  Z d  Z d „  Z RS(   s,   Something that must be an C{int} or C{long}.c         C   s/   t  | t t f ƒ s+ t d | f ƒ ‚ n  | S(   Ns   %r isn't an int or long(   R   t   intt   longR    (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   4   s    (   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   2   s   t   Floatc           B   s   e  Z d  Z d „  Z RS(   s7   Something that must be an C{int}, C{long}, or C{float}.c         C   s2   t  | t t t f ƒ s. t d | f ƒ ‚ n  | S(   Ns   %r isn't a float(   R   R   R   t   floatR    (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   <   s    (   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   :   s   t   Bytesc           B   s   e  Z d  Z d „  Z RS(   s   A binary string.c         C   s)   t  | t ƒ s% t d | f ƒ ‚ n  | S(   Ns   %r isn't a str(   R   t   strR    (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   D   s    (   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   B   s   t   Unicodec           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s³   Something that must be a C{unicode}.

    If the value is a C{str}, it will automatically be decoded.

    @param encoding: The encoding to automatically decode C{str}s with.
    s   utf-8c         C   s   | |  _  d  S(   N(   t   encoding(   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   R   s    c         C   sƒ   t  | t ƒ rZ y | j |  j ƒ } WqZ t k
 rV } t d | t | ƒ f ƒ ‚ qZ Xn  t  | t ƒ s t d | f ƒ ‚ n  | S(   Ns   %r can't be decoded: %ss   %r isn't a unicode(   R   R   t   decodeR   t   UnicodeDecodeErrorR    t   unicode(   R   R   t   e(    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   U   s    #(   R   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   J   s   t   Listc           B   s    e  Z d  Z d „  Z d „  Z RS(   sn   Something which must be a C{list}.

    @param schema: The schema that all values of the list must match.
    c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   e   s    c         C   sž   t  | t ƒ s% t d | f ƒ ‚ n  t | ƒ } xf t | ƒ D]X \ } } y |  j j | ƒ | | <Wq> t k
 r• } t d | |  j | f ƒ ‚ q> Xq> W| S(   Ns   %r is not a lists   %r could not coerce with %s: %s(   R   t   listR    t	   enumerateR   R   (   R   R   t   new_listt   it   subvalueR   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   h   s    (   R   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   `   s   	t   Tuplec           B   s    e  Z d  Z d „  Z d „  Z RS(   s¤   Something which must be a fixed-length tuple.

    @param schema: A sequence of schemas, which will be applied to
        each value in the tuple respectively.
    c         G   s   | |  _  d  S(   N(   R   (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   }   s    c         C   s´   t  | t ƒ s% t d | f ƒ ‚ n  t | ƒ t |  j ƒ k rk t d t |  j ƒ t | ƒ | f ƒ ‚ n  g  } x6 t |  j | ƒ D]" \ } } | j | j | ƒ ƒ q„ Wt | ƒ S(   Ns   %r is not a tuples   Need %s items, got %s in %r(   R   t   tupleR    t   lenR   t   zipt   appendR   (   R   R   t	   new_valueR   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   €   s    %(   R   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR"   v   s   	t   KeyDictc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   sé   Something which must be a C{dict} with defined keys.

    The keys must be constant and the values must match a per-key schema.

    @param schema: A dict mapping keys to schemas that the values of those
        keys must match.
    c         C   s1   | d  k r g  } n  t | ƒ |  _ | |  _ d  S(   N(   t   Nonet   sett   optionalR   (   R   R   R+   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   ”   s    	c   	      C   s"  i  } t  | t ƒ s+ t d | f ƒ ‚ n  xœ | j ƒ  D]Ž \ } } | |  j k ro t d | |  j f ƒ ‚ n  y |  j | j | ƒ | | <Wq8 t k
 rÅ } t d | | |  j | | f ƒ ‚ q8 Xq8 Wt | j ƒ  ƒ } t |  j j ƒ  ƒ |  j } | | } | rt d | f ƒ ‚ n  | S(   Ns   %r is not a dict.s   %r is not a valid key as per %rs7   Value of %r key of dict %r could not coerce with %s: %ss   Missing keys %s(	   R   t   dictR    t	   iteritemsR   R   R*   t   keysR+   (	   R   R   t   new_dictt   kt   vR   t   new_keyst   required_keyst   missing(    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   š   s&    %
N(   R   R   R   R)   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR(   Œ   s   t   Dictc           B   s    e  Z d  Z d „  Z d „  Z RS(   s°   Something which must be a C{dict} with arbitrary keys.

    @param key_schema: The schema that keys must match.
    @param value_schema: The schema that values must match.
    c         C   s   | |  _  | |  _ d  S(   N(   t
   key_schemat   value_schema(   R   R6   R7   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   ·   s    	c         C   sn   t  | t ƒ s% t d | f ƒ ‚ n  i  } x< | j ƒ  D]. \ } } |  j j | ƒ | |  j j | ƒ <q8 W| S(   Ns   %r is not a dict.(   R   R,   R    t   itemsR7   R   R6   (   R   R   R/   R0   R1   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   »   s    &(   R   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR5   °   s   	t   Messagec           B   s&   e  Z d  Z d d d „ Z d „  Z RS(   s
  
    Like L{KeyDict}, but with three predefined keys: C{type}, C{api},
    and C{timestamp}. Of these, C{api} and C{timestamp} are optional.


    @param type: The type of the message. The C{type} key will need to
        match this as a constant.
    @param schema: A dict of additional schema in a format L{KeyDict}
        will accept.
    @param optional: An optional list of keys that should be optional.
    @param api: The server API version needed to send this message,
        if C{None} any version is fine.
    c         C   s™   | |  _  | |  _ t ƒ  | d <t t ƒ  t d  ƒ ƒ | d <t | ƒ | d <| d  k	 rm | j d d g ƒ n d d g } t t	 |  ƒ j
 | d | ƒd  S(   Nt	   timestampt   apit   typeR+   (   R<   R;   R   R	   R   R   R)   t   extendt   superR9   R   (   R   R<   R   R+   R;   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   Ò   s    		c         C   sL   x3 | j  ƒ  D]% } | |  j k r | j | ƒ q q Wt t |  ƒ j | ƒ S(   N(   R.   R   t   popR>   R9   R   (   R   R   R0   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   Þ   s    N(   R   R   R   R)   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR9   Ä   s   N(   R   t	   ExceptionR    t   objectR   R	   R   R   R   R   R   R   R"   R(   R5   R9   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyt   <module>   s   $