3
6ßL]f"  ã               @   s‚   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 ddlmZ ejeƒZG dd	„ d	eƒZee	ƒG d
d„ deƒƒZdS )z The service registry. é    N)ÚDictÚEventÚ	HasTraitsÚIntÚprovidesé   )ÚIServiceRegistry)ÚImportManager)ÚSTRING_BASE_CLASSc               @   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   ú?/tmp/pip-install-5sebvq2j/envisage/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 )$ÚServiceRegistryz The service registry. Ú c             C   s$   | j ||||ƒ}|dk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_service=   s    z$ServiceRegistry.get_required_servicec             C   s.   | j ||||ƒ}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   J   s
    
zServiceRegistry.get_servicec             C   s:   y| j | \}}}W n  tk
r4   td| ƒ‚Y nX |S )z+ Return the service with the specified id. zno service with id <%d>)Ú	_servicesÚKeyErrorÚ
ValueError)r   Ú
service_idr   ÚobjÚ
propertiesr   r   r   Úget_service_from_idV   s
    z#ServiceRegistry.get_service_from_idc                sÂ   g }x|| j jƒ D ]n\}\}}}	| j|ƒ|krt|tƒrDtƒ j|ƒ}
n|}
| j|
|||	|ƒ}t|ƒdkst| j	||	|ƒr|j
|ƒ qW ˆ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>}   s    z.ServiceRegistry.get_services.<locals>.<lambda>)Úkeyc                s
   t | ˆ ƒS )N)r&   )r'   )r   r   r   r(   €   s    T)r)   Úreverse)r   ÚitemsÚ_get_protocol_nameÚ
isinstancer
   r	   Úimport_symbolÚ_resolve_factoryr   Ú_eval_queryÚappendÚsort)r   r   r   r   r   r   r"   Únamer#   r$   Zactual_protocolr   )r   r   r   r   a   s    
zServiceRegistry.get_servicesc             C   sB   y| j | \}}}|jƒ }W n  tk
r<   td| ƒ‚Y nX |S )z@ Return the dictionary of properties associated with a service. zno service with id <%d>)r   Úcopyr    r!   )r   r"   r   r#   r$   r   r   r   Úget_service_properties„   s    z&ServiceRegistry.get_service_propertiesNc             C   sF   | j |ƒ}|dkri }| jƒ }|||f| j|< || _tjd||ƒ |S )z Register a service. Nzservice <%d> registered %s)r,   Ú_next_service_idr   Ú
registeredÚloggerÚdebug)r   r   r#   r$   Zprotocol_namer"   r   r   r   Úregister_service�   s    
z ServiceRegistry.register_servicec             C   sN   y(| j | \}}}|||jƒ f| j |< W n  tk
rH   td| ƒ‚Y nX dS )z= Set the dictionary of properties associated with a service. zno service with id <%d>N)r   r4   r    r!   )r   r"   r$   r   r#   Zold_propertiesr   r   r   Úset_service_properties¡   s    z&ServiceRegistry.set_service_propertiesc             C   sN   y(| j j|ƒ\}}}|| _tjd|ƒ W n  tk
rH   td| ƒ‚Y nX dS )z Unregister a service. zservice <%d> unregisteredzno service with id <%d>N)r   ÚpopÚunregisteredr8   r9   r    r!   )r   r"   r   r#   r$   r   r   r   Úunregister_service­   s    z"ServiceRegistry.unregister_servicec             C   s   i }|j |jƒ |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   s0   | j ||ƒ}yt||ƒ}W n   d}Y nX |S )z� Evaluate a query over a single service.

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

        F)rB   Úeval)r   r   r$   r   rA   Úresultr   r   r   r0   È   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)r-   r
   r   r   )r   Zprotocol_or_namer3   r   r   r   r,   Ø   s
    
z"ServiceRegistry._get_protocol_namec             C   s   t ||ƒ S )z? Is the object a factory for services supporting the protocol? )r-   )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   r6   î   s    z ServiceRegistry._next_service_idc             C   s@   | j ||ƒr<t|tƒr"tƒ j|ƒ}|f |Ž}|||f| j|< |S )zA If 'obj' is a factory then use it to create the actual service. )rE   r-   r
   r	   r.   r   )r   r   r3   r#   r$   r"   r   r   r   r/   õ   s    

z ServiceRegistry._resolve_factory)r   r   r   )r   r   r   )r   r   r   )N)r   r   r   r   r   r7   r=   r   r   r   rF   r   r   r%   r   r5   r:   r;   r>   rB   r0   r,   rE   r6   r/   r   r   r   r   r      s&   


#
		r   )r   ÚloggingZ
traits.apir   r   r   r   r   Zi_service_registryr   Zimport_managerr	   Ú_compatr
   Ú	getLoggerr   r8   Ú	Exceptionr   r   r   r   r   r   Ú<module>   s   
