3
=Ad]­A  ã               @   sâ   d Z ddlZddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
mZmZmZm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mZ ddlmZ ddlmZ eje ƒZ!eeƒG dd„ deƒƒZ"dS )z( An extensible, pluggable, application. é    N)Ú	ETSConfig)ÚIPreferencesÚScopedPreferences)Úset_default_preferences)ÚDelegateÚEventÚ	HasTraitsÚInstanceÚStr)ÚVetoableEventÚprovidesé   )ÚIApplication)ÚIExtensionRegistry)ÚIImportManager)ÚIPluginManager)ÚIServiceRegistry)ÚApplicationEvent)ÚImportManagerc                   sº  e Zd ZdZeZeZeZee	ƒZ
eeƒZeeƒZeeƒZeeƒZeddd�Zeddd�ZeeƒZeeƒZeeƒZeeed�ZdP‡ fdd„	Zd	d
„ Zdd„ Z dd„ Z!dd„ Z"dQdd„Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dRd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,„ Z0dSd-d.„Z1d/d0„ Z2dTd1d2„Z3dUd4d5„Z4dVd6d7„Z5d8d9„ Z6d:d;„ Z7dWd<d=„Z8dXd>d?„Z9d@dA„ Z:dBdC„ Z;dDdE„ Z<dFdG„ Z=dHdI„ Z>dJdK„ Z?dLdM„ Z@dNdO„ ZA‡  ZBS )YÚApplicationz» An extensible, pluggable, application.

    This class handles the common case for non-GUI applications, and it is
    intended to be subclassed to change start/stop behaviour etc.

    Úplugin_managerT)Úmodify)ÚfactoryNc                sH   t t| ƒjf |Ž | jƒ  t| jƒ |dk	rDx|D ]}| j|ƒ q2W dS )aˆ   Constructor.

        We allow the caller to specify an initial list of plugins, but the list
        itself is not part of the public API. To add and remove plugins after
        after construction, use the 'add_plugin' and 'remove_plugin' methods
        respectively. The application is also iterable, so to iterate over the
        plugins use 'for plugin in application: ...'.

        N)Úsuperr   Ú__init__Ú_initialize_application_homer   ÚpreferencesÚ
add_plugin)ÚselfÚpluginsÚtraitsÚplugin)Ú	__class__© úp/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/envisage-4.7.2-py3.6.egg/envisage/application.pyr   h   s    

zApplication.__init__c             C   s   t jS )z Trait initializer. )r   Úapplication_home)r   r#   r#   r$   Ú_home_default“   s    zApplication._home_defaultc             C   s,   t jjtj| jƒ}t jj|ƒs(t j|ƒ |S )z Trait initializer. )ÚosÚpathÚjoinr   Ú	user_dataÚidÚexistsÚmakedirs)r   r*   r#   r#   r$   Ú_user_data_default˜   s
    
zApplication._user_data_defaultc             C   s   t ƒ S )z Trait initializer. )r   )r   r#   r#   r$   Ú_preferences_default¥   s    z Application._preferences_defaultc             C   s   | j ƒ r| jƒ  dS )z Run the application. N)ÚstartÚstop)r   r#   r#   r$   Úrun¬   s    zApplication.runc             C   s   | j j||ƒ dS )z4 Add a listener for extensions being added/removed. N)Úextension_registryÚadd_extension_point_listener)r   ÚlistenerÚextension_point_idr#   r#   r$   r4   ¸   s    z(Application.add_extension_point_listenerc             C   s   | j j|ƒ dS )z Add an extension point. N)r3   Úadd_extension_point)r   Zextension_pointr#   r#   r$   r7   Á   s    zApplication.add_extension_pointc             C   s   | j j|ƒS )zL Return a list containing all contributions to an extension point.

        )r3   Úget_extensions)r   r6   r#   r#   r$   r8   È   s    zApplication.get_extensionsc             C   s   | j j|ƒS )z3 Return the extension point with the specified Id. )r3   Úget_extension_point)r   r6   r#   r#   r$   r9   Ï   s    zApplication.get_extension_pointc             C   s
   | j jƒ S )zL Return all extension points that have been added to the registry.

        )r3   Úget_extension_points)r   r#   r#   r$   r:   Ô   s    z Application.get_extension_pointsc             C   s   | j j||ƒ dS )z7 Remove a listener for extensions being added/removed. N)r3   Úremove_extension_point_listener)r   r5   r6   r#   r#   r$   r;   Û   s    z+Application.remove_extension_point_listenerc             C   s   | j j|ƒ dS )z Remove an extension point. N)r3   Úremove_extension_point)r   r6   r#   r#   r$   r<   ä   s    z"Application.remove_extension_pointc             C   s   | j j||ƒ dS )z7 Set the extensions contributed to an extension point. N)r3   Úset_extensions)r   r6   Ú
extensionsr#   r#   r$   r=   ë   s    zApplication.set_extensionsc             C   s   | j j|ƒS )z9 Import the symbol defined by the specified symbol path. )Ú_import_managerÚimport_symbol)r   Zsymbol_pathr#   r#   r$   r@   ö   s    zApplication.import_symbolc             C   s
   t | jƒS )z0 Return an iterator over the manager's plugins. )Úiterr   )r   r#   r#   r$   Ú__iter__ÿ   s    zApplication.__iter__c             C   s   | j j|ƒ dS )z Add a plugin to the manager. N)r   r   )r   r!   r#   r#   r$   r     s    zApplication.add_pluginc             C   s   | j j|ƒS )z* Return the plugin with the specified Id. )r   Ú
get_plugin)r   Ú	plugin_idr#   r#   r$   rC     s    zApplication.get_pluginc             C   s   | j j|ƒ dS )z# Remove a plugin from the manager. N)r   Úremove_plugin)r   r!   r#   r#   r$   rE     s    zApplication.remove_pluginc             C   sP   t jdƒ | jƒ  | _}|js>| jjƒ  | jƒ | _t jdƒ n
t jdƒ |j S )zW Start the plugin manager.

        Returns True unless the start was vetoed.

        z*---------- application starting ----------z)---------- application started ----------z.---------- application start vetoed ----------)ÚloggerÚdebugÚ_create_application_eventÚstartingÚvetor   r0   Ústarted)r   Úeventr#   r#   r$   r0     s    




zApplication.startc             C   s   | j j||ƒS )z Start the specified plugin. )r   Ústart_plugin)r   r!   rD   r#   r#   r$   rM   4  s    zApplication.start_pluginc             C   sZ   t jdƒ | jƒ  | _}|jsH| jjƒ  | jjƒ  | jƒ | _	t jdƒ n
t jdƒ |j S )zU Stop the plugin manager.

        Returns True unless the stop was vetoed.

        z*---------- application stopping ----------z)---------- application stopped ----------z----------- application stop vetoed ----------)
rF   rG   rH   ÚstoppingrJ   r   r1   r   ÚsaveÚstopped)r   rL   r#   r#   r$   r1   9  s    





zApplication.stopc             C   s   | j j||ƒS )z Stop the specified plugin. )r   Ústop_plugin)r   r!   rD   r#   r#   r$   rQ   Y  s    zApplication.stop_pluginÚ c             C   s   | j j||||ƒ}|S )zŠ Return the service that matches the specified query.

        Raise a 'NoSuchServiceError' exception if no such service exists.

        )Úservice_registryÚget_required_service)r   ÚprotocolÚqueryÚminimizeÚmaximizeÚservicer#   r#   r$   rT   b  s    z Application.get_required_servicec             C   s   | j j||||ƒ}|S )z> Return at most one service that matches the specified query. )rS   Úget_service)r   rU   rV   rW   rX   rY   r#   r#   r$   rZ   o  s    zApplication.get_servicec             C   s   | j j|ƒS )z+ Return the service with the specified id. )rS   Úget_service_from_id)r   Ú
service_idr#   r#   r$   r[   x  s    zApplication.get_service_from_idc             C   s   | j j|ƒS )z@ Return the dictionary of properties associated with a service. )rS   Úget_service_properties)r   r\   r#   r#   r$   r]   }  s    z"Application.get_service_propertiesc             C   s   | j j||||ƒ}|S )z5 Return all services that match the specified query. )rS   Úget_services)r   rU   rV   rW   rX   Zservicesr#   r#   r$   r^   ‚  s    zApplication.get_servicesc             C   s   | j j|||ƒ}|S )z Register a service. )rS   Úregister_service)r   rU   ÚobjÚ
propertiesr\   r#   r#   r$   r_   ‹  s    
zApplication.register_servicec             C   s   | j j||ƒ dS )z= Set the dictionary of properties associated with a service. N)rS   Úset_service_properties)r   r\   ra   r#   r#   r$   rb   ”  s    z"Application.set_service_propertiesc             C   s   | j j|ƒ dS )z Unregister a service. N)rS   Úunregister_service)r   r\   r#   r#   r$   rc   ›  s    zApplication.unregister_servicec             C   s   ddl m} || d�S )z Trait initializer. r   )ÚPluginExtensionRegistry)r   )Zplugin_extension_registryrd   )r   rd   r#   r#   r$   Ú_extension_registry_default¨  s    z'Application._extension_registry_defaultc             C   s   ddl m} || d�S )z Trait initializer. r   )ÚPluginManager)Úapplication)r   rf   )r   rf   r#   r#   r$   Ú_plugin_manager_default²  s    z#Application._plugin_manager_defaultc             C   s   ddl m} |ƒ S )z Trait initializer. r   )ÚServiceRegistry)rS   ri   )r   ri   r#   r#   r$   Ú_service_registry_default¼  s    z%Application._service_registry_defaultc             C   s    |dk	rd|_ |dk	r| |_ dS )z Static trait change handler. N)rg   )r   Z
trait_nameÚoldÚnewr#   r#   r$   Ú_plugin_manager_changedÜ  s
    z#Application._plugin_manager_changedc             C   s
   t | d�S )z Create an application event. )rg   )r   )r   r#   r#   r$   rH   é  s    z%Application._create_application_eventc             C   s2   t jjtj| jƒt_t jjtjƒs.t jtjƒ dS )z, Initialize the application home directory. N)	r'   r(   r)   r   Zapplication_datar+   r%   r,   r-   )r   r#   r#   r$   r   î  s
    z(Application._initialize_application_home)N)N)N)NN)NN)rR   rR   rR   )rR   rR   rR   )rR   rR   rR   )N)CÚ__name__Ú
__module__Ú__qualname__Ú__doc__r
   r+   Úhomer*   r	   r   r   r   r   rI   r   rK   rN   rP   r   Zplugin_addedZplugin_removedr   r3   r   r   r   rS   r   r   r?   r   r&   r.   r/   r2   r4   r7   r8   r9   r:   r;   r<   r=   r@   rB   r   rC   rE   r0   rM   r1   rQ   rT   rZ   r[   r]   r^   r_   rb   rc   re   rh   rj   rm   rH   r   Ú__classcell__r#   r#   )r"   r$   r      sf   	+
	
		
 
	

	
	
	

 r   )#rq   Úloggingr'   Ztraits.etsconfig.apir   Zapptools.preferences.apir   r   r   Z
traits.apir   r   r   r	   r
   r   r   Zi_applicationr   Zi_extension_registryr   Zi_import_managerr   Zi_plugin_managerr   Zi_service_registryr   Zapplication_eventr   Zimport_managerr   Ú	getLoggerrn   rF   r   r#   r#   r#   r$   Ú<module>   s    
