3
<Ad]Š'  ã               @   sÒ   d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
 ddlmZ y
ejZW n, ek
r‚   ddlmZ edd„ ƒZY nX ejeƒZdded
ƒdœZdd„ ZG dd„ deƒZddd„Zdefdd„ZdS )a¯	   Common toolkit loading utilities and classes

This module provides common code for ETS packages that need to do GUI toolkit
discovery and loading.  The common patterns that ETS has settled on are that
where different GUI toolkits require alternative implementations of features
the toolkit is expected to provide a callable object which takes a relative
module path and an object name, separated by a colon and return the toolkit's
implementation of that object (usually this is a class, but it could be
anything).  The assumption is that this is implemented by objects in
sub-modules of the toolkit, but plugin authors are free to use whatever methods
they like.

Which toolkit to use is specified via the :py:mod:`traits.etsconfig.etsconfig`
package, but if this is not explicitly set by an application at startup or via
environment variables, there needs to be a way of discovering and loading any
available working toolkit implementations.  The default mechanism is via the
now-standard :py:mod:`pkg_resources` and :py:mod:`setuptools` "entry point"
system.

This module provides three things:

- a function :py:func:`import_toolkit` that attempts to find and load a toolkit
  entry point for a specified toolkit name

- a function :py:func:`find_toolkit` that attempts to find a toolkit entry
  point that works

- a class :py:class:`Toolkit` class that implements the standard logic for
  finding toolkit objects.

These are done in a library-agnostic way so that the same tools can be used
not just for different pyface backends, but also for TraitsUI and ETS
libraries where we need to switch between different GUI toolkit
implementations.

Note that there is no requirement for new toolkit implementations to use this
:py:class:`Toolkit` implementation, but they should be compatible with it.

Default toolkit loading logic
-----------------------------

The :py_func:`find_toolkit` function uses the following logic when attempting
to load toolkits:

- if ETSConfig.toolkit is set, try to load a plugin with a matching name.
  If it succeeds, we are good, and if it fails then we error out.

- after that, we try every 'pyface.toolkit' plugin we can find.  If one
  succeeds, we consider ourselves good, and set the ETSConfig.toolkit
  appropriately.  The order is configurable, and by default will try to load
  the `qt4` toolkit first, `wx` next, then all others in arbitrary order,
  and `null` last.

- finally, if all else fails, we try to load the null toolkit.
é    N)Ú	HasTraitsÚListÚReadOnlyÚStrÚ
TraitError)Ú	ETSConfig)Úcontextmanagerc          	   c   s8   t jrtdƒ‚| t _y
dV  W n   dt _‚ Y nX dS )zô Perform an operation with toolkit provisionally set

        This sets the toolkit attribute of the ETSConfig object set to the
        provided value. If the operation fails with an exception, the toolkit
        is reset to nothing.
        z ETSConfig toolkit is already setNÚ )r   ÚtoolkitÚAttributeErrorZ_toolkit)Útoolkit_name© r   úm/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/pyface-6.1.2-py3.6.egg/pyface/base_toolkit.pyÚprovisional_toolkitH   s    
r   é   é   Úinf)Zqt4ÚwxÚnullc             C   s   t j| jdƒS )Nr   )ÚTOOLKIT_PRIORITIESÚgetÚname)Úpluginr   r   r   Ú<lambda>c   s    r   c                   s8   e Zd ZdZeZeZeeƒZ	‡ fdd„Z
dd„ Z‡  ZS )ÚToolkita   A basic toolkit implementation for use by specific toolkits.

    This implementation uses pathname mangling to find modules and objects in
    those modules.  If an object can't be found, the toolkit will return a
    class that raises NotImplementedError when it is instantiated.
    c                s&   t t| ƒjf ||t|ƒdœ|—Ž d S )N)Úpackager
   Úpackages)Úsuperr   Ú__init__Úlist)Úselfr   r
   r   Útraits)Ú	__class__r   r   r   w   s
    
zToolkit.__init__c                sþ   ddl m} ˆjdƒ\}}|jdƒs,d| }x®| jD ]¤‰y||ˆƒ}W nx tk
r¾ ‰  z\t‡ fdd„|jdƒD ƒƒrv‚ dtjkr®ddl	}|j
tjƒ}|d \}}	}
}ˆ|kr®‚ W Y dd‰ Š X q4X t||dƒ}|dk	r4|S q4W | j‰G ‡‡‡fd
d„dtƒ}|S )zë Return the toolkit specific object with the given name.

        Parameters
        ----------
        name : str
            The name consists of the relative module path and the object name
            separated by a colon.
        r   )Úimport_moduleú:Ú.c             3   s    | ]}|r|ˆ j d  kV  qdS )r   N)Úargs)Ú.0Úpart)Úexcr   r   ú	<genexpr>“   s    z#Toolkit.__call__.<locals>.<genexpr>Z	ETS_DEBUGNr   c                   s    e Zd ZdZ‡ ‡‡fdd„ZdS )z'Toolkit.__call__.<locals>.UnimplementedzÃ An unimplemented toolkit object

            This is returned if an object isn't implemented by the selected
            toolkit.  It raises an exception if it is ever instantiated.
            c                s   d}t |ˆˆˆ f ƒ‚d S )Nz&the %s %s backend doesn't implement %s)ÚNotImplementedError)r    r&   ÚkwargsÚmsg)r   r   r
   r   r   r   °   s    z0Toolkit.__call__.<locals>.Unimplemented.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   )r   r   r
   r   r   ÚUnimplemented©   s   r2   éÿÿÿÿ)Ú	importlibr#   ÚsplitÚ
startswithr   ÚImportErrorÚallÚosÚenvironÚ	tracebackÚ
extract_tbÚsysÚexc_tracebackÚgetattrr
   Úobject)r    r   r#   ZmnameÚonameÚmoduler;   ÚframesÚfilenameÚlinenoÚfunctionÚtextÚobjr2   r   )r)   r   r   r
   r   Ú__call__   s,    	

zToolkit.__call__)r.   r/   r0   r1   r   r   r
   r   r   r   r   rI   Ú__classcell__r   r   )r"   r   r   f   s   r   úpyface.toolkitsc             C   s   t tj|| ƒƒ}t|ƒdkr@d}|j|| ƒ}tj|ƒ t|ƒ‚n4t|ƒdkrtd}djdd„ |D ƒƒ}tj	||| |ƒ xd|D ]\}y|j
ƒ }|S  ttfk
rÔ } z*d}tj||j|jƒ tj|d	d
� W Y dd}~X qzX qzW d}|j|| ƒ}tj|ƒ t|ƒ‚dS )aû   Attempt to import an toolkit specified by an entry point.

    Parameters
    ----------
    toolkit_name : str
        The name of the toolkit we would like to load.
    entry_point : str
        The name of the entry point that holds our toolkits.

    Returns
    -------
    toolkit_object : callable
        A callable object that implements the Toolkit interface.

    Raises
    ------
    RuntimeError
        If no toolkit is found, or if the toolkit cannot be loaded for some
        reason.
    r   z!No {} plugin found for toolkit {}r   z,multiple %r plugins found for toolkit %r: %sz, c             s   s   | ]}|j V  qd S )N)Úmodule_name)r'   r   r   r   r   r*   Ô   s    z!import_toolkit.<locals>.<genexpr>z Could not load plugin %r from %rT)Úexc_infoNz#No {} plugin could be loaded for {})r   Úpkg_resourcesÚiter_entry_pointsÚlenÚformatÚloggerÚdebugÚRuntimeErrorÚjoinÚwarningÚloadr7   r   Úinfor   rL   )r   Úentry_pointÚpluginsr-   Úmodulesr   Ztoolkit_objectr)   r   r   r   Úimport_toolkit·   s,    


$
r\   c                sâ   t jrtt j| ƒS ‡ fdd„tj| ƒD ƒ}xˆt||d�D ]x}y$t j|jƒ� |jƒ }|S Q R X W q8 t	t
tfk
r® } z,d}tj|| |j|jƒ tj|dd� W Y dd}~X q8X q8W t jdƒ� td| ƒS Q R X td	j| ƒƒ‚dS )
a   Find a toolkit that works.

    If ETSConfig is set, then attempt to find a matching toolkit.  Otherwise
    try every plugin for the entry_point until one works.  The ordering of the
    plugins is supplied via the priorities function which should be suitable
    for use as a sorting key function.  If all else fails, explicitly try to
    load the "null" toolkit backend.  If that fails, give up.

    Parameters
    ----------
    entry_point : str
        The name of the entry point that holds our toolkits.
    toolkits : collection of strings
        Only consider toolkits which match the given strings, ignore other
        ones.
    priorities : callable
        A callable function that returns an priority for each plugin.

    Returns
    -------
    toolkit : Toolkit instance
        A callable object that implements the Toolkit interface.

    Raises
    ------
    TraitError
        If no working toolkit is found.
    RuntimeError
        If no ETSConfig.toolkit is set but the toolkit cannot be loaded for
        some reason.
    c                s"   g | ]}ˆ d ks|j ˆ kr|‘qS )N)r   )r'   r   )Útoolkitsr   r   ú
<listcomp>
  s    z find_toolkit.<locals>.<listcomp>)ÚkeyNz#Could not load %s plugin %r from %rT)rM   r   z Could not import any {} toolkit.)r   r
   r\   rN   rO   Úsortedr   r   rW   r7   r   rT   rR   rX   rL   rS   r   rQ   )rY   r]   Z
prioritiesÚentry_pointsr   r
   r)   r-   r   )r]   r   Úfind_toolkitæ   s     $rb   éþÿÿÿr3   )rK   )r1   Úloggingr9   rN   r=   Z
traits.apir   r   r   r   r   Ztraits.etsconfig.apir   r   r   Ú
contextlibr   Ú	getLoggerr.   rR   Úfloatr   Zdefault_prioritiesr   r\   rb   r   r   r   r   Ú<module>7   s&   

Q
/