a
    ø£Beö,  ã                   @   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 ddl	Z	dZ
dZdZdZdZd	Zd
ZdZe
dededededededediZG dd„ dejjƒZeƒ Zej de¡ dd„ Zdd„ 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S )0aD  
This module contains factory functions that attempt
to return Qt submodules from the various python Qt bindings.

It also protects against double-importing Qt with different
bindings, which is unstable and likely to crash

This is used primarily by qt and qt_for_kernel, and shouldn't
be accessed directly from the outside
é    N)ÚpartialÚ	lru_cacheZpyqt6Zpyside6Zpyqt5Zpyside2ZpyqtZpyqtv1ZpysideZpyqtdefaultÚPyQt6ÚPySide6ÚPyQt5ÚPySide2ÚPySideÚPyQt4c                   @   s*   e Zd ZdZdd„ Zdd„ Zd	dd„ZdS )
ÚImportDenierzfImport Hook that will guard against bad Qt imports
    once IPython commits to a specific binding
    c                 C   s   t ƒ | _d S ©N)ÚsetÚ_ImportDenier__forbidden)Úself© r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/IPython/external/qt_loaders.pyÚ__init__8   s    zImportDenier.__init__c                 C   s   t j |d ¡ | j |¡ d S r   )ÚsysÚmodulesÚpopr   Úadd)r   Úmodule_namer   r   r   Úforbid;   s    zImportDenier.forbidNc                 C   s(   |rd S || j v r$td|tƒ f ƒ‚d S )Nzi
    Importing %s disabled by IPython, which has
    already imported an Incompatible QT Binding: %s
    )r   ÚImportErrorÚ
loaded_api)r   ÚfullnameÚpathÚtargetr   r   r   Ú	find_spec?   s    
üÿzImportDenier.find_spec)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r
   3   s   r
   c                 C   s2   t t ¡ ƒ}| t|  ¡ |D ]}t |¡ qdS )zXCommit to a particular API, and trigger ImportErrors on subsequent
    dangerous importsN)r   Úapi_to_moduleÚvaluesÚremoveÚIDr   )Úapir   Úmodr   r   r   Ú
commit_apiP   s    r(   c                   C   st   t j d¡rtS t j d¡r tS t j d¡r0tS t j d¡r@tS t j d¡r`tƒ dkrZtS t	S nt j d¡rpt
S dS )	zçReturn which API is loaded, if any

    If this returns anything besides None,
    importing any other Qt binding is unsafe.

    Returns
    -------
    None, 'pyside6', 'pyqt6', 'pyside2', 'pyside', 'pyqt', 'pyqt5', 'pyqtv1'
    zPyQt6.QtCorezPySide6.QtCorezPyQt5.QtCorezPySide2.QtCorezPyQt4.QtCoreé   zPySide.QtCoreN)r   r   ÚgetÚQT_API_PYQT6ÚQT_API_PYSIDE6ÚQT_API_PYQT5ÚQT_API_PYSIDE2Úqtapi_versionÚQT_API_PYQTÚQT_API_PYQTv1ÚQT_API_PYSIDEr   r   r   r   r   Z   s    

r   c              	   C   s˜   t |  }ddlm} g d¢}| ttttfv r6| d¡ |D ]>}z|d||f ƒ}W n tyh   Y  dS 0 |du r: dS q:| t	kr”ddl
}|jdkS d	S )
a5  Safely check for PyQt4/5, PySide or PySide2, without importing submodules

    Parameters
    ----------
    api : str [ 'pyqtv1' | 'pyqt' | 'pyqt5' | 'pyside' | 'pyside2' | 'pyqtdefault']
        Which module to check for

    Returns
    -------
    True if the relevant module appears to be importable
    r   )r   )ÚQtCoreÚQtGuiÚQtSvgÚ	QtWidgetsz%s.%sFN)é   r   é   T)r"   Zimportlib.utilr   r-   r.   r+   r,   Úappendr   r2   r   Ú__version_info__)r&   r   r   ÚrequiredZsubmodÚspecr   r   r   r   Úhas_bindingw   s     


r=   c                  C   sn   zddl } W n: tyF   zddlm }  W n ty@   Y Y dS 0 Y n0 z|  d¡W S  tyh   Y dS 0 dS )z„Return which QString API has been set, if any

    Returns
    -------
    The QString API version (1 or 2), or None if not set
    r   N©ÚsipÚQString)r?   r   r   ÚgetapiÚ
ValueErrorr>   r   r   r   r/   Ÿ   s    r/   c                 C   s6   t | ƒsdS tƒ }| tkr&|tdfv S || dfv S dS )z?Safely query whether an API is importable, without importing itFN)r=   r   ÚQT_API_PYQT_DEFAULTr+   )r&   Úcurrentr   r   r   Ú
can_importµ   s    rE   r)   c                 C   sŠ   ddl }| dur(| d| ¡ | d| ¡ ddlm}m}m} |jdk rTtd|j ƒ‚|j	|_
|j|_| d¡} | dkrztnt}||||fS )	zë
    Import PyQt4

    Parameters
    ----------
    version : 1, 2, or None
        Which QString/QVariant API to use. Set to None to use the system
        default
    ImportErrors raised within this function are non-recoverable
    r   Nr@   ZQVariant©r4   r3   r5   i  z'IPython requires PyQt4 >= 4.7, found %sr7   )r?   Zsetapir	   r4   r3   r5   ZPYQT_VERSIONr   ZPYQT_VERSION_STRÚ
pyqtSignalÚSignalÚpyqtSlotÚSlotrA   r1   r0   )Úversionr?   r4   r3   r5   r&   r   r   r   Úimport_pyqt4Á   s    
ÿ
rL   c                  C   s^   ddl m} m}m}m} | j| _| j| _t	 
d¡}|j |j¡ |j |j¡ t}| |||fS )zX
    Import PyQt5

    ImportErrors raised within this function are non-recoverable
    r   ©r3   r5   r6   r4   ÚQtGuiCompat)r   r3   r5   r6   r4   rG   rH   rI   rJ   ÚtypesÚ
ModuleTypeÚ__dict__Úupdater-   ©r3   r5   r6   r4   rN   r&   r   r   r   Úimport_pyqt5å   s    
rT   c                  C   s^   ddl m} m}m}m} | j| _| j| _t	 
d¡}|j |j¡ |j |j¡ t}| |||fS )zX
    Import PyQt6

    ImportErrors raised within this function are non-recoverable
    r   rM   rN   )r   r3   r5   r6   r4   rG   rH   rI   rJ   rO   rP   rQ   rR   r+   rS   r   r   r   Úimport_pyqt6û   s    
rU   c                  C   s    ddl m} m}m} || |tfS )zY
    Import PySide

    ImportErrors raised within this function are non-recoverable
    r   rF   )r   r4   r3   r5   r2   rF   r   r   r   Úimport_pyside  s    rV   c                  C   s\   ddl m} m}m}m}m} t d¡}|j 	| j¡ |j 	|j¡ |j 	|j¡ |||t
fS )zZ
    Import PySide2

    ImportErrors raised within this function are non-recoverable
    r   ©r4   r3   r5   r6   ÚQtPrintSupportrN   )r   r4   r3   r5   r6   rX   rO   rP   rQ   rR   r.   ©r4   r3   r5   r6   rX   rN   r   r   r   Úimport_pyside2  s    
rZ   c                  C   s\   ddl m} m}m}m}m} t d¡}|j 	| j¡ |j 	|j¡ |j 	|j¡ |||t
fS )zZ
    Import PySide6

    ImportErrors raised within this function are non-recoverable
    r   rW   rN   )r   r4   r3   r5   r6   rX   rO   rP   rQ   rR   r,   rY   r   r   r   Úimport_pyside6+  s    
r[   c                 C   sÌ   t ttttttttt	t
ttttdd�tti}| D ]Z}||vr`td|d dd„ | ¡ D ƒ¡f ƒ‚t|ƒsjq0|| ƒ }|d }t|ƒ |  S dtjv ržtjd= td	tƒ ttƒtt ƒttƒttƒ| f ƒ‚d
S )a½  
    Attempt to import Qt, given a preference list
    of permissible bindings

    It is safe to call this function multiple times.

    Parameters
    ----------
    api_options : List of strings
        The order of APIs to try. Valid items are 'pyside', 'pyside2',
        'pyqt', 'pyqt5', 'pyqtv1' and 'pyqtdefault'

    Returns
    -------
    A tuple of QtCore, QtGui, QtSvg, QT_API
    The first three are the Qt modules. The last is the
    string indicating which module was loaded.

    Raises
    ------
    ImportError, if it isn't possible to import any requested
    bindings (either because they aren't installed, or because
    an incompatible library has already been installed)
    r7   )rK   z'Invalid Qt API %r, valid values are: %sz, c                 S   s   g | ]}d | ‘qS )z%rr   )Ú.0Úkr   r   r   Ú
<listcomp>i  ó    zload_qt.<locals>.<listcomp>éÿÿÿÿÚQT_APIaQ  
    Could not load requested Qt binding. Please ensure that
    PyQt4 >= 4.7, PyQt5, PyQt6, PySide >= 1.0.3, PySide2, or
    PySide6 is available, and only one is imported per session.

    Currently-imported Qt library:                              %r
    PyQt5 available (requires QtCore, QtGui, QtSvg, QtWidgets): %s
    PyQt6 available (requires QtCore, QtGui, QtSvg, QtWidgets): %s
    PySide2 installed:                                          %s
    PySide6 installed:                                          %s
    Tried to load:                                              %r
    N)r+   rU   r,   r[   r-   rT   r.   rZ   r2   rV   r0   rL   r1   r   rC   ÚRuntimeErrorÚjoinÚkeysrE   r(   ÚosÚenvironr   r   r=   )Zapi_optionsÚloadersr&   Úresultr   r   r   Úload_qt<  sF    ôÿÿ

úôÿri   c                    s   t dƒ‡ ‡fdd„ƒ}|S )z@Construct an enum helper to account for PyQt5 <-> PyQt6 changes.Nc                    s,   t  ˆ tkr| n|  d¡d ¡tjˆj ƒS )NÚ.r   )ÚoperatorÚ
attrgetterr+   Ú
rpartitionr   r   Ú__package__)Úname©ra   r3   r   r   Ú_enum“  s
    ÿ
þzenum_factory.<locals>._enum)r   )ra   r3   rq   r   rp   r   Úenum_factory�  s    rr   )r)   )&r!   Úimportlib.abcÚ	importlibr   re   rO   Ú	functoolsr   r   rk   r+   r,   r-   r.   r0   r1   r2   rC   r"   ÚabcÚMetaPathFinderr
   r%   Ú	meta_pathÚinsertr(   r   r=   r/   rE   rL   rT   rU   rV   rZ   r[   ri   rr   r   r   r   r   Ú<module>   sN   
ô
(
$	T