3
8Ad] w  ã               @   sr  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m	Z	 ddlZddl
mZ ddlmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( ddl)m*Z* dd	l+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 d
Z5dd„ Z6dd„ Z7dd„ Z8G dd„ de#ƒZ9e9ƒ Z:G dd„ deƒZ;G dd„ deƒZ<G dd„ de<ƒZ=G dd„ deƒZ>G dd„ de>ƒZ?G dd„ de>ƒZ@eeedd d!d"e&d#d$�d%�ed&d'd!d(d)�d*d+d,�d-d.�ZAeeedd d!d"e&d#d$�d%�ed&d'd!d(d)�ed/d0d!d(d#d/d1�ed2d!d"d3�d*d+d,�d4e:d5�ZBeee@gd6eAd7�ee?gd6eBd7�gd8�ZCG d9d0„ d0eƒZDd=d<d/„ZEdS )>zM A Traits UI demo that borrows heavily from the design of the wxPython demo.
é    )Úabsolute_importN)ÚStringIO)Ú	ConfigObj)Ú	HasTraitsÚHasPrivateTraitsÚStrÚInstanceÚPropertyÚAnyÚCodeÚHTMLÚtrueÚfalseÚDict)Ú
TreeEditorÚObjectTreeNodeÚTreeNodeObjectÚViewÚItemÚVSplitÚTabbedÚVGroupÚHGroupÚHeadingÚHandlerÚUIInfoÚInstanceEditorÚ
HTMLEditorÚIncludeÚspring)Úlistdir)	ÚjoinÚisdirÚsplitÚsplitextÚdirnameÚbasenameÚabspathÚexistsÚisabszfrom traits.api import *

c             C   s(   | j ddƒ} | d d… jƒ | dd …  S )NÚ_ú é   )ÚreplaceÚupper)Úname© r0   úp/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/extras/demo.pyÚuser_name_for@   s    r2   c             C   sÀ   t | ƒ}tj|jƒ}xB|D ].\}}}}}tj| }|dkr|d dkrP qW d| jƒ fS | jƒ }	tdj	|	|d d |d … ƒƒ}
|	d|d d … |	|d d…  }	dj	|	ƒ} | jƒ } |
| fS )aD  Return module docstring and source code from python source code.

    Parameters
    ----------
    source : Str (Unicode)
        Python source code.

    Returns
    -------
    docstring : str
        The first module-level string; i.e. the module docstring.
    source : str
        The source code, sans docstring.
    ÚSTRINGr,   r   Ú Ú
N)
r   ÚtokenizeÚgenerate_tokensÚreadlineÚtokenÚtok_nameÚstripÚ
splitlinesÚevalr!   )ÚsourceÚfZpython_tokensÚttypeZtstringZtstartÚtendZtlineZ
token_nameZsource_linesÚ	docstringr0   r0   r1   Úextract_docstring_from_sourceJ   s    
"$
rC   c             C   sh   y,t j| ddd��}|jƒ }W dQ R X t|ƒS  tk
rb   ddl}|jƒ }d}|j|ƒ}|dfS X dS )zîReturn module docstring and source code from python source file.

    Returns
    -------
    docstring : str
        The first module-level string; i.e. the module docstring.
    source : str
        The source code, sans docstring.
    Úrzutf-8)ÚencodingNr   z Sorry, something went wrong.

{}r4   )ÚioÚopenÚreadrC   Ú	ExceptionÚ	tracebackÚ
format_excÚformat)Ú	file_nameÚfhZsource_coderJ   Ztraceback_textÚ	error_fmtZ	error_msgr0   r0   r1   Úparse_sourcep   s    

rP   c               @   sD   e Zd ZeeƒZdd„ Zdd„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ ZdS )ÚDemoFileHandlerc             C   sT  || _ |j}d|_|  t_t_t|jƒ\|_|_	tjj
t|jƒƒ |jj}d|d< |j|d< |jtjd _yštj|jddd�� }tt|jƒ |jd	ƒ||ƒ W d Q R X | jd
|ƒ}|d k	rÄt|d�}n,| jd|ƒ}|d k	rät|d�}n| jd|ƒ}|d k�rd|d< W n2 tk
�r6 } ztt|ƒd�}W Y d d }~X nX tjjt|jƒƒ ||_d S )Nr4   Z
___main___Ú__name__Ú__file__Ú__main__rD   zutf-8)rE   ÚexecZmodal_popup)ÚdemoZpopuprV   )Úmsg)ÚinfoÚobjectÚlogÚsysÚstdoutÚstderrrP   ÚpathÚdescriptionr>   Úappendr%   ÚparentÚinit_dicÚmodulesrS   rF   rG   rU   ÚcompilerH   Ú_get_objectÚModalDemoButtonÚ
DemoButtonrI   Ú	DemoErrorÚstrÚremoverV   )ÚselfrX   ÚdfÚlocalsÚfprV   Zexcpr0   r0   r1   Úinit™   s4    
$
 zDemoFileHandler.initc             C   s<   t j|jddd�� }tt|jƒ |jdƒ||ƒ W dQ R X dS )z9 Executes the file in df.path in the namespace of locals.rD   zutf-8)rE   rU   N)rF   rG   r^   rU   rd   rH   )rk   rl   rm   rn   r0   r0   r1   Úexecute_testÉ   s    zDemoFileHandler.execute_testc             C   s   d|j _dS )z Closes the view.
        N)rY   rV   )rk   rX   Zis_okr0   r0   r1   ÚclosedÒ   s    zDemoFileHandler.closedc          	   C   sX   |j |ƒp|j |jƒ ƒ}|d k	rTtt|ƒtƒrFy
|ƒ }W n   Y nX t|tƒrT|S d S )N)ÚgetÚ
capitalizeÚ
isinstanceÚtyper   )rk   r/   ZdicrY   r0   r0   r1   re   Û   s    

zDemoFileHandler._get_objectc             C   s   | j j j|7  _d S )N)rX   rY   rZ   )rk   Útextr0   r0   r1   Úwriteí   s    zDemoFileHandler.writec             C   s   d S )Nr0   )rk   r0   r0   r1   Úflushð   s    zDemoFileHandler.flushN)rR   Ú
__module__Ú__qualname__r   r   rX   ro   rp   rq   re   rw   rx   r0   r0   r0   r1   rQ   Œ   s   0		rQ   c               @   s,   e Zd ZeZeeedƒedddd�ƒƒZ	dS )rh   zError in source filerW   ÚcustomF)ÚstyleÚ
show_labelN)
rR   ry   rz   r   rW   r   r   r   r   Útraits_viewr0   r0   r0   r1   rh   û   s
   rh   c               @   sR   e Zd ZeeƒZeddeddd�d�Ze	e
e
edƒdƒeeed	ƒeƒƒd
d�ZdS )rg   rV   FzRun demo...Zlive)ÚlabelÚkind)r}   Úeditorz!Click the button to run the demo:Z20Ú	demo_itemT)Ú	resizableN)rR   ry   rz   r   r   rV   r   r   r‚   r   r   r   r   r   r   r~   r0   r0   r0   r1   rg     s   rg   c               @   s"   e Zd Zeddeddd�d�ZdS )rf   rV   FzRun demo...Úmodal)r   r€   )r}   r�   N)rR   ry   rz   r   r   r‚   r0   r0   r0   r1   rf   ;  s   rf   c               @   s>   e Zd ZeZeZdd„ Zddd„Zdd„ Zdd	„ Z	d
d„ Z
dS )ÚDemoTreeNodeObjectc             C   s   | j S )zD Returns whether chidren of this object are allowed or not.
        )Úallows_children)rk   Únoder0   r0   r1   Útno_allows_children\  s    z&DemoTreeNodeObject.tno_allows_childrenNc             C   s   | j dkr| jƒ | _ | j S )z9 Returns whether or not the object has children.
        N)Ú_has_childrenÚhas_children)rk   r‡   r0   r0   r1   Útno_has_childrene  s    

z#DemoTreeNodeObject.tno_has_childrenc             C   s   | j dkr| jƒ | _ | j S )z% Gets the object's children.
        N)Ú_get_childrenÚget_children)rk   r‡   r0   r0   r1   Útno_get_childrenq  s    

z#DemoTreeNodeObject.tno_get_childrenc             C   s   t ‚dS )z9 Returns whether or not the object has children.
        N)ÚNotImplementedError)rk   r‡   r0   r0   r1   rŠ   }  s    zDemoTreeNodeObject.has_childrenc             C   s   t ‚dS )z% Gets the object's children.
        N)r�   )rk   r‡   r0   r0   r1   r�   †  s    zDemoTreeNodeObject.get_children)N)rR   ry   rz   r
   r‰   rŒ   rˆ   r‹   rŽ   rŠ   r�   r0   r0   r0   r1   r…   L  s   	
	r…   c               @   sX   e Zd ZeZeZeZeZ	e
ZeZeZeeƒZeZeZdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚDemoFilec             C   s   t | jj| jd ƒS )Nz.py)r!   ra   r^   r/   )rk   r0   r0   r1   Ú	_get_path¶  s    zDemoFile._get_pathc             C   s   | j st| jƒ| _ | j S )N)Ú
_nice_namer2   r/   )rk   r0   r0   r1   Ú_get_nice_name½  s    zDemoFile._get_nice_namec             C   s   | j }|| _| jd||ƒ d S )NÚ	nice_name)r”   r’   Útrait_property_changed)rk   ÚvalueÚoldr0   r0   r1   Ú_set_nice_nameÂ  s    zDemoFile._set_nice_namec             C   s   dS )z9 Returns whether or not the object has children.
        Fr0   )rk   r0   r0   r1   rŠ   Ë  s    zDemoFile.has_childrenN)rR   ry   rz   r
   ra   r	   r^   r   r/   r”   r   r†   r   r_   r   r>   r   r   rV   rZ   r’   r‘   r“   r˜   rŠ   r0   r0   r0   r1   r�   �  s   	r�   c               @   s¤   e Zd ZeZeZeZeZ	ee
ƒZeeƒZeZeZeZeZeZe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d„ Zdd„ Z dd„ Z!dS )ÚDemoPathc             C   s   t | jj| jƒS )N)r!   ra   r^   r/   )rk   r0   r0   r1   r‘     s    zDemoPath._get_pathc             C   s   | j st| jƒ| _ | j S )N)r’   r2   r/   )rk   r0   r0   r1   r“     s    zDemoPath._get_nice_namec             C   s   | j }|| _| jd||ƒ d S )Nr”   )r”   r’   r•   )rk   r–   r—   r0   r0   r1   r˜     s    zDemoPath._set_nice_namec             C   s   | j d kr| jƒ  | j S )N)Ú_descriptionÚ	_get_init)rk   r0   r0   r1   Ú_get_description  s    
zDemoPath._get_descriptionc             C   s   | j d kr| jƒ  | j S )N)Ú_sourcer›   )rk   r0   r0   r1   Ú_get_source'  s    
zDemoPath._get_sourcec             C   s*   i }t t| jdƒƒ\}}tt| |ƒ |S )Nz__init__.py)rP   r!   r^   rU   Úexec_str)rk   rb   r_   r>   r0   r0   r1   Ú_get_init_dic1  s    zDemoPath._get_init_dicc             C   s6   | j rtt| jdƒƒ\| _}n
d| _d}t| | _d S )Nz__init__.pyz<img src="traits_ui_demo.jpg">r4   )Ú	use_filesrP   r!   r^   rš   rŸ   r�   )rk   r>   r0   r0   r1   r›   F  s    zDemoPath._get_initc             C   sX   | j }xLt|ƒD ]@}t||ƒ}t|ƒr*dS | jrt|ƒ\}}|dkr|dkrdS qW dS )z9 Returns whether or not the object has children.
        Tz.pyÚ__init__F)r^   r    r!   r"   r¡   r$   )rk   r^   r/   Úcur_pathÚextr0   r0   r1   rŠ   U  s    
zDemoPath.has_childrenc             C   s"   | j s| jr| jƒ }n| jƒ }|S )z% Gets the object's children.
        )Úconfig_dictÚconfig_filenameÚget_children_from_configÚget_children_from_datastructure)rk   Úchildrenr0   r0   r1   r�   i  s    
zDemoPath.get_childrenc             C   s®   g }g }| j }xtt|ƒD ]h}t||ƒ}t|ƒrL| j|ƒr€|jt| |d�ƒ q| jrt|ƒ\}}|dkr|dkr|jt	| |d�ƒ qW t
jdƒ}|j|d� |j|d� || S )zG Gets the object's children based on the filesystem structure.
        )ra   r/   z.pyr¢   r/   )Úkey)r^   r    r!   r"   Úhas_py_filesr`   r™   r¡   r$   r�   ÚoperatorÚ
attrgetterÚsort)rk   ÚdirsÚfilesr^   r/   r£   r¤   Úsort_keyr0   r0   r1   r¨   u  s     


z(DemoPath.get_children_from_datastructurec             C   sÊ  | j s.t| jƒr.yt| jƒ| _ W n   Y nX | j s<| jƒ S g }g }�xX| j jƒ D �]H\}}|jdƒsR|jddƒ}|dk	ržt| |d�}||_	||_ |j
|ƒ qRg }|jdg ƒ}t|tƒs¾|g}xV|D ]N}	t| j|	ƒ}	x<tj|	ƒD ].}
t|
ƒ\}}|dkràt|ƒdkrà|j
|ƒ qàW qÄW t|ƒdk�rpi }x"|D ]}
d|
d i|t|
ƒ< �q.W t| d	d�}||_	||_ |j
|ƒ qRt|ƒdkrRt| |d
 d�}||_	|j
|ƒ qRW tjdƒ}|j|d� |j|d� || S )zj
        Gets the object's children as specified in its configuration file or
        dictionary.
        Zno_demoÚ	sourcedirN)ra   r/   r°   z.pyr¢   r,   r4   r   r”   )rª   )r¥   r(   r¦   r   r¨   Úitemsrr   Úpopr™   r”   r`   rt   Úlistr!   r^   ÚglobÚiglobr$   r&   Úlenr�   r¬   r­   r®   )rk   r¯   r°   Úkeywordr–   r²   ZdemoobjÚnamesÚ	filenamesÚfilenamer/   Úpathnamer¤   r¥   Úfiler±   r0   r0   r1   r§   ’  s\    





z!DemoPath.get_children_from_configc             C   sP   xJt |ƒD ]>}t||ƒ}t|ƒr0| j|ƒrHdS q
t|ƒ\}}|dkr
dS q
W dS )NTz.pyF)r    r!   r"   r«   r$   )rk   r^   r/   r£   r¤   r0   r0   r1   r«   Ð  s    

zDemoPath.has_py_filesN)"rR   ry   rz   r
   ra   r	   r^   r   r/   r”   r   r_   r   r>   rb   r   r¡   r†   r   r¥   r¦   r’   r‘   r“   r˜   rœ   rž   r    r›   rŠ   r�   r¨   r§   r«   r0   r0   r0   r1   r™   Õ  s0   		

>r™   r_   ZDescriptionFÚreadonlyT)Zformat_text)r   r}   r|   r�   r>   ZSourcer{   )r   r}   r|   ZDockWindowShellZtabbed)ZexportÚidztraitsui.demos.demo.path_view)rÀ   rV   ÚDemo)r   r}   r|   rƒ   Zvisible_whenrZ   )r}   r|   ztraitsui.demos.demo.file_view)rÀ   Úhandlerr”   )Znode_forr   Úview)Únodesc               @   s,   e Zd ZeZeeƒZeZdd„ Z	dd„ Z
dS )rÁ   c             C   s&   t tdddtd�| jddddd�}|S )	z$ Constructs the default traits view.ÚrootF)r/   rÀ   r}   r�   ztraitsui.demos.demo.DemoTi¶  i„  )ÚtitlerÀ   rƒ   ÚwidthÚheight)r   r   Údemo_tree_editorrÆ   )rk   r~   r0   r0   r1   Údefault_traits_viewH  s    zDemo.default_traits_viewc             C   s
   | |_ dS )z1 Handles the 'root' trait being changed.
        N)ra   )rk   rÅ   r0   r0   r1   Ú_root_changede  s    zDemo._root_changedN)rR   ry   rz   r   r^   r   r™   rÅ   rÆ   rÊ   rË   r0   r0   r0   r1   rÁ   5  s
   r4   úTraits UI Demosc             C   sh   |d krt ttjd ƒƒ}t|ƒ\}}t|ƒdkrHt|ƒ rHt|||ƒ}t||t	|| |d�d�j
ƒ  d S )Nr   )r/   r¡   r¦   )r^   rÆ   rÅ   )r%   r'   r[   Úargvr#   r¸   r)   r!   rÁ   r™   Zconfigure_traits)r¡   Údir_namer¦   rÆ   r^   r/   r0   r0   r1   rV   o  s    )FNr4   rÌ   )FÚ__doc__Ú
__future__r   r[   r¶   r9   r6   r¬   rF   r   Ú	configobjr   Z
traits.apir   r   r   r   r	   r
   r   r   r   r   r   Ztraitsui.apir   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Úosr    Úos.pathr!   r"   r#   r$   r%   r&   r'   r(   r)   rŸ   r2   rC   rP   rQ   Zdemo_file_handlerrh   rg   rf   r…   r�   r™   Z	path_viewZ	demo_viewrÉ   rÁ   rV   r0   r0   r0   r1   Ú<module>   s–   4H,
&h'DE  	: 