ó
ù¢TQc           @   s,   d  Z  d d l Z d e f d „  ƒ  YZ d S(   sT   
A class for storing a tree graph. Primarily used for filter constructs in the
ORM.
iÿÿÿÿNt   Nodec           B   sž   e  Z d  Z d Z d d e d „ Z d d e d „ Z e e ƒ Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   s±   
    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.
    t   DEFAULTc         C   s>   | r | p g  |  _  | p" |  j |  _ g  |  _ | |  _ d S(   s  
        Constructs a new Node. If no connector is given, the default will be
        used.

        Warning: You probably don't want to pass in the 'negated' parameter. It
        is NOT the same as constructing a node and calling negate() on the
        result.
        N(   t   childrent   defaultt	   connectort   subtree_parentst   negated(   t   selfR   R   R   (    (    s*   ../Django//lib/python/django/utils/tree.pyt   __init__   s    		c         C   s   t  | | | ƒ } |  | _ | S(   s¹  
        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    t	   __class__(   t   clsR   R   R   t   obj(    (    s*   ../Django//lib/python/django/utils/tree.pyt   _new_instance"   s    		c         C   su   |  j  r? d |  j d j g  |  j D] } t | ƒ ^ q" ƒ f Sd |  j d j g  |  j D] } t | ƒ ^ qX ƒ f S(   Ns   (NOT (%s: %s))s   , s   (%s: %s)(   R   R   t   joinR   t   str(   R   t   c(    (    s*   ../Django//lib/python/django/utils/tree.pyt   __str__0   s
    	$c         C   s[   t  d |  j d |  j ƒ } |  j | _ t j |  j | ƒ | _ t j |  j | ƒ | _ | S(   s9   
        Utility method used by copy.deepcopy().
        R   R   (   R    R   R   R	   t   copyt   deepcopyR   R   (   R   t   memodictR   (    (    s*   ../Django//lib/python/django/utils/tree.pyt   __deepcopy__7   s
    c         C   s   t  |  j ƒ S(   sF   
        The size of a node if the number of children it has.
        (   t   lenR   (   R   (    (    s*   ../Django//lib/python/django/utils/tree.pyt   __len__A   s    c         C   s   t  |  j ƒ S(   s*   
        For truth value testing.
        (   t   boolR   (   R   (    (    s*   ../Django//lib/python/django/utils/tree.pyt   __bool__G   s    c         C   s   t  |  ƒ j |  ƒ S(   N(   t   typeR   (   R   (    (    s*   ../Django//lib/python/django/utils/tree.pyt   __nonzero__M   s    c         C   s   | |  j  k S(   sM   
        Returns True is 'other' is a direct child of this instance.
        (   R   (   R   t   other(    (    s*   ../Django//lib/python/django/utils/tree.pyt   __contains__P   s    c         C   så   | |  j  k r" | |  j k r" d St |  j  ƒ d k  rC | |  _ n  |  j | k r« t | t ƒ r˜ | j | k s‚ t | ƒ d k r˜ |  j  j | j  ƒ qá |  j  j | ƒ n6 |  j |  j  |  j |  j ƒ } | |  _ | | g |  _  d S(   s6  
        Adds a new node to the tree. If the conn_type is the same as the root's
        current connector type, the node is added to the first level.
        Otherwise, the whole tree is pushed down one level and a new root
        connector is created, connecting the existing tree and the new node.
        Ni   i   (	   R   R   R   t
   isinstanceR    t   extendt   appendR   R   (   R   t   nodet	   conn_typeR   (    (    s*   ../Django//lib/python/django/utils/tree.pyt   addV   s    	c         C   s5   |  j  |  j |  j |  j ƒ g |  _ |  j |  _ d S(   s­  
        Negate the sense of the root connector. This reorganises the children
        so that the current node has a single child: a negated node containing
        all the previous children. This slightly odd construction makes adding
        new children behave more intuitively.

        Interpreting the meaning of this negate is up to client code. This
        method is useful for implementing "not" arrangements.
        N(   R   R   R   R   R   (   R   (    (    s*   ../Django//lib/python/django/utils/tree.pyt   negatem   s    
c         C   s³   t  |  j ƒ d k r! | |  _ nH |  j | k ri |  j |  j |  j |  j ƒ g |  _ | |  _ t |  _ n  |  j j |  j |  j |  j |  j ƒ ƒ |  j	 |  _ t |  _ g  |  _ d S(   sÃ   
        Sets up internal state so that new nodes are added to a subtree of the
        current node. The conn_type specifies how the sub-tree is joined to the
        existing children.
        i   N(
   R   R   R   R   R   t   FalseR   R   R	   R   (   R   R!   (    (    s*   ../Django//lib/python/django/utils/tree.pyt   start_subtree{   s    		c         C   s_   |  j  j ƒ  } |  j |  j |  j ƒ } | j |  _ | j |  _ | j |  _ |  j j | ƒ d S(   sÔ   
        Closes off the most recently unmatched start_subtree() call.

        This puts the current state into a node of the parent tree and returns
        the current instances state to be the parent.
        N(   R   t   popR	   R   R   R   R   (   R   R   R    (    (    s*   ../Django//lib/python/django/utils/tree.pyt   end_subtree   s    N(   t   __name__t
   __module__t   __doc__R   t   NoneR$   R   R   t   classmethodR   R   R   R   R   R   R"   R#   R%   R'   (    (    (    s*   ../Django//lib/python/django/utils/tree.pyR       s   		
							(   R*   R   t   objectR    (    (    (    s*   ../Django//lib/python/django/utils/tree.pyt   <module>   s   