a
    ¦ˆcÝ)  ã                   @   sŠ   d Z ddlZddlZddlmZmZmZmZmZm	Z	m
Z
mZ e 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dS )a  
This module defines the :py:class:`Application` class for Pyface, Tasks
and similar applications.  Although the primary use cases are for GUI
applications, the :py:class:`Application` class does not have any explicit
dependency on GUI code, and can be used for CLI or server applications.

Usual usage is to subclass :py:class:`Application`, overriding at least the
:py:meth:`Application._run` method, but usually the
:py:meth:`Application.start` and :py:meth:`Application.stop`
methods as well.

However the class can be used as-is by listening to the
:py:attr:`Application.application_initialized` event and performing
appropriate work there::

    def do_work(event):
        print("Hello world")

    app = Application()
    app.observe(do_work, 'application_initialized')

é    N)Ú	DirectoryÚEventÚHasStrictTraitsÚInstanceÚReadOnlyÚStrÚVetoableÚVetoableEventc                   @   s   e Zd ZdZdS )ÚApplicationExceptionz7 Exception subclass for Application-centric exceptions N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úU/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/application.pyr
   3   s   r
   c                   @   s   e Zd ZdZdS )ÚApplicationExitz· Exception which indicates application should try to exit.

    If no arguments, then assumed to be a normal exit, otherwise the arguments
    give information about the problem.
    Nr   r   r   r   r   r   9   s   r   c                   @   s   e Zd ZdZeZeZdS )ÚApplicationEventz) An event associated with an application N)r   r   r   r   r   ÚapplicationÚ
event_typer   r   r   r   r   C   s   r   c                   @   sò   e Zd ZdZedƒZeƒ Zeƒ Zeƒ Ze	ƒ Z
e	ƒ ZeeeƒƒZeeeƒƒZeeeƒƒZeƒ ZeeeƒƒZeeeƒƒ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d„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#d d!„ Z$d"S )$ÚApplicationzÖ A base class for applications.

    This class handles the basic lifecycle of an application and a few
    fundamental facilities.  It is suitable as a base for any application,
    not just GUI applications.
    zPyface Applicationc                 C   s   dS )z¬ Start the application, setting up things that are required

        Subclasses should call the superclass start() method before doing any
        work themselves.
        Tr   ©Úselfr   r   r   ÚstartŠ   s    zApplication.startc                 C   s   dS )z® Stop the application, cleanly releasing resources if possible.

        Subclasses should call the superclass stop() method after doing any
        work themselves.
        Tr   r   r   r   r   Ústop’   s    zApplication.stopc              
   C   s  d }}t  d¡ |  d¡ |  ¡ }|�rt  d¡ |  d¡ z”z|  ¡ }W nN tyš } z6|jdkrrt  d¡ n
t  d¡ |jdk}W Y d}~n
d}~0 0 W t  d	¡ |  d
¡ |  ¡ }|rÒ|  d¡ t  d¡ n8t  d	¡ |  d
¡ |  ¡ }|�r
|  d¡ t  d¡ 0 |�o|�o|S )z‹ Run the application.

        Return
        ------
        status : bool
            Whether or not the application ran normally
        Fz---- Application starting ----Ústartingz---- Application started ----Ústartedr   z ---- ApplicationExit raised ----Nz---- Application stopping ----ÚstoppingÚstoppedz---- Application stopped ----)	ÚloggerÚinfoÚ_fire_application_eventr   Ú_runr   ÚargsÚ	exceptionr   )r   Úrunr   r   Úexcr   r   r   r%   š   s6    





"


û


zApplication.runFc              	   C   s|   t  d¡ |s|  ¡ rnz@z|  ¡  W n ty@   t  d¡ Y n0 W t  d¡ |  ¡  qxt  d¡ |  ¡  0 n
t  d¡ dS )aì   Exits the application.

        This method handles a request to shut down the application by the user,
        eg. from a menu.  If force is False, the application can potentially
        veto the close event, leaving the application in the state that it was
        before the exit method was called.

        Parameters
        ----------
        force : bool, optional (default False)
            If set, windows will receive no closing events and will be
            destroyed unconditionally. This can be useful for reliably tearing
            down regression tests, but should be used with caution.

        Raises
        ------
        ApplicationExit
            Some subclasses may trigger the exit by raising ApplicationExit.
        z"---- Application exit started ----z$Error preparing for application exitz---- Application exit ----z!---- Application exit vetoed ----N)r   r    Ú	_can_exitÚ_prepare_exitÚ	Exceptionr$   Ú_exit)r   Úforcer   r   r   ÚexitÀ   s    


ÿ
zApplication.exitc                 C   s(   t j | j¡s$t d¡ t  | j¡ dS )z” Set up the home directory for the application

        This is where logs, preference files and other config files will be
        stored.
        z3Application home directory does not exist, creatingN)ÚosÚpathÚexistsÚhomer   r    Úmakedirsr   r   r   r   Úinitialize_application_homeâ   s    
z'Application.initialize_application_homec                 C   s   |   d¡ dS )aK   Actual implementation of running the application

        This should be completely overriden by applications which want to
        actually do something.  Usually this method starts an event loop and
        blocks, but for command-line applications this could be where the
        main application logic is called from.
        Úapplication_initializedT)r!   r   r   r   r   r"   ò   s    
zApplication._runc                 C   s   t | |d�}t| ||ƒ d S )N)r   r   )r   Úsetattr)r   r   Úeventr   r   r   r!     s    z#Application._fire_application_eventc                 C   s   t ƒ  | _}|j S )ag   Is exit vetoed by anything?

        The default behaviour is to fire the :py:attr:`exiting` event and check
        to see if any listeners veto.  Subclasses may wish to override to
        perform additional checks.

        Returns
        -------
        can_exit : bool
            Return True if exit is OK, False if something vetoes the exit.
        )r   ÚexitingZveto)r   r5   r   r   r   r'   	  s    zApplication._can_exitc                 C   s   dS )zm Do any application-level state saving and clean-up

        Subclasses should override this method.
        Nr   r   r   r   r   r(     s    zApplication._prepare_exitc                 C   s
   t ƒ ‚dS )zs Shut down the application

        This is where application event loops and similar should be shut down.
        N)r   r   r   r   r   r*     s    zApplication._exitc                 C   s   ddl m} | ¡ S )z+ Use the application's directory as the id r   ©Ú	ETSConfig)Útraits.etsconfig.apir8   Z_get_application_dirname©r   r8   r   r   r   Ú_id_default)  s    zApplication._id_defaultc                 C   s   ddl m} tj |j| j¡S )z$ Default home comes from ETSConfig. r   r7   )r9   r8   r-   r.   ÚjoinZapplication_dataÚidr:   r   r   r   Ú_home_default/  s    zApplication._home_defaultc                 C   s   ddl m} |jS )z) Default user_data comes from ETSConfig. r   r7   )r9   r8   Ú	user_datar:   r   r   r   Ú_user_data_default5  s    zApplication._user_data_defaultc                 C   s   ddl m} |jS )z' Default company comes from ETSConfig. r   r7   )r9   r8   Úcompanyr:   r   r   r   Ú_company_default;  s    zApplication._company_defaultc                 C   s   ddl m} || ƒ}|S )z@ Default description is the docstring of the application class. r   )Úgetdoc)ÚinspectrC   )r   rC   Útextr   r   r   Ú_description_defaultA  s    z Application._description_defaultN)F)%r   r   r   r   r   ÚnamerA   Údescriptionr=   r   r0   r?   r   r   r   r   r   r3   r	   r6   r   r   r   r   r%   r,   r2   r"   r!   r'   r(   r*   r;   r>   r@   rB   rF   r   r   r   r   r   M   s8   &
"
r   )r   Úloggingr-   Z
traits.apir   r   r   r   r   r   r   r	   Ú	getLoggerr   r   r)   r
   r   r   r   r   r   r   r   Ú<module>
   s   (


