3
5Ad]i'  ã               @   s   d Z ddlmZ ddlZddlZddlZddl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Zy,ddlmZmZ ddlmZ ddlmZ W n< ek
rÌ   ddlmZmZ ddlmZ ddlmZ Y nX ejeƒZG d	d
„ d
ƒZdd„ Zedkrüeƒ  dS )zKThis module generates tvtk (Traited VTK) classes from the VTK-Python API.

é    )Úprint_functionN)ÚOptionParseré   )Úget_tvtk_nameÚcamel2enthought)ÚWrapperGenerator)ÚHelperGeneratorc               @   sD   e Zd ZdZddd„Zdd„ Zdd„ Zdd
d„Zdd„ Zdd„ Z	dS )ÚTVTKGeneratorzGenerates all the TVTK code.Ú c             C   sP   |st jƒ }tjj|dƒ| _tjj| jƒs6tj| jƒ d| _t	ƒ | _
tƒ | _dS )aé  Initializes the instance.

        Parameters
        ----------

        - out_dir -  `string`

          The output directory to generate code in.  The directory is
          created if it does not exist.  A directory called
          `tvtk_classes` is created inside this directory and all the
          code is written here.  Any existing code there is blindly
          overwritten.  If no out_dir is specified, a temporary one is
          created using `tempfile.mkdtemp`.

        Útvtk_classesztvtk_classes.zipN)ÚtempfileÚmkdtempÚosÚpathÚjoinÚout_dirÚexistsÚmakedirsÚzip_namer   Úwrap_genr   Ú
helper_gen)Úselfr   © r   út/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/mayavi-4.7.1-py3.6-linux-x86_64.egg/tvtk/code_gen.pyÚ__init__+   s    zTVTKGenerator.__init__c             C   s¬  | j }| j}| j}ttjj|dƒdƒ� W dQ R X tjƒ }|j	ƒ dd… }|j
ƒ }d| }|d| 7 }ttjj|dƒdƒ�}|j|ƒ W dQ R X ttjj|dƒdƒ� �ü}	|j|	ƒ |jƒ j}
g }d	g}d
g}xn|jƒ D ]b}|j}||krêqÖ||koü|jdƒ sÖ|jdƒ�rqÖtt|ƒ sÖttt|ƒdƒ �r.qÖ|j|ƒ qÖW x`|
D ]X}xP|D ]H}|j|k�rLt|jƒ}tjd|j|f ƒ | j||ƒ |j||	ƒ �qLW �qBW W dQ R X dS )z9Generate all the wrapper code in `self.out_dir`.
        z__init__.pyÚwNé   zvtk_build_version = '%s'
zvtk_build_src_version = '%s'
zvtk_version.pyztvtk_helper.pyÚvtkOpenGLGL2PSHelperImplZVTKPythonAlgorithmBaseÚvtkZvtkQtÚIsAzWrapping %s as %s)r   r   r   Úopenr   r   r   r   Ú
vtkVersionÚGetVTKVersionÚGetVTKSourceVersionÚwriteZwrite_prelimsÚget_treeÚtreeÚnameÚ
startswithÚhasattrÚgetattrÚappendr   ÚloggerÚdebugÚ_write_wrapper_classÚ	add_class)r   r   r   r   ÚvZvtk_versionZvtk_src_versionÚcodeÚfZhelper_filer&   ÚclassesÚignoreÚincludeÚnoder'   ÚnodesÚ	tvtk_namer   r   r   Úgenerate_codeJ   sH    


 


zTVTKGenerator.generate_codec             C   s¸   g }x:|D ]2}| j jƒ j|ƒ}|dkr2td| ƒ |j|ƒ q
W x>|dd… D ].}|jƒ }x |D ]}||kr`|jd|ƒ q`W qNW |jdd„ d� x"|D ]}t|j	ƒ}| j
||ƒ q–W dS )aq  Given VTK class names in the list `names`, write out the
        wrapper classes to a suitable file.  This is a convenience
        method so one can generate a just a few of the wrapper classes
        if desired.  This is useful when debugging.  Please note that
        the method also generates code for all the ancestors of the
        specified classes.

        NzERROR: Cannot find class: %sr   c             S   s   | j S )N)Úlevel)Úxr   r   r   Ú<lambda>Ÿ   s    z5TVTKGenerator.write_wrapper_classes.<locals>.<lambda>)Úkey)r   r%   Zget_nodeÚprintr+   Úget_ancestorsÚinsertÚsortr   r'   r.   )r   Únamesr7   r'   r6   ZancÚir8   r   r   r   Úwrite_wrapper_classes†   s    



z#TVTKGenerator.write_wrapper_classesFc             C   sÌ   t jƒ }t jj| jƒ}t j|ƒ tj| jdtj	ƒ}|rtt
j
t jjddƒƒ}x(|D ] }t jj|ƒ}|j|d| ƒ qPW |jdƒ |jƒ  t jj|d | j ƒr°t j|d | j ƒ tj| j|ƒ t j|ƒ dS )a4  Build the zip file (with name `self.zip_name`) in the
        current directory.

        Parameters
        ----------
        include_src : `bool` (default: False)
          If True, also includes all the ``*.py`` files in the ZIP file.
          By default only the ``*.pyc`` files are included.

        r   r   z*.pyztvtk_classes/%sú/N)r   Úgetcwdr   Údirnamer   ÚchdirÚzipfileÚ	PyZipFiler   ÚZIP_DEFLATEDÚglobr   Úbasenamer$   ÚwritepyÚcloser   ÚunlinkÚshutilÚmove)r   Zinclude_srcÚcwdÚdÚzÚlr;   Úfnamer   r   r   Ú	build_zip¦   s     



zTVTKGenerator.build_zipc             C   s^   t jj| jƒ}t j|ƒ}d}t|ƒdkr8|d dkr8d}|rHtj|ƒ ntd|ƒ tdƒ dS )zRDelete the temporary directory where the code has been
        generated.
        r   r   r   zNot removing directory:z-It does not contain a tvtk_classes directory!N)	r   r   rG   r   ÚlistdirÚlenrQ   Úrmtreer>   )r   Ztmp_dirrT   Úokr   r   r   ÚcleanÂ   s    

zTVTKGenerator.cleanc             C   sf   t |ƒd }tjd dkr6ttjj| j|ƒddd�}nttjj| j|ƒdƒ}| jj	||ƒ |j
ƒ  dS )z!Write the wrapper code to a file.z.pyr   é   r   zutf-8)ÚencodingN)r   ÚsysÚversion_infor    r   r   r   r   r   r9   rO   )r   r6   r8   rW   Úoutr   r   r   r.   Ô   s    z"TVTKGenerator._write_wrapper_classN)r
   )F)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r9   rD   rX   r]   r.   r   r   r   r   r	   (   s   
< 
r	   c           	   C   s&  d} t | ƒ}|jdddddddd	� |jd
dddddd� |jddddddd� |jddddddd� |jddddddd� |jƒ \}}|jrÔtjƒ }|jtjƒ tjƒ }|jtjƒ tj	dƒ}|j
|ƒ |j|ƒ t|jƒ}t|ƒd krô|jƒ  n
|j|ƒ |j�r|j|jƒ |j�r"|jƒ  d S )!Nzçusage: %prog [options] [vtk_classes]

The options are described below.  An optional list of VTK classes for
which code is to be generated may be specified.  If none are specified
code will be generated for all the VTK classes.
    z-oz--output-dirÚstoreÚstringr   r
   z+Output directory in which to generate code.)ÚactionÚtypeÚdestÚdefaultÚhelpz-nz
--no-cleanÚstore_falser]   Tz%Do not clean the temporary directory.)ri   rk   rl   rm   z-zz--no-zipfileÚzipzDo not create a ZIP file.z-sz--sourceÚ
store_trueÚsrcFzGInclude source files (*.py) in addition to *.pyc files in the ZIP file.z-vz	--verboseÚverbosezVerbose output for debuggingz4%(asctime)s - %(name)s - %(levelname)s - %(message)sr   )r   Ú
add_optionÚ
parse_argsrr   ÚloggingÚ	getLoggerÚsetLevelÚDEBUGÚStreamHandlerÚ	FormatterÚsetFormatterÚ
addHandlerr	   r   rZ   r9   rD   ro   rX   rq   r]   )ÚusageÚparserÚoptionsÚargsr,   ÚchÚ	formatterÚgenr   r   r   Úmainå   sP    




r„   Ú__main__)rf   Ú
__future__r   Z
vtk_moduler   r   Úos.pathrI   r   rQ   rL   ru   Úoptparser   r`   Úcommonr   r   Zwrapper_genr   Zspecial_genr   ÚSystemErrorrv   rc   r,   r	   r„   r   r   r   r   Ú<module>   s2   
 >: