ó
åŽ‹]c           @   sÁ  d  d l  Z  d  d l m Z m Z m Z m Z m Z m Z m Z m	 Z	 m
 Z
 d  d l m Z d  d l m Z d  d l Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d e f d „  ƒ  YZ d! Z d „  Z d „  Z e ƒ  Z d „  Z d „  Z e e e e e e d „ Z d „  Z d e f d „  ƒ  YZ  i d e 6d e 6Z! i  Z" d „  Z# d „  Z$ d „  Z% d „  Z& e d" d „ Z' e d# d „ Z( d „  Z) i  Z* d „  Z+ d „  Z, e e d  „ Z- d S($   iÿÿÿÿN(	   t   CheckedPMapt   CheckedPSett   CheckedPVectort   CheckedTypet   InvariantExceptiont   _restore_picklet   get_typet   maybe_parse_user_typet   maybe_parse_many_user_types(   t   optional(   t   wrap_invariantc         C   s•   t  t g  | D]' } t | j j | i  ƒ j ƒ  ƒ ^ q g  ƒ ƒ |  | <xG t |  j ƒ  ƒ D]3 \ } } t | t ƒ rZ | |  | | <|  | =qZ qZ Wd  S(   N(   t   dictt   sumt   listt   __dict__t   gett   itemst
   isinstancet   _PField(   t   dctt   basest   namet   bt   kt   v(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt
   set_fields   s
    Gc            sE   t  d „  ‡  f d †  | Dƒ Dƒ ƒ } | rA t | d d ƒ ‚ n  d  S(   Nc         s   s!   |  ] \ } } | s | Vq d  S(   N(    (   t   .0t   is_okt
   error_code(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pys	   <genexpr>   s    	c         3   s   |  ] } | ˆ  ƒ Vq d  S(   N(    (   R   t	   invariant(   t   subject(    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pys	   <genexpr>   s    s   Global invariant failed(    (   t   tupleR   (   R   t
   invariantst   error_codes(    (   R   s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   check_global_invariants   s    	c         C   s5   t  | t ƒ r( |  t k r( | j | ƒ S|  | | ƒ S(   N(   R   R   t   PFIELD_NO_SERIALIZERt	   serialize(   t
   serializert   formatt   value(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyR$   #   s    c            sr   | j  rn t ‡  f d †  | j  Dƒ ƒ rn t  ˆ  ƒ } d j |  j | | j ƒ } t |  | | j  | | ƒ ‚ n  d  S(   Nc         3   s$   |  ] } t  ˆ  t | ƒ ƒ Vq d  S(   N(   R   R   (   R   t   t(   R'   (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pys	   <genexpr>+   s    s'   Invalid type for field {0}.{1}, was {2}(   t   typet   anyR&   t   __name__t
   PTypeError(   t   destination_clst   fieldR   R'   t   actual_typet   message(    (   R'   s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt
   check_type*   s    )c         C   s9   t  | ƒ } t | ƒ d k r" t St t | d ƒ |  ƒ S(   Ni    (   R   t   lent   Falset
   issubclassR   (   t   type_clst
   field_typet   types(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   is_type_cls1   s    c         C   s9   | s
 t  St |  | j ƒ s  t  Sd t j | j ƒ j k S(   Nt   ignore_extra(   R3   R8   R)   t   inspectt
   getargspect   factoryt   args(   R5   R.   R9   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   is_field_ignore_extra_complaint8   s
    R   c           B   s&   e  Z d Z d „  Z e d „  ƒ Z RS(	   R)   R   t   initialt	   mandatoryt   _factoryR%   c         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   R)   R   R?   R@   RA   R%   (   t   selfR)   R   R?   R@   R<   R%   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   __init__F   s    					c         C   s]   |  j  t k rV t |  j ƒ d k rV t t |  j ƒ d ƒ } t | t ƒ rV | j Sn  |  j  S(   Ni   i    (	   RA   t   PFIELD_NO_FACTORYR2   R)   R   R   R4   R   t   create(   RB   t   typ(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyR<   N   s
    $
(   R)   R   R?   R@   RA   R%   (   R+   t
   __module__t	   __slots__RC   t   propertyR<   (    (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyR   C   s   	c         C   s
   t  d  f S(   N(   t   Truet   None(   t   _(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   <lambda>Y   t    c         C   s   |  S(   N(    (   t   x(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRM   Z   RN   c         C   s   | S(   N(    (   RL   R'   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRM   \   RN   c         C   s   | S(   N(    (   RL   R'   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRM   ]   RN   c   	      C   s¤   t  |  t t t f ƒ r- t t |  ƒ ƒ } n t t |  ƒ ƒ } | t k rc t | ƒ rc t | ƒ n | } t	 d | d | d | d | d | d | ƒ } t
 | ƒ | S(   s  
    Field specification factory for :py:class:`PRecord`.

    :param type: a type or iterable with types that are allowed for this field
    :param invariant: a function specifying an invariant that must hold for the field
    :param initial: value of field if not specified when instantiating the record
    :param mandatory: boolean specifying if the field is mandatory or not
    :param factory: function called when field is set.
    :param serializer: function that returns a serialized version of the field
    R)   R   R?   R@   R<   R%   (   R   R   t   setR   R   R   t   PFIELD_NO_INVARIANTt   callableR
   R   t   _check_field_parameters(	   R)   R   R?   R@   R<   R%   R7   t   invariant_functionR.   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyR.   _   s    *
c            s  xR ˆ  j  D]G } t | t  ƒ r
 t | t j ƒ r
 t d j t  | ƒ ƒ ƒ ‚ q
 q
 Wˆ  j t k	 r¾ t ˆ  j ƒ r¾ ˆ  j  r¾ t	 ‡  f d †  ˆ  j  Dƒ ƒ r¾ t d j t  ˆ  j ƒ ƒ ƒ ‚ n  t ˆ  j
 ƒ sÜ t d ƒ ‚ n  t ˆ  j ƒ sú t d ƒ ‚ n  t ˆ  j ƒ st d ƒ ‚ n  d  S(   Ns    Type parameter expected, not {0}c         3   s!   |  ] } t  ˆ  j | ƒ Vq d  S(   N(   R   R?   (   R   R(   (   R.   (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pys	   <genexpr>Š   s    s   Initial has invalid type {0}s   Invariant must be callables   Factory must be callables   Serializer must be callable(   R)   R   t   sixt   string_typest	   TypeErrorR&   R?   t   PFIELD_NO_INITIALRR   R*   R   R<   R%   (   R.   R(   (    (   R.   s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRS   ƒ   s    #")!R,   c           B   s   e  Z d  Z d „  Z RS(   s  
    Raised when trying to assign a value with a type that doesn't match the declared type.

    Attributes:
    source_class -- The class of the record
    field -- Field name
    expected_types  -- Types allowed for the field
    actual_type -- The non matching type
    c         O   sA   t  t |  ƒ j | | Ž  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   superR,   RC   t   source_classR.   t   expected_typesR/   (   RB   RZ   R.   R[   R/   R=   t   kwargs(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRC   ¡   s
    			(   R+   RG   t   __doc__RC   (    (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyR,   —   s   	t   PVectort   PSetc         C   s   t  |  | f } t | | ƒ S(   s=   Unpickling function for auto-generated PVec/PSet field types.(   t   _seq_field_typesR   (   t   checked_classt	   item_typet   datat   type_(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   _restore_seq_field_pickle±   s    c         C   s   d j  d „  |  Dƒ ƒ S(   s4   Convert a tuple of types to a human-readable string.RN   c         s   s$   |  ] } t  | ƒ j j ƒ  Vq d  S(   N(   R   R+   t
   capitalize(   R   RF   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pys	   <genexpr>¸   s    (   t   join(   R7   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   _types_to_names¶   s    c            sx   t  j ˆ  ˆ f ƒ } | d k	 r% | Sd ˆ  f ‡  ‡ f d †  ƒ  Y} t ˆ  } t | j ƒ | | _ | t  ˆ  ˆ f <| S(   sF   Create a subclass of the given checked class with the given item type.t   TheTypec              s    e  Z ˆ Z ‡  ‡ f d  †  Z RS(   c            s   t  ˆ  ˆ t |  ƒ f f S(   N(   Re   R   (   RB   (   Ra   Rb   (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt
   __reduce__Ã   s    (   R+   RG   t   __type__Rj   (    (   Ra   Rb   (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRi   À   s   N(   R`   R   RK   t   SEQ_FIELD_TYPE_SUFFIXESRh   t   _checked_typesR+   (   Ra   Rb   Rd   Ri   t   suffix(    (   Ra   Rb   s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   _make_seq_field_typeº   s    
c      
      sg   t  |  | ƒ ‰  | r' ‡  f d †  } n	 ˆ  j } t d | rH t ˆ  ƒ n ˆ  d | d t d | | ƒ ƒ S(   sˆ  
    Create checked field for either ``PSet`` or ``PVector``.

    :param checked_class: ``CheckedPSet`` or ``CheckedPVector``.
    :param item_type: The required type for the items in the set.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory.

    :return: A ``field`` containing a checked class.
    c            s!   |  d  k r d  Sˆ  j |  ƒ Sd  S(   N(   RK   RE   (   t   argument(   Ri   (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyR<   Û   s    R)   R<   R@   R?   (   Ro   RE   R.   t   optional_typeRJ   (   Ra   Rb   R	   R?   R<   (    (   Ri   s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   _sequence_fieldÌ   s    	c         C   s   t  t |  | | ƒ S(   sl  
    Create checked ``PSet`` field.

    :param item_type: The required type for the items in the set.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory if no value is given
        for the field.

    :return: A ``field`` containing a ``CheckedPSet`` of the given type.
    (   Rr   R   (   Rb   R	   R?   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt
   pset_fieldè   s    c         C   s   t  t |  | | ƒ S(   su  
    Create checked ``PVector`` field.

    :param item_type: The required type for the items in the vector.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory if no value is given
        for the field.

    :return: A ``field`` containing a ``CheckedPVector`` of the given type.
    (   Rr   R   (   Rb   R	   R?   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   pvector_fieldø   s    c         C   s
   t  d f S(   NRN   (   RJ   (   t   item(    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRM     RN   c         C   s   t  |  | f } t | | ƒ S(   s8   Unpickling function for auto-generated PMap field types.(   t   _pmap_field_typesR   (   t   key_typet
   value_typeRc   Rd   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   _restore_pmap_field_pickle  s    c            s   t  j ˆ  ˆ f ƒ } | d k	 r% | Sd t f ‡  ‡ f d †  ƒ  Y} d j t | j ƒ t | j ƒ ƒ | _ | t  ˆ  ˆ f <| S(   sD   Create a subclass of CheckedPMap with the given key and value types.t   TheMapc              s   e  Z ˆ  Z ˆ Z d  „  Z RS(   c         S   s   t  |  j |  j t |  ƒ f f S(   N(   Ry   t   __key_type__t   __value_type__R   (   RB   (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRj     s    (   R+   RG   R{   R|   Rj   (    (   Rw   Rx   (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyRz     s   s   {0}To{1}PMapN(	   Rv   R   RK   R    R&   Rh   t   _checked_key_typest   _checked_value_typesR+   (   Rw   Rx   Rd   Rz   (    (   Rw   Rx   s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   _make_pmap_field_type  s    c            sj   t  |  | ƒ ‰  | r' ‡  f d †  } n	 ˆ  j } t d t d ˆ  ƒ  d | rW t ˆ  ƒ n ˆ  d | d | ƒ S(   sd  
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param invariant: Pass-through to ``field``.

    :return: A ``field`` containing a ``CheckedPMap``.
    c            s!   |  d  k r d  Sˆ  j |  ƒ Sd  S(   N(   RK   RE   (   Rp   (   Rz   (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyR<   7  s    R@   R?   R)   R<   R   (   R   RE   R.   RJ   Rq   (   Rw   Rx   R	   R   R<   (    (   Rz   s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt
   pmap_field(  s    	(    (    (    (.   RU   t   pyrsistent._checked_typesR    R   R   R   R   R   R   R   R   R	   Rq   R
   R:   R   R"   R$   R1   R8   R>   t   objectR   t   PFIELD_NO_TYPERQ   RD   RX   R#   R3   R.   RS   RW   R,   Rl   R`   Re   Rh   Ro   Rr   Rs   Rt   t   _validRv   Ry   R   R€   (    (    (    s7   lib/python2.7/site-packages/pyrsistent/_field_common.pyt   <module>   sH   @													#	
							