a
    «ˆcŒx  ã                   @   sZ   d Z ddlZddlZddlZddlZddlmZ ddlmZ	 ddl
mZ G dd„ dƒZdS )	zkThis module parses the VTK methods, obtains the argument and return
type information, and organizes them.

é    Né   )Ú
class_tree)Ú
vtk_module)Úis_version_9c                   @   s°   e Zd ZdZd*dd„Z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e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&„ Zd'd(„ Zd)S ),ÚVTKMethodParsera¹  This class provides useful methods for parsing methods of a VTK
    class or instance.

    The class allows one to categorize the methods of the VTK class
    and also obtain the method signatures in a form that is easy to
    use.  When the `parse` method is called, it in turn calls the
    `_organize_methods` method.  This method organizes the VTK methods
    into different instance variables described in the following.
    `self.toggle_meths` contains a dictionary of all the boolean
    methods of the form <Value>On/Off.  The dictionary keys are
    strings with the <Value>'s and the value of each item is the
    default value (0/1) of the item (the example below will clarify
    this).  `self.state_meths` contains a dictionary which collects
    the Set<Prop>To<Value> type of methods.  The key is the <Prop> and
    the value is a list containing the different string <Value>'s and
    their corresponding mapped value.  The first value in these is the
    default value of the <Prop>.  `self.get_set_meths` will contain a
    dictionary which collects all the methods of the form
    Set/Get<Prop> that are not already specified in
    `self.toggle_meths` or `self.state_meths`.  The default value of
    the Get<Prop> is stored.  If the value accepted by the method has
    a range (via the methods `Get<Prop>MinValue` and
    `Get<Prop>MaxValue`), then that range is computed and stored.
    `self.get_meths` stores the methods that are of the form
    `Get<Prop>`.  `self.other_meths` stores the remaining methods.
    The parsing is quite fast.  Parsing every class in the VTK API
    takes a couple of seconds (on a Pentium III @ 450Mhz).

    Here is an example::

       >>> import vtk
       >>> p = VTKMethodParser()
       >>> p.parse(vtk.vtkProperty)
       >>> print(p.get_toggle_methods())
       {'EdgeVisibility': 0, 'BackfaceCulling': 0, 'FrontfaceCulling': 0}
       >>> print(p.get_state_methods()['Representation'])
       [['Surface', 2], ['Points', 0], ['Surface', 2], ['Wireframe', 1]]
       >>> print(p.get_get_set_methods()['Opacity'])
       (1.0, (0.0, 1.0))
       >>> print(p.get_get_methods())
       ['GetClassName']
       >>> print(p.get_other_methods()[:3])
       ['BackfaceRender', 'DeepCopy', 'IsA']


    The class also provides a method called `get_method_signature`
    that obtains the Python method signature given the VTK method
    object.  Here is an example::

       >>> import vtk
       >>> o = vtk.vtkProperty
       >>> print(VTKMethodParser.get_method_signature(o.GetClassName))
       [(['string'], None)]
       >>> print(VTKMethodParser.get_method_signature(o.GetColor)[0])
       ([('float', 'float', 'float')], None)
       >>> print(VTKMethodParser.get_method_signature(o.GetColor)[1])
       ([None], (('float', 'float', 'float'),))

    The `get_method_signature` is fairly efficient and obtaining the
    signature for every method in every class in the VTK API takes
    around 6 seconds (on a Pentium III @ 450Mhz).

    Tc                 C   sP   |rt  t¡| _| j ¡  nd| _t d¡| _t 	dd¡ 
¡ dv | _|  ¡  dS )a  Initializes the object.

        Parameters
        ----------

        - use_tree : `bool`

          If True (default), use a ClassTree instance to obtain a
          concrete subclass for an abstract base class.  This is used
          only to find the range and default values for some of the
          methods.  If False, no ClassTree instance is created.

          This is optional because, creating a ClassTree is expensive.
          The parser functionality can be very useful even without the
          use of a ClassTree.  For example, if one wants to save the
          state of a VTK object one only needs to know the names of
          the methods and not their default values, ranges etc.  In
          that case using a parser should be cheap.

        Nz
To[A-Z0-9]ZVTK_PARSER_VERBOSEÚ )Ú1Útrue)r   Z	ClassTreeÚvtkÚ_treeÚcreateÚreÚcompileÚ_state_patnÚosÚgetenvÚlowerÚ_verboseÚ_initialize)ÚselfZuse_tree© r   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tvtk/vtk_parser.pyÚ__init__U   s    ÿzVTKMethodParser.__init__c                 C   sb   t |dƒs|j}n|}|  |¡}|rBtj ¡ }|jdkrBtj ¡  |  ||¡ |r^tj 	|¡ dS )a  Parse the methods for a given VTK object/class.

        Given a VTK class or object, this method parses the methods
        and orgaizes them into useful categories.  The categories and
        their usage is documented in the documentation for the class.

        Parameters
        ----------

        - obj : VTK class or instance

        - no_warn : `bool` (default: True)

          If True (default), it suppresses any warnings generated by
          the VTK object when parsing the methods.  This is safe to
          use.

        Ú	__bases__Ú	vtkObjectN)
ÚhasattrÚ	__class__Úget_methodsr
   r   ZGetGlobalWarningDisplayÚ__name__ZGlobalWarningDisplayOffÚ_organize_methodsZSetGlobalWarningDisplay)r   ÚobjZno_warnÚklassÚmethodsÚwarnr   r   r   Úparse}   s    




zVTKMethodParser.parsec                 C   sF   i }t |jƒdkr:|jd }t|ƒ}| |¡}| |¡ qt| ¡ ƒS )z/Returns all the methods of the classes parents.r   )Úlenr   ÚdirÚfromkeysÚupdateÚlistÚkeys)r   r!   r"   ÚmethsÚdr   r   r   Ú_get_parent_methods£   s    

z#VTKMethodParser._get_parent_methodsc                 C   sn  t |ƒdd… }t|dƒr0|jD ]}| |¡ q |  |¡}ddg}t|dƒrfd|vrfd|v rf| d¡ |D ]Ú}d}| d¡r’d|dd…  }d	}n| d¡r°d|dd…  }d	}|rj||v rj||v sÌ||v rjd	}t|d
ƒ�r|jd }	t||ƒt|	|dƒu �rt||ƒt|	|dƒu �rd}|rj| 	d|dd…  d|dd…  g¡ qj|dd… D ]~}| d¡�rR|dd… }
d|
 d|
  }}d|
 d }|
d }|D ]4}||v �s°||k�r˜| |¡ | |¡  �qR�q˜�qRd|v �rôd|v �rô| 	ddg¡ d|v �rd|v �r| 	ddg¡ d|v �r8d|v �r8| 	g d¢¡ |dd… D ]$}||v �rD||v�rD| |¡ �qD|S )z8Returns all the relevant methods of the given VTK class.NÚ__members__ZGetInputZSetInputFÚGetÚSeté   TÚmror   ÚToÚOffZGetViewPropZGetPropZSetPropZGetViewPropsZGetPropsZSetPropsÚGetScaledTextZGetTextScaleMode)r5   ZSetScaledTextZScaledTextOnZScaledTextOff)
r&   r   r.   Úremover-   ÚappendÚ
startswithr   ÚgetattrÚextend)r   r!   r"   ÚmÚignoreÚskipÚcheckÚm1Zskip_methodZbase_clsÚbaseZmgÚmsZm_stZm_tÚmethodr   r   r   r   ­   sh    



ÿþ



ÿ(

zVTKMethodParser.get_methodsc                 C   s   | j S )zhReturns a dictionary of the parsed <Value>On/Off methods
        along with the default value.

        )Útoggle_meths©r   r   r   r   Úget_toggle_methodsþ   s    z"VTKMethodParser.get_toggle_methodsc                 C   s   | j S )a  Returns a dict of the parsed Set<Prop>To<Value>.

        The keys are the <Prop> string with a list of the different
        <Value> strings along with their corresponding value (if
        obtainable).  The first value is the default value of the
        state.

        )Ústate_methsrD   r   r   r   Úget_state_methods  s    	z!VTKMethodParser.get_state_methodsc                 C   s   | j S )aO  Returns a dict of the parsed Get/Set<Value> methods.

        The keys of the dict are the <Value> strings and contain a
        two-tuple containing the default value (or None if it is not
        obtainable for some reason) and a pair of numbers specifying
        an acceptable range of values (or None if not obtainable).

        )Úget_set_methsrD   r   r   r   Úget_get_set_methods  s    	z#VTKMethodParser.get_get_set_methodsc                 C   s   | j S )z{Return a list of parsed Get<Value> methods.

        All of these methods do NOT have a corresponding Set<Value>.

        )Ú	get_methsrD   r   r   r   Úget_get_methods  s    zVTKMethodParser.get_get_methodsc                 C   s   | j S )zOReturn list of all other methods, that are not
        categorizable.

        )Úother_methsrD   r   r   r   Úget_other_methods#  s    z!VTKMethodParser.get_other_methodsc              	      s   ˆj }|du rdS |d| d¡… }| d¡ ‰g }g }d}d}d}t d¡}| d¡D ]Š}| |¡r~d}d}| | ¡ ¡ qX| d	¡r¨d}d}| | ¡ ¡ |d
7 }qX|rÂ|| | ¡  ||< qX|rX||d
  | ¡  ||d
 < qX‡‡fdd„‰ ‡ fdd„|D ƒ}dd„ |D ƒ}t d¡}	t	|ƒ}
g }t
|
ƒD �]È\}}| d¡}dd„ |D ƒ}t|ƒd
k�rnd|d g}n$|d
 dk�rŠd|d g}n| ¡  |\}}|d
d… }|�s°d}|�rÌ|d dv �rÌ|d }d}dddddœ}|du�rÀ|�rÀ| d¡d
 }|| d
d…  d¡}|dk�rÀg }t|ƒ|k�rÀt
| d¡ƒD ]x\}}t| ¡ ƒdk�r¨||  ¡ }d|v �r†||v �r†| || ¡ n d|v �rœ| d¡ n
| |¡ n
| |¡ �q<d |¡}|du�rt ddd „ |¡}t d!d"d „ |¡}t d#d$d „ |¡}t d%d&d „ |¡}|du�r6| d'¡�r6d(|v �r6d)}|d*k�rDd}|du�r|d+|v �rf| d+d)¡}nd,|v �r|| d,d)¡}|d-k�rŠd}zH|�r¢t|	 d.|¡ƒ}|�rÐt|	 d.|¡ƒ}t|ƒtd*ƒk�rÐ|g}W n t�yæ   Y n0 | |g|f¡ �q0|S )/a^  Returns information on the Python method signature given
        the VTK method.

        The doc string of the given method object to get the method
        signature.  The method returns a list of tuples, each of which
        contains 2 items, the first is a list representing the return
        value the second represents the arguments to be passed to the
        function.  If the method supports different return values and
        arguments, this function returns all of their signatures.

        Parameters
        ----------

        - method : `method`

          A VTK method object.

        Nz

úV.Fr   z(V\.)|(\w+\()Ú
TzC++:r   c                    s<   ˆr&|   ˆ jd¡} |   dd¡  dd¡S |   dˆ j d¡S d S )Nr   z(self, ú(z(selfrN   )Úreplacer   )Úx)rB   Únew_sigr   r   Ú_processZ  s    z6VTKMethodParser.get_method_signature.<locals>._processc                    s   g | ]}ˆ |ƒ‘qS r   r   ©Ú.0rR   )rT   r   r   Ú
<listcomp>a  ó    z8VTKMethodParser.get_method_signature.<locals>.<listcomp>c                 S   s   g | ]}||  d ¡d… ‘qS )rP   N)ÚfindrU   r   r   r   rW   b  rX   z\bz->c                 S   s   g | ]}|  ¡ ‘qS r   )Ústrip)rV   Úyr   r   r   rW   m  rX   ÚNoneéÿÿÿÿ)ú)ú]ú,ÚintÚlong)zunsigned intzunsigned charzunsigned longzunsigned shortZunsignedÚvoidÚstringz, z\w+:c                 S   s   dS ©Nr   r   ©Úmor   r   r   Ú<lambda>ž  rX   z6VTKMethodParser.get_method_signature.<locals>.<lambda>z\bstr\bc                 S   s   dS )Nrd   r   rf   r   r   r   rh      rX   z=[\-e0-9.]+c                 S   s   dS re   r   rf   r   r   r   rh   ¢  rX   z\bCallback\bc                 S   s   dS )NÚfunctionr   rf   r   r   r   rh   ¤  rX   rP   z...ÚtupleÚstrz[float, ...]z(float, ...)z(, )ú")Ú__doc__rY   r8   r   r   ÚsplitÚmatchr7   rZ   r)   Ú	enumerater%   ÚreverseÚcountÚjoinÚsubrQ   ÚevalÚtypeÚSyntaxError)rB   ÚdocÚsigZc_sigZin_sigZin_c_sigÚcounterZ
py_sig_patÚlineÚpatÚtmpZsig_idxÚirR   ÚretÚargZn_argZarg_mapZcargÚargsÚidxZcarg_valr   )rT   rB   rS   r   Úget_method_signature*  s¼    





ÿ

ÿ


 





z$VTKMethodParser.get_method_signaturec                 C   s   | j S )z1Return the ClassTree instance used by this class.)r   rD   r   r   r   Úget_treeÄ  s    zVTKMethodParser.get_treec                 C   s"   i | _ i | _i | _g | _g | _dS )z"Initializes the method categories.N)rC   rF   rH   rJ   rL   rD   r   r   r   r   Ì  s
    zVTKMethodParser._initializec                    s\   |   ¡  |dd… }|  ˆ |¡}|  ˆ |¡}|  ˆ |¡}|  ˆ |¡}‡ fdd„|D ƒ| _dS )zöOrganizes the given methods of a VTK class into different
        categories.

        Parameters
        ----------

        - klass : A VTK class

        - methods : `list` of `str`

          A list of the methods to be categorized.

        Nc                    s$   g | ]}t tˆ |ƒtjjƒr|‘qS r   )Ú
isinstancer9   ÚcollectionsÚabcÚCallablerU   ©r!   r   r   rW   í  s   ÿz5VTKMethodParser._organize_methods.<locals>.<listcomp>)r   Ú_find_toggle_methodsÚ_find_state_methodsÚ_find_get_set_methodsÚ_find_get_methodsrL   )r   r!   r"   r+   r   r‰   r   r   Ù  s    
ÿz!VTKMethodParser._organize_methodsc                 C   s&   z|  |¡ W n ty    Y n0 d S )N)r6   Ú
ValueError)r   r+   rB   r   r   r   Ú_remove_methodò  s    zVTKMethodParser._remove_methodc           
   
   C   s6  |dd… }| j }|j}g d¢}|dd… D ]¦}|dkrL|dd… |v rLq,n|dd… dkr^q,|dd… dkr,|dd… }|d |v r,d| |v r,d||< | |¡ | |d ¡ |  |d	| ¡ |  |d| ¡ q,|�r2|  |¡}	|	�r2|D ]B}zt|	d
| ƒƒ ||< W qî ttf�y.   t|j|ƒ Y qî0 qî|S )z€Find/store methods of the form <Value>{On,Off} in the given
        `methods`.  Returns the remaining list of methods.

        N)ZCopyVectorsZCopyTensorsZCopyTCoordsZCopyScalarsZCopyNormalsZCopyGlobalIdsZCopyPedigreeIdsZvtkDataSetAttributeséþÿÿÿZAlphaBitPlanesZOnr4   r/   r0   úGet%s)	rC   r   r6   r�   Ú_get_instancer9   Ú	TypeErrorÚAttributeErrorÚprint)
r   r!   r"   r+   ÚtmÚ
klass_nameZproblem_methodsrB   Úkeyr    r   r   r   rŠ   ø  s8    ÿ

z$VTKMethodParser._find_toggle_methodsc              
   C   s  g d¢}|dd… }| j }|dd… D �]$}||vr&|dd… dkr&| j |¡}|r&d|dd…  |vr&|d| ¡ … }d| |v r&|| ¡ d d… }	| |¡ ||v r¾||  |	dg¡ q&|	dgg||< | d| ¡ |  |d| ¡ d| d |v �r | d| d ¡ | d| d ¡ z| d| d	 ¡ W q& t�yJ   Y q&0 q&|�r|  |¡}
|j	}|
�r| 
d
¡�s| ¡ D ]–\}}t|
d| ƒƒ }|dd… D ]n}zt|
d||d f ƒƒ  W n t�yÖ   Y n80 t|
d| ƒƒ }	|	|d< |	|k�r¢| d|d |	g¡ �q¢�q~|S )zÙFind/store methods of the form Set<Prop>To<Value> in the
        given `methods`.  Returns the remaining list of methods.  The
        method also computes the mapped value of the different
        <Values>.

        )ZSetUpdateExtentToWholeExtentZSetDataExtentToWholeExtentZSetOutputSpacingToDefaultZSetOutputOriginToDefaultZSetOutputExtentToDefaultNr1   r0   r/   é   ÚMaxValueÚMinValueZAsStringZViewerr‘   z	Set%sTo%sr   r   )rF   r   ÚsearchÚstartr6   r7   r�   rŽ   r’   r   ÚendswithÚitemsr9   r“   Úinsert)r   r!   r"   r<   r+   ÚsmrB   ro   r˜   Úvalr    r—   ÚvaluesÚdefaultrR   r   r   r   r‹     sN    


z#VTKMethodParser._find_state_methodsc              	   C   sb  |dd… }| j }|j}|dd… D �]”}|dd… dkr>q$q$|dkrJq$q$|dkrf|dd… dkrfq$q$tƒ sè|dkr„|dd… dks$|dkrœ|dd… d	ks$|d
kr´|dd… dv s$|dkrÌ|dd… dks$|dkrè|dd… dkrèq$q$|dk�r|dd… dk�rq$q$|dk�r(|dd… dk�r(q$q$|dv �r6q$q$d|dd…  |v r$|dd… }| d| ¡ | d| ¡ d| d |v �r²| d| d ¡ | d| d ¡ d||< q$d||< q$|�r^| j�rØtd|› �ƒ |  |¡}|�r8| ¡ D �]@\}}	tƒ �s&|dv �s |dk�r&|dk�r&d}
nÆtƒ �sP|dk�rP|dk�rPd||< �qðnœtƒ �r |dk�rl|dk�sš|dk�r€|dk�sš|d k�r”|d!k�sšd"�r d}
nLz2| j�rÀtd#|› d$|› d%�ƒ t|d&| ƒƒ }
W n t	�yê   d}
Y n0 |	�r$t|d'| ƒƒ }t|d(| ƒƒ }|
||ff||< n|
df||< �qð~n&| ¡ D ]\}}	|	dk�r@d||< �q@|S ))a°  Find/store methods of the form {Get,Set}Prop in the given
        `methods` and returns the remaining list of methods.

        Note that it makes sense to call this *after*
        `_find_state_methods` is called in order to avoid incorrect
        duplication.  This method also computes the default value and
        the ranges of the arguments (when possible) by using the
        Get<Prop>{MaxValue,MinValue} methods.

        Nr1   r0   ZvtkPropZAllocatedRenderTimeZvtkGenericAttributeCollectionZAttributesToInterpolateZvtkOverlappingAMRZOriginZvtkOrientationMarkerWidget)ZOutlineColorZViewportZvtkImageDataGeometryFilterZExtentZvtkVolumeMapperZCroppingRegionPlanesZvtkContextMouseEventZ
InteractorZvtkPiecewisePointHandleItemZPiecewiseFunction)ZvtkDataEncoderZvtkWebApplicationr/   rš   r›   r   zInstantiating )ZvtkPolyDataZvtkContext2DZvtkHyperOctreeZ	Dimension)r1   )r   r1   ZvtkHigherOrderTetraZParametricCoordsZ
vtkPlotBarZLookupTableFz
  Calling z.Getz()r‘   zGet%sMinValuezGet%sMaxValue)
rH   r   r   r6   r   r•   r’   rŸ   r9   r“   )r   r!   r"   r+   Zgsmr—   rB   r˜   r    Úvaluer¤   ÚlowÚhighr   r   r   rŒ   `  sÔ    ÿÿþýüûúùø	÷
ö
ÿ
ÿ



þüûÿþ	ÿþýüûúù
	
z%VTKMethodParser._find_get_set_methodsc                 C   sX   |dd… }| j }|dd… D ]4}|dkr.qq|dd… dkr| |¡ | |¡ q|S )z}Find/store methods of the form Get<Value> in the given
        `methods` and returns the remaining list of methods.

        Nr/   r1   )rJ   r7   r6   )r   r!   r"   r+   ZgmrB   r   r   r   r�   à  s    
z!VTKMethodParser._find_get_methodsc              	   C   sp   d}z
|ƒ }W n\ t tfyj   | jrf| j}| |j¡}|durf|jD ]}|  | |j¡¡}|rF qfqFY n0 |S )a!  Given a VTK class, `klass`, returns an instance of the
        class.

        If the class is abstract, it uses the class tree to return an
        instantiable subclass.  This is necessary to get the values of
        the 'state' methods and the ranges for the Get/Set methods.

        N)	r“   ÚNotImplementedErrorr   Zget_noder   Úchildrenr’   Z	get_classÚname)r   r!   r    ÚtÚnÚcr   r   r   r’   ð  s    	

zVTKMethodParser._get_instanceN)T)T)r   Ú
__module__Ú__qualname__rm   r   r$   r-   r   rE   rG   rI   rK   rM   Ústaticmethodrƒ   r„   r   r   r�   rŠ   r‹   rŒ   r�   r’   r   r   r   r   r      s.   @
(
&
Q
 %C r   )rm   Úcollections.abcr†   r   Útypesr   r   r   r   r
   Úcommonr   r   r   r   r   r   Ú<module>   s   