ó
œ-'Nc           @   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 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(   sC   Something which must apply to any of a number of different schemas.c         G   s   | |  _  d S(   sE   
        @param schemas: Any number of other schema objects.
        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   3   s    (   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   1   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   9   s   t   Stringc           B   s   e  Z d  Z d   Z RS(   s    Something that must be a C{str}.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   C   s    (   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   A   s   t   Unicodec           B   s   e  Z d  Z d   Z RS(   s$   Something that must be a C{unicode}.c         C   s)   t  | t  s% t d | f   n  | S(   Ns   %r isn't a unicode(   R   t   unicodeR    (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   K   s    (   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   I   s   t   UnicodeOrStringc           B   s    e  Z d  Z d   Z d   Z RS(   ss   Something that must be a C{unicode} or {str}.

    If the value is a C{str}, it will automatically be decoded.
    c         C   s   | |  _  d S(   sU   
        @param encoding: The encoding to automatically decode C{str}s with.
        N(   t   encoding(   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   W   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    R   (   R   R   t   e(    (    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   Q   s   	t   Listc           B   s    e  Z d  Z d   Z d   Z RS(   s"   Something which must be a C{list}.c         C   s   | |  _  d S(   sS   
        @param schema: The schema that all values of the list must match.
        N(   R   (   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   j   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   p   s    (   R   R   R   R   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyR   h   s   	t   Tuplec           B   s    e  Z d  Z d   Z d   Z RS(   s-   Something which must be a fixed-length tuple.c         G   s   | |  _  d S(   s   
        @param schema: A sequence of schemas, which will be applied to
            each value in the tuple respectively.
        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#   ~   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.
    c         C   s1   | d k r g  } n  t |  |  _ | |  _ d S(   su   
        @param schema: A dict mapping keys to schemas that the values
            of those keys must match.
        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(   s6   Something which must be a C{dict} with arbitrary keys.c         C   s   | |  _  | |  _ d S(   s   
        @param key_schema: The schema that keys must match.
        @param value_schema: The schema that values must match.
        N(   t
   key_schemat   value_schema(   R   R7   R8   (    (    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   itemsR8   R   R7   (   R   R   R0   R1   R2   (    (    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.pyR6   ž   s   	t   Messagec           B   s#   e  Z d  Z 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.
    c         C   s   | |  _  t   | d <t t   t d    | d <t |  | d <| d  k	 rd | j d d g  n d d g } t t |   j	 | d | d  S(   Nt	   timestampt   apit   typeR,   (
   R=   R   R	   R   R   R*   t   extendt   superR:   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?   R:   R   (   R   R   R1   (    (    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   N(   R   t	   ExceptionR    t   objectR   R	   R   R   R   R   R   R   R   R#   R)   R6   R:   (    (    (    s4   /usr/lib/python2.7/dist-packages/landscape/schema.pyt   <module>   s   %