a
    «ˆcð  ã                   @   s4   d Z ddlZddlZG dd„ dƒZG dd„ dƒZdS )z­This module generates the class hierarchy for any given Python
modules.  This can be used (among other things) to generate the
traitified VTK classes in the correct order.

é    Nc                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTreeNodea¹  Represents a node in the class tree.

    Stores information on the sub and super classes of a particular
    class.  It also stores the inheritance level of the class inside
    the inheritance tree (essentially how many levels of inheritance
    are there below this class).  This inheritance level is computed
    when the `get_level` method is called.  The `get_level` method
    works only when the parent information is complete.

    c                 C   s$   || _ |j| _g | _g | _d| _dS )zµGiven a class, create a node in the tree.

        Parameters
        ----------
        - klass : `class`

          The class which is represented as a node in the tree.

        N)ÚklassÚ__name__ÚnameÚchildrenÚparentsÚlevel)Úselfr   © r
   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tvtk/class_tree.pyÚ__init__   s
    
zTreeNode.__init__c                 C   s(   t |tƒsJ ‚|| jvr$| j |¡ dS )zAdd a parent node.N)Ú
isinstancer   r   Úappend)r	   Úparentr
   r
   r   Ú
add_parent-   s    
zTreeNode.add_parentc                 C   s(   t |tƒsJ ‚|| jvr$| j |¡ dS )zAdd a child node. N)r   r   r   r   )r	   Úchildr
   r
   r   Ú	add_child3   s    
zTreeNode.add_childc                 C   s4   | j s.| jr(tdd„ | jD ƒƒd | _ nd| _ | j S )zäReturns the inheritance level of the node (an int).  If the
        level has not been set, the method computes it.  Note however,
        that this computation will fail if the parent information is
        incorrect.

        c                 S   s   g | ]}|  ¡ ‘qS r
   )Ú	get_level)Ú.0Úxr
   r
   r   Ú
<listcomp>B   ó    z&TreeNode.get_level.<locals>.<listcomp>é   r   )r   r   Úmax©r	   r
   r
   r   r   9   s
    zTreeNode.get_levelc                    s   ‡ fdd„‰ g }ˆ | |ƒ |S )zWReturns a list of ancestor nodes from which this class has
        descended.

        c                    s&   |  | j¡ | jD ]}ˆ ||ƒ qd S ©N)Úextendr   )ÚnodeÚ	ancestorsÚp©Ú_get_ancestorsr
   r   r!   L   s    
z.TreeNode.get_ancestors.<locals>._get_ancestorsr
   )r	   r   r
   r    r   Úget_ancestorsG   s    
zTreeNode.get_ancestorsN)	r   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r"   r
   r
   r
   r   r      s   r   c                   @   sV   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	ddd„Z
ddd„ZdS )Ú	ClassTreeaû  Contains and generates all the class tree information.

    On initialization of the instance, nothing is done.  The classes
    are obtained using the list of modules (or a single module) that
    is used to initialize the instance.  One must then call the
    `create` method to generate the tree structure.  The instance of
    the class also can be treated as an iterator which iterates over
    the nodes of the tree.

    There are two ways in which the tree hierarchy is stored.  A
    dictionary mapping class names to the tree node and a tree
    represented as a list of lists containing the nodes.  The tree is
    organized based on a concept of an inheritance level.  A class
    that has no parent classes (no base classes) is said to be at
    level zero.  If a class inherits successively from 7 classes, it
    is at level 6.  An example of inheritance for a vtkFoo class is
    given below:

      vtkFoo -> vtkBar -> vtkObject -> vtkObjectBase

    Here, vtkObjectBase has an inheritance level of 0 and vtkFoo a
    level of 3.  One can traverse the tree by using the level as an
    index and find all the classes at a particular level.

    Here is some example usage of this class::

        >>> import vtk
        >>> t = ClassTree(vtk)
        >>> t.create()
        >>> print(t.get_node('vtkObject').name)
        vtkObject
        >>> print(t.get_node('vtkObject').parents[0].name)
        vtkObjectBase
        >>> print(len(t.tree[0]))
        1
        >>> t.tree[0][0].name
        vtkObjectBase

    c                 C   s,   i | _ g g| _t|dƒs"|g| _n|| _dS )a3  Initialize the instance with the given modules.

        Parameters
        ----------

        - modules : `sequence` of modules or a module

          This is either a single module or a sequence of modules.
          The instance uses these list of modules to generate the
          class tree.

        Ú__iter__N)ÚnodesÚtreeÚhasattrÚmodules)r	   r+   r
   r
   r   r   ~   s
    

zClassTree.__init__c                 C   s   t | j ¡ ƒS r   )Úiterr(   Úvaluesr   r
   r
   r   r'   ’   s    zClassTree.__iter__c                 C   sL   | j |jdd�}|jD ]0}| j|dd�}| |¡ | |¡ |  |¡ qdS )z5Does the hard work of generating the class hierarchy.r   )ÚcreateN)Úget_noder   Ú	__bases__Úget_node_from_classr   r   Ú_generate_hierarchy)r	   r   r   ÚbaseZ	base_noder
   r
   r   r2   •   s    


zClassTree._generate_hierarchyc                 C   st   d}| j D ]}t||ƒr
t||ƒ  S q
tt|ƒr<tt|ƒ}|spz| j| j}W n tyn   td| ƒ‚Y n0 |S )z:Given a class name in the given modules returns the class.NzCannot find class of name %s)r+   r*   ÚgetattrÚbuiltinsr(   r   ÚKeyError)r	   r   r   Úmr
   r
   r   Ú	get_classž   s    



zClassTree.get_classc                 C   s*   |j }|| jvr&t|ƒ}|| j|< |S dS )z"Create a node for the given class.N)r   r(   r   )r	   r   r   r   r
   r
   r   Úadd_node­   s
    

zClassTree.add_noder   c                 C   s,   || j v r| j | S |r(|  |  |¡¡S dS )aU  Get a node of the given name.

        Parameters
        ----------

        - name : `str`

          Name of the node to get.

        - create : `boolean`

          If True, a new node will be added if no node of the given
          name is available.  Defaults to False.

        Returns
        -------

        - `TreeNode`

        N)r(   r9   r8   )r	   r   r.   r
   r
   r   r/   µ   s    

zClassTree.get_nodec                 C   s,   |j }|| jv r| j| S |r(|  |¡S dS )aX  Get a node of the given class.

        Parameters
        ----------

        - cls : `class`

          Class of the node to get.

        - create : `boolean`

          If True, a new node will be added if no node of the given
          name is available.  Defaults to False.

        Returns
        -------

        - `TreeNode`

        N)r   r(   r9   )r	   Úclsr.   r   r
   r
   r   r1   Ï   s
    

zClassTree.get_node_from_classNc                 C   sÂ   |du r&g }| j D ]}| t|ƒ¡ q|D ]8}d|v r8q*|  |¡}|tur*|r*t|dƒr*|  |¡ q*| D ]8}| ¡ }t| j	ƒ|kr�| j	 
g ¡ qt| j	|  
|¡ qh| j	D ]}|jdd„ d� q¨dS )aT  This method generates the class tree given an optional list
        of class names.

        Parameters
        ----------

        - class_names - `list` of `str`

          An optional list of names of the classes to generate the
          tree for.  Defaults to None where the class list is computed
          from the modules.

        NÚ.r0   c                 S   s   | j S r   )r   )r   r
   r
   r   Ú<lambda>  r   z"ClassTree.create.<locals>.<lambda>)Úkey)r+   r   Údirr8   ÚNotImplementedr*   r2   r   Úlenr)   r   Úsort)r	   Zclass_namesr7   r   r   r   Údr(   r
   r
   r   r.   ê   s*    

ÿÿ
zClassTree.create)r   )r   )N)r   r#   r$   r%   r   r'   r2   r8   r9   r/   r1   r.   r
   r
   r
   r   r&   U   s   (	

r&   )r%   Úsysr5   r   r&   r
   r
   r
   r   Ú<module>   s   E