3
8Ad]  ã               @   s8  d dl mZmZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlZe
dd	„ ƒZd(dd„Zd)dd„Zeedd�Zeedd�Zeedd�Zeedd�Zeedd�Zeedd�ZejdkZdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"d*dd„Z#d d!„ Z$d"d#„ Z%d$d%„ Z&d&d'„ Z'dS )+é    )Úabsolute_importÚprint_functionN)ÚpartialÚwraps)Úcontextmanager)Úskip)ÚSkipTest)Ú	ETSConfigc              #   st   g ‰dd„ ‰ ‡ ‡fdd„} ‡ ‡fdd„}t jj}z| t_|t j_dV  W dtˆƒdkr^ˆd ‚tjt_|t j_X dS )	a;  Context manager that captures all exceptions, even those coming from
    the UI thread. On exit, the first exception is raised (if any).

    It also temporarily overwrites the global function
    traits.trait_notifier.handle_exception , which logs exceptions to
    console without re-raising them by default.
    c             S   s(   d}|dj tj| ||ƒƒ7 }t|ƒ d S )NzUncaught exception:
Ú )ÚjoinÚ	tracebackÚformat_exceptionÚprint)ÚtypeÚvalueÚtbÚmessage© r   úq/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/tests/_tools.pyÚ_print_uncaught_exception,   s    zBstore_exceptions_on_all_threads.<locals>._print_uncaught_exceptionc                s   ˆj |ƒ ˆ | ||ƒ d S )N)Úappend)r   r   r   )r   Ú
exceptionsr   r   Ú
excepthook1   s    
z3store_exceptions_on_all_threads.<locals>.excepthookc                s(   t jƒ \}}}ˆj|ƒ ˆ |||ƒ d S )N)ÚsysÚexc_infor   )ÚobjectZ
trait_nameÚoldÚnewr   r   r   )r   r   r   r   Úhandle_exception5   s    
z9store_exceptions_on_all_threads.<locals>.handle_exceptionNr   )ÚtraitsZtrait_notifiersr   r   r   ÚlenÚ__excepthook__)r   r   Z_original_handle_exceptionr   )r   r   r   Ústore_exceptions_on_all_threads    s    

r"   r
   c             C   s
   t j| kS )N)r	   Útoolkit)Úbackend_namer   r   r   Ú_is_current_backendF   s    r%   c                sP   t jˆƒr6tˆ ƒs0ˆ dkrdnd}t|ƒˆƒ}qLˆ}ntˆƒ‡ ‡fdd„ƒ}|S )z@Decorator that skip tests if the backend is not the desired one.r
   zTest only for {}c                 s6   t ˆ ƒs(ˆ dkrdnd}t|jˆ ƒƒ‚n
ˆ| |ŽS d S )Nr
   zTest only for {})r%   r   Úformat)ÚargsÚkwargsr   )r$   Úitemr   r   ÚwrapperT   s    z$skip_if_not_backend.<locals>.wrapper)ÚinspectÚisclassr%   r   r   )r)   r$   r   r*   r   )r$   r)   r   Úskip_if_not_backendJ   s    
r-   Úwx)r$   Zqt4ÚnullZDarwinc                s   t ˆ ƒ‡ fdd„ƒ}|S )zÎDecorator that skip tests if the backend is set to 'null'.

    Some tests handle both wx and Qt in one go, but many things are not
    defined in the null backend. Use this decorator to skip the test.
    c                 s    t dƒrtdƒ‚n
ˆ | |ŽS d S )Nr/   z&Test not working on the 'null' backend)r%   r   )r'   r(   )Ú	test_funcr   r   r*   |   s    
zskip_if_null.<locals>.wrapper)r   )r0   r*   r   )r0   r   Úskip_if_nullv   s    r1   c                s   ‡ ‡fdd„‰dˆ_ ˆS )zrDecorator that stores the number of times a function is called.

    The counter is stored in func._n_counts.
    c                 s   ˆ j d7  _ ˆ | |ŽS )Né   )Ú_n_calls)r'   r(   )ÚfuncÚwrappedr   r   r5   ‹   s    zcount_calls.<locals>.wrappedr   )r3   )r4   r   )r4   r5   r   Úcount_calls…   s    r6   c             C   s   t jdkr| jƒ S | jƒ S d S )Nr.   )r	   r#   ÚGetChildrenÚchildren)Únoder   r   r   Úget_children–   s    
r:   c             C   sf   t ƒ r8ddl}| jjdƒ}|j|j|jƒ ƒ}|j|ƒ n*tƒ rbddl	m
} | jj|jjƒ}|jƒ  dS )z)Press the OK button in a wx or qt dialog.r   NZbutton)Úqt)Úis_current_backend_wxr.   ZcontrolZFindWindowByNameZCommandEventZwxEVT_COMMAND_BUTTON_CLICKEDÚGetIdÚProcessEventÚis_current_backend_qt4Zpyfacer;   Z	findChildZQtGuiZQPushButtonZclick)Zuir.   Z	ok_buttonZclick_eventr;   r   r   r   Úpress_ok_button�   s    
r@   c             C   s0   t ƒ r| jƒ S tƒ r,| jƒ jƒ | jƒ jƒ fS dS )z¡Return the size of the dialog.

    Return a tuple (width, height) with the size of the dialog in pixels.
    E.g.:

        >>> get_dialog_size(ui.control)
    N)r<   ZGetSizeTupler?   ÚsizeÚwidthÚheight)Z
ui_controlr   r   r   Úget_dialog_size°   s    	rD   c             C   sV   t d| t|ƒ ƒ t d| t| |ƒƒ d ƒ x t|ƒD ]}t| ||d ƒ q:W dS )zHPrint the result of applying a function on `node` and its children.
    ú-ú Ú
r2   N)r   Ústrr:   Úapply_on_children)r4   r9   Ú_levelÚchildr   r   r   rI   Â   s    rI   c             C   s   t dd„ | ƒ dS )z�Print the name and id of `node` and its children.

    Use as::

        >>> ui = xxx.edit_traits()
        >>> wx_print_names(ui.control)
    c             S   s   | j ƒ | jƒ fS )N)ÚGetNamer=   )Únr   r   r   Ú<lambda>Ó   s    z wx_print_names.<locals>.<lambda>N)rI   )r9   r   r   r   Úwx_print_namesË   s    rO   c             C   s   t dd„ | ƒ dS )zˆPrint the name of `node` and its children.

    Use as::

        >>> ui = xxx.edit_traits()
        >>> qt_print_names(ui.control)
    c             S   s   | j ƒ S )N)Z
objectName)rM   r   r   r   rN   Þ   s    z qt_print_names.<locals>.<lambda>N)rI   )r9   r   r   r   Úqt_print_namesÖ   s    rP   c                s(   ˆj ‰ ‡ ‡fdd„}|ˆ_ djˆjƒ ƒS )z¦Prints a message when `node` is destroyed.

    Use as:

        >>> ui = xxx.edit_traits()
        >>> apply_on_children(wx_announce_when_destroyed, ui.control)
    c                  s   t dˆƒ ˆ ƒ  t dˆƒ d S )NzDestroying:z
Destroyed:)r   r   )Ú_destroy_methodr9   r   r   Údestroy_wrappedì   s    
z3wx_announce_when_destroyed.<locals>.destroy_wrappedzNode {} decorated)ZDestroyr&   rL   )r9   rR   r   )rQ   r9   r   Úwx_announce_when_destroyedá   s    	rS   c                s$   ddl ‰‡ ‡fdd„tˆƒD ƒ}|S )zÇFind all wx event names that correspond to a certain event number.

    Example:

        >>> wx_find_event_by_number(10010)
        ['wxEVT_COMMAND_MENU_SELECTED', 'wxEVT_COMMAND_TOOL_CLICKED']
    r   Nc                s(   g | ] }|j d ƒrtˆ|ƒˆ kr|‘qS )ZwxEVT)Ú
startswithÚgetattr)Ú.0Úattr)Úevt_numr.   r   r   ú
<listcomp>  s    z+wx_find_event_by_number.<locals>.<listcomp>)r.   Údir)rX   Úpossibler   )rX   r.   r   Úwx_find_event_by_number÷   s    	r\   )r
   )r
   )r   )(Ú
__future__r   r   r   r   r+   Ú	functoolsr   r   Ú
contextlibr   Úunittestr   Únoser   Ztraits.etsconfig.apir	   Ztraits.trait_notifiersr   r"   r%   r-   r<   r?   Zis_current_backend_nullZskip_if_not_wxZskip_if_not_qt4Zskip_if_not_nullÚplatformZ	is_mac_osr1   r6   r:   r@   rD   rI   rO   rP   rS   r\   r   r   r   r   Ú<module>   s:   &



	