a
    ©ˆcg#  ã                   @   sv   d Z ddlZddlmZmZmZmZmZ ddlm	Z	 ddl
mZ e e¡ZG dd„ deƒZee	ƒG d	d
„ d
eƒƒZdS )ú The service registry. é    N)ÚDictÚEventÚ	HasTraitsÚIntÚprovidesé   )ÚIServiceRegistry)ÚImportManagerc                   @   s   e Zd ZdZdS )ÚNoSuchServiceErrorz. Raised when a required service is not found. N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/envisage/service_registry.pyr      s   r   c                   @   s˜   e Zd ZdZeZeZeZe	Z
d dd„Zd!dd„Zdd„ Zd"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S )$ÚServiceRegistryr   Ú c                 C   s$   |   ||||¡}|du r t|ƒ‚|S )zŠ Return the service that matches the specified query.

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

        N)Úget_servicer   )ÚselfÚprotocolÚqueryÚminimizeÚmaximizeÚservicer   r   r   Úget_required_serviceE   s    	z$ServiceRegistry.get_required_servicec                 C   s.   |   ||||¡}t|ƒdkr&|d }nd}|S )z> Return at most one service that matches the specified query. r   N)Úget_servicesÚlen)r   r   r   r   r   Úservicesr   r   r   r   r   T   s
    
zServiceRegistry.get_servicec                 C   s8   z| j | \}}}W n ty2   td| ƒ‚Y n0 |S )z+ Return the service with the specified id. úno service with id <%d>)Ú	_servicesÚKeyErrorÚ
ValueError©r   Ú
service_idr   ÚobjÚ
propertiesr   r   r   Úget_service_from_id`   s
    z#ServiceRegistry.get_service_from_idc                    s¾   g }| j  ¡ D ]n\}\}}}	|  |¡|krt|tƒrBtƒ  |¡}
n|}
|  |
|||	|¡}t|ƒdksr|  	||	|¡r| 
|¡ qˆdkrœ|j‡fdd„d� nˆ dkrº|j‡ fdd„dd� |S )	z5 Return all services that match the specified query. r   r   c                    s
   t | ˆ ƒS ©N©Úgetattr©Úx)r   r   r   Ú<lambda>‡   ó    z.ServiceRegistry.get_services.<locals>.<lambda>)Úkeyc                    s
   t | ˆ ƒS r(   r)   r+   )r   r   r   r-   Š   r.   T)r/   Úreverse)r    ÚitemsÚ_get_protocol_nameÚ
isinstanceÚstrr
   Úimport_symbolÚ_resolve_factoryr   Ú_eval_queryÚappendÚsort)r   r   r   r   r   r   r$   Únamer%   r&   Zactual_protocolr   )r   r   r   r   k   s     

ÿzServiceRegistry.get_servicesc                 C   s@   z| j | \}}}| ¡ }W n ty:   td| ƒ‚Y n0 |S )z@ Return the dictionary of properties associated with a service. r   ©r    Úcopyr!   r"   r#   r   r   r   Úget_service_propertiesŽ   s    z&ServiceRegistry.get_service_propertiesNc                 C   sF   |   |¡}|du ri }|  ¡ }|||f| j|< || _t d||¡ |S )z Register a service. Nzservice <%d> registered %s)r2   Ú_next_service_idr    Ú
registeredÚloggerÚdebug)r   r   r%   r&   Zprotocol_namer$   r   r   r   Úregister_serviceš   s    
z ServiceRegistry.register_servicec                 C   sL   z(| j | \}}}||| ¡ f| j |< W n tyF   td| ƒ‚Y n0 dS )z= Set the dictionary of properties associated with a service. r   Nr;   )r   r$   r&   r   r%   Zold_propertiesr   r   r   Úset_service_properties«   s
    z&ServiceRegistry.set_service_propertiesc                 C   sL   z(| j  |¡\}}}|| _t d|¡ W n tyF   td| ƒ‚Y n0 dS )z Unregister a service. zservice <%d> unregisteredr   N)r    ÚpopÚunregisteredr@   rA   r!   r"   r#   r   r   r   Úunregister_serviceµ   s    z"ServiceRegistry.unregister_servicec                 C   s   i }|  |j¡ |  |¡ |S )z2 Create a namespace in which to evaluate a query. )ÚupdateÚ__dict__)r   r   r&   Ú	namespacer   r   r   Ú_create_namespaceÅ   s    
z!ServiceRegistry._create_namespacec                 C   s6   |   ||¡}zt||ƒ}W n ty0   d}Y n0 |S )z� Evaluate a query over a single service.

        Return True if the service matches the query, otherwise return False.

        F)rJ   ÚevalÚ	Exception)r   r   r&   r   rI   Úresultr   r   r   r7   Î   s    
zServiceRegistry._eval_queryc                 C   s$   t |tƒr|}nd|j|jf }|S )z- Returns the full class name for a protocol. z%s.%s)r3   r4   r   r   )r   Zprotocol_or_namer:   r   r   r   r2   Þ   s    
þz"ServiceRegistry._get_protocol_namec                 C   s   t ||ƒ S )z? Is the object a factory for services supporting the protocol? )r3   )r   r   r%   r   r   r   Ú_is_service_factoryì   s    z#ServiceRegistry._is_service_factoryc                 C   s   |  j d7  _ | j S )z Returns the next service ID. r   )Ú_service_id)r   r   r   r   r>   õ   s    z ServiceRegistry._next_service_idc                 C   sD   |   ||¡r@t|tƒr"tƒ  |¡}|f i |¤Ž}|||f| j|< |S )zA If 'obj' is a factory then use it to create the actual service. )rN   r3   r4   r
   r5   r    )r   r   r:   r%   r&   r$   r   r   r   r6   ü   s    
z ServiceRegistry._resolve_factory)r   r   r   )r   r   r   )r   r   r   )N)r   r   r   r   r   r?   rE   r   r    r   rO   r   r   r'   r   r=   rB   rC   rF   rJ   r7   r2   rN   r>   r6   r   r   r   r   r       s(    ÿ


#

		r   )r   ÚloggingZ
traits.apir   r   r   r   r   Zi_service_registryr	   Zimport_managerr
   Ú	getLoggerr   r@   rL   r   r   r   r   r   r   Ú<module>
   s   
