
 X                 @   sB   d  Z  d d l Z d d l m Z m Z Gd d   d e  Z d S)zT
A class for storing a tree graph. Primarily used for filter constructs in the
ORM.
    N)	force_str
force_textc               @   s   e  Z d  Z d Z d Z d d d d d  Z e d d d 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 d d  Z d d   Z d S)Nodez
    A single internal node in the tree graph. A Node should be viewed as a
    connection (the root) with the children being either leaf nodes or other
    Node instances.
    DEFAULTNFc             C   s>   | r | d d  n g  |  _  | p+ |  j |  _ | |  _ d S)zd
        Constructs a new Node. If no connector is given, the default will be
        used.
        N)childrendefault	connectornegated)selfr   r   r	    r   ?/home/ubuntu/projects/ifolica/build/django/django/utils/tree.py__init__   s    zNode.__init__c             C   s   t  | | |  } |  | _ | S)a  
        This is called to create a new instance of this class when we need new
        Nodes (or subclasses) in the internal code in this class. Normally, it
        just shadows __init__(). However, subclasses with an __init__ signature
        that is not an extension of Node.__init__ might need to implement this
        method to allow a Node to create a new instance of them (if they have
        any extra setting up to do).
        )r   	__class__)clsr   r   r	   objr   r   r   _new_instance    s    
	zNode._new_instancec             C   sE   |  j  r d n d } t | |  j d j d d   |  j D  f  S)Nz(NOT (%s: %s))z(%s: %s)z, c             s   s   |  ] } t  |  Vq d  S)N)r   ).0cr   r   r   	<genexpr>0   s    zNode.__str__.<locals>.<genexpr>)r	   r   r   joinr   )r
   templater   r   r   __str__.   s    zNode.__str__c             C   s   t  d  |  j j |  f S)Nz<%s: %s>)strr   __name__)r
   r   r   r   __repr__2   s    zNode.__repr__c             C   sC   t  d |  j d |  j  } |  j | _ t j |  j |  | _ | S)z9
        Utility method used by copy.deepcopy().
        r   r	   )r   r   r	   r   copydeepcopyr   )r
   Zmemodictr   r   r   r   __deepcopy__5   s    zNode.__deepcopy__c             C   s   t  |  j  S)zF
        The size of a node if the number of children it has.
        )lenr   )r
   r   r   r   __len__>   s    zNode.__len__c             C   s   t  |  j  S)z*
        For truth value testing.
        )boolr   )r
   r   r   r   __bool__D   s    zNode.__bool__c             C   s   t  |   j |   S)N)typer!   )r
   r   r   r   __nonzero__J   s    zNode.__nonzero__c             C   s   | |  j  k S)zM
        Returns True is 'other' is a direct child of this instance.
        )r   )r
   otherr   r   r   __contains__M   s    zNode.__contains__Tc             C   s   | |  j  k r | S| s- |  j  j |  | S|  j | k r t | t  r | j r | j | k sv t |  d k r |  j  j | j   |  S|  j  j |  | Sn: |  j |  j  |  j |  j  } | |  _ | | g |  _  | Sd S)a>  
        Combines this tree and the data represented by data using the
        connector conn_type. The combine is done by squashing the node other
        away if possible.

        This tree (self) will never be pushed to a child node of the
        combined tree, nor will the connector or negated properties change.

        The function returns a node which can be used in place of data
        regardless if the node other got squashed or not.

        If `squash` is False the data is prepared and added as a child to
        this tree without further logic.
           N)	r   appendr   
isinstancer   r	   r   extendr   )r
   dataZ	conn_typeZsquashr   r   r   r   addS   s"    !	zNode.addc             C   s   |  j  |  _  d S)z9
        Negate the sense of the root connector.
        N)r	   )r
   r   r   r   negate   s    zNode.negate)r   
__module____qualname____doc__r   r   classmethodr   r   r   r   r   r!   r#   r%   r+   r,   r   r   r   r   r      s   	,r   )r/   r   Zdjango.utils.encodingr   r   objectr   r   r   r   r   <module>   s   