
^Q\                  @   sf   d  d l  m Z d  d l m Z m Z d  d l m Z d  d l m Z m	 Z	 Gd d   d e
  Z d S)    )	Container)integer_typesstring_types)DynamicError)Fieldset
MultiFieldc               @   s   e  Z d  Z e e e f Z d d   Z d d   Z d d   Z	 d d   Z
 d	 d
   Z d d   Z d d   Z d d   Z d S)LayoutSlicec             C   sA   | |  _  t | t  r4 t | | d d  |  _ n	 | |  _ d  S)N   )layout
isinstancer   slice)selfr
   key r   L/var/www/dbchiro/venv/build/django-crispy-forms/crispy_forms/layout_slice.py__init__   s    	zLayoutSlice.__init__c             O   s   | r` t  | t  r$ t |  } n	 | f } | |  j k rI | | } n
 | | } | | |   St  | t  r| | | |   S| | |  Sd S)z|
        Returns a layout object of type `LayoutClass` with `args` and `kwargs` that
        wraps `fields` inside.
        N)r   listtuple
args_first)r   LayoutClassfieldsargskwargs	argumentsr   r   r   wrapped_object   s    	
zLayoutSlice.wrapped_objectc             C   s&  t  |  j t  rT xt |  j j t |  j j     D] } | |  j |  q7 Wn t  |  j t  r"x |  j D] } | d } t |  d k r | |  j | d  qp |  j j | d } x% | d d  D] } | j | } q Wy | | | d  Wqp t k
 rt	 d   Yqp Xqp Wn  d S)z
        Iterates over layout objects pointed in `self.slice` executing `function` on them.
        It passes `function` penultimate layout object and the position where to find last one
        r   r	   zqTrying to wrap a field within an already wrapped field,                             recheck your filter or layoutNr   r   )
r   r   rangeindiceslenr
   r   r   
IndexErrorr   )r   functionipointerpositionlayout_objectr   r   r   pre_map*   s     +
zLayoutSlice.pre_mapc                s,        f d d   }  j  |  d S)z
        Wraps every layout object pointed in `self.slice` under a `LayoutClass` instance with
        `args` and `kwargs` passed.
        c                s*    j    |  j |    |  j | <d  S)N)r   r   )r$   j)r   r   r   r   r   r   wrap_objectM   s    z%LayoutSlice.wrap.<locals>.wrap_objectN)r%   )r   r   r   r   r'   r   )r   r   r   r   r   wrapH   s    zLayoutSlice.wrapc                s,        f d d   }  j  |  d S)z
        Wraps every layout object pointed in `self.slice` under a `LayoutClass` instance with
        `args` and `kwargs` passed, unless layout object's parent is already a subclass of
        `LayoutClass`.
        c                s<   t  |     s8  j   |  j |    |  j | <n  d  S)N)r   r   r   )r$   r&   )r   r   r   r   r   r   wrap_object_onceZ   s    z/LayoutSlice.wrap_once.<locals>.wrap_object_onceN)r%   )r   r   r   r   r)   r   )r   r   r   r   r   	wrap_onceT   s    zLayoutSlice.wrap_oncec             O   s   t  |  j t  r |  j j d k	 r0 |  j j n d } |  j | |  j j |  j | |  |  j j | <xr t t |  j j t	 |  j j      D]" } | | k r |  j j | =q q Wn! t  |  j t
  r t d   n  d S)z
        Wraps all layout objects pointed in `self.slice` together under a `LayoutClass`
        instance with `args` and `kwargs` passed.
        Nr   z=wrap_together doesn't work with filter, only with [] operator)r   r   startr   r
   r   reversedr   r   r   r   r   )r   r   r   r   r+   r!   r   r   r   wrap_togetherb   s    $)1zLayoutSlice.wrap_togetherc             C   s  t  |  j t  rX x t |  j j t |  j j     D] } | |  j j |  q7 Wn t  |  j t  rx |  j D] } | d } |  j j | d } x+ | d d  D] } | } | j | } q W| j d k r t  | t	  r | |  qt | |  qt Wn  d S)z
        Iterates over layout objects pointed in `self.slice` executing `function` on them
        It passes `function` last layout object
        r   r	   Nupdate_attrs)
r   r   r   r   r   r
   r   r   __name__r   )r   r    r!   r"   r#   r$   Zprevious_layout_objectr   r   r   mapv   s    +
zLayoutSlice.mapc                s#     f d d   } |  j  |  d S)z`
        Updates attributes of every layout object pointed in `self.slice` using kwargs
        c                s&   t  |  d  r" |  j j    n  d  S)Nattrs)hasattrr1   update)r$   )r   r   r   r.      s    z3LayoutSlice.update_attributes.<locals>.update_attrsN)r0   )r   r   r.   r   )r   r   update_attributes   s    zLayoutSlice.update_attributesN)r/   
__module____qualname__r   r   r   r   r   r   r%   r(   r*   r-   r0   r4   r   r   r   r   r      s   r   N)Zcrispy_forms.bootstrapr   Zcrispy_forms.compatibilityr   r   Zcrispy_forms.exceptionsr   Zcrispy_forms.layoutr   r   objectr   r   r   r   r   <module>   s   