3
8ßL] 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   ú:/tmp/pip-install-5sebvq2j/traitsui/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_tokensZttypeZtstringZtstartÚtendZtlineZ
token_nameZsource_linesZ	docstringr0   r0   r1   Úextract_docstring_from_sourceJ   s    
"$
rA   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ÚreadrA   Ú	ExceptionÚ	tracebackÚ
format_excÚformat)Ú	file_nameÚfhZsource_coderH   Ztraceback_textÚ	error_fmtÚ	error_msgr0   r0   r1   Úparse_sourcep   s    

rO   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__rB   zutf-8)rC   ÚexecZmodal_popup)ÚdemoZpopuprU   )Úmsg)ÚinfoÚobjectÚlogÚsysÚstdoutÚstderrrO   ÚpathÚdescriptionr>   Úappendr%   ÚparentÚinit_dicÚmodulesrR   rD   rE   rT   ÚcompilerF   Ú_get_objectÚModalDemoButtonÚ
DemoButtonrG   Ú	DemoErrorÚstrÚremoverU   )ÚselfrW   ÚdfÚlocalsÚfprU   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.rB   zutf-8)rC   rT   N)rD   rE   r]   rT   rc   rF   )rj   rk   rl   rm   r0   r0   r1   Úexecute_testÉ   s    zDemoFileHandler.execute_testc             C   s   d|j _dS )z Closes the view.
        N)rX   rU   )rj   rW   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   )rj   r/   ZdicrX   r0   r0   r1   rd   Û   s    

zDemoFileHandler._get_objectc             C   s   | j j j|7  _d S )N)rW   rX   rY   )rj   Útextr0   r0   r1   Úwriteí   s    zDemoFileHandler.writec             C   s   d S )Nr0   )rj   r0   r0   r1   Úflushð   s    zDemoFileHandler.flushN)rQ   Ú
__module__Ú__qualname__r   r   rW   rn   ro   rp   rd   rv   rw   r0   r0   r0   r1   rP   Œ   s   0		rP   c               @   s,   e Zd ZeZeeedƒedddd�ƒƒZ	dS )rg   zError in source filerV   ÚcustomF)ÚstyleÚ
show_labelN)
rQ   rx   ry   r   rV   r   r   r   r   Útraits_viewr0   r0   r0   r1   rg   û   s
   rg   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 )rf   rU   FzRun demo...Zlive)ÚlabelÚkind)r|   Úeditorz!Click the button to run the demo:Z20Ú	demo_itemT)Ú	resizableN)rQ   rx   ry   r   r   rU   r   r   r�   r   r   r   r   r   r   r}   r0   r0   r0   r1   rf     s   rf   c               @   s"   e Zd Zeddeddd�d�ZdS )re   rU   FzRun demo...Úmodal)r~   r   )r|   r€   N)rQ   rx   ry   r   r   r�   r0   r0   r0   r1   re   ;  s   re   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)rj   Ú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)rj   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)rj   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)rj   r†   r0   r0   r1   r‰   }  s    zDemoTreeNodeObject.has_childrenc             C   s   t ‚dS )z% Gets the object's children.
        N)rŽ   )rj   r†   r0   r0   r1   rŒ   †  s    zDemoTreeNodeObject.get_children)N)rQ   rx   ry   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!   r`   r]   r/   )rj   r0   r0   r1   Ú	_get_path¶  s    zDemoFile._get_pathc             C   s   | j st| jƒ| _ | j S )N)Ú
_nice_namer2   r/   )rj   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)rj   Ú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   )rj   r0   r0   r1   r‰   Ë  s    zDemoFile.has_childrenN)rQ   rx   ry   r
   r`   r	   r]   r   r/   r“   r   r…   r   r^   r   r>   r   r   rU   rY   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!   r`   r]   r/   )rj   r0   r0   r1   r�     s    zDemoPath._get_pathc             C   s   | j st| jƒ| _ | j S )N)r‘   r2   r/   )rj   r0   r0   r1   r’     s    zDemoPath._get_nice_namec             C   s   | j }|| _| jd||ƒ d S )Nr“   )r“   r‘   r”   )rj   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)rj   r0   r0   r1   Ú_get_description  s    
zDemoPath._get_descriptionc             C   s   | j d kr| jƒ  | j S )N)Ú_sourcerš   )rj   r0   r0   r1   Ú_get_source'  s    
zDemoPath._get_sourcec             C   s*   i }t t| jdƒƒ\}}tt| |ƒ |S )Nz__init__.py)rO   r!   r]   rT   Úexec_str)rj   ra   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_filesrO   r!   r]   r™   rž   rœ   )rj   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$   )rj   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)rj   Ú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.
        )r`   r/   z.pyr¡   r/   )Úkey)r]   r    r!   r"   Úhas_py_filesr_   r˜   r    r$   r�   ÚoperatorÚ
attrgetterÚsort)rj   Ú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)r`   r/   r¯   z.pyr¡   r,   r4   r   r“   )r©   )r¤   r(   r¥   r   r§   Úitemsrq   Úpopr˜   r“   r_   rs   Úlistr!   r]   ÚglobÚiglobr$   r&   Úlenr�   r«   r¬   r­   )rj   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$   )rj   r]   r/   r¢   r£   r0   r0   r1   rª   Ð  s    

zDemoPath.has_py_filesN)"rQ   rx   ry   r
   r`   r	   r]   r   r/   r“   r   r^   r   r>   ra   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^   ÚDescriptionFÚreadonlyT)Zformat_text)r~   r|   r{   r€   r>   ZSourcerz   )r~   r|   r{   ZDockWindowShellZtabbed)ÚexportÚidztraitsui.demos.demo.path_view)rÁ   rU   ÚDemo)r~   r|   r{   r‚   Zvisible_whenrY   )r|   r{   ztraitsui.demos.demo.file_view)rÁ   Úhandlerr“   )Znode_forr~   Úview)Z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Æ   )rj   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)r`   )rj   rÅ   r0   r0   r1   Ú_root_changede  s    zDemo._root_changedN)rQ   rx   ry   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'   rZ   Úargvr#   r·   r)   r!   rÂ   r˜   Zconfigure_traits)r    Zdir_namer¥   rÆ   r]   r/   r0   r0   r1   rU   o  s    )FNr4   rÌ   )FÚ__doc__Ú
__future__r   rZ   rµ   r9   r6   r«   rD   r   Z	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   rA   rO   rP   Zdemo_file_handlerrg   rf   re   r„   r�   r˜   Z	path_viewZ	demo_viewrÉ   rÂ   rU   r0   r0   r0   r1   Ú<module>   s–   4H,
&h'DE  	: 