o
    ZhW.                     @   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   R/var/www/html/lang_env/lib/python3.10/site-packages/IPython/external/qt_loaders.py__init__9   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_specr   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r
   4   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_apiQ   s
   r(   c                   C   sr   t jdrtS t jdrtS t jdrtS t jdr tS t jdr/t dkr-tS t	S t jdr7t
S dS )	zReturn 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   [   s   

r   c              	   C   s   t |  }ddlm} g d}| ttttfv r|d |D ]}z
|d||f }W n ty4   Y  dS w |du r< dS q| t	krJd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"   importlib.utilr   r-   r.   r+   r,   appendr   r2   r   __version_info__)r&   r   r   requiredZsubmodspecr   r   r   r   has_bindingx   s$   

r>   c                  C   sj   zddl } W n ty#   zddlm }  W n ty    Y Y dS w Y nw z| dW S  ty4   Y dS w )zReturn 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   s2   t | sdS t }| tkr|tdfv S || dfv 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   rF   r)   c                 C   s   ddl }| dur|d|  |d|  ddlm}m}m} |jdk r*td|j |j	|_
|j|_|d} | dkr=t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   NrA   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SlotrB   r1   r0   )versionr@   r4   r3   r5   r&   r   r   r   import_pyqt4   s   

rM   c                  C   ^   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   rH   rI   rJ   rK   types
ModuleType__dict__updater-   r3   r5   r6   r4   rP   r&   r   r   r   import_pyqt5      
rV   c                  C   rN   )zX
    Import PyQt6

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

    ImportErrors raised within this function are non-recoverable
    r   rG   )r   r4   r3   r5   r2   rG   r   r   r   import_pyside  s   rY   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   QtPrintSupportrP   )r   r4   r3   r5   r6   r[   rQ   rR   rS   rT   r.   )r4   r3   r5   r6   r[   rP   r   r   r   import_pyside2  s   
r\   c                  C   s   dd } ddl m}m}m}m}m} td}|j	|j |j
dk r2|j	|j |j	|j n|j	| | |j	| | |||tfS )zZ
    Import PySide6

    ImportErrors raised within this function are non-recoverable
    c                    s    fddt  D S )Nc                    s"   i | ]}| d s|t |qS )_)
startswithgetattr).0namemoduler   r   
<dictcomp>4  s    
z5import_pyside6.<locals>.get_attrs.<locals>.<dictcomp>)dirrb   r   rb   r   	get_attrs3  s   
z!import_pyside6.<locals>.get_attrsr   rZ   rP   )      )r   r4   r3   r5   r6   r[   rQ   rR   rS   rT   r;   r,   )rf   r4   r3   r5   r6   r[   rP   r   r   r   import_pyside6,  s   

ri   c                 C   s   t ttttttttt	t
ttttddtti}| D ]-}||vr0td|ddd | D f t|s5q||  }|d }t| |  S dtjv rOtjd= td	t tttt tttt| f )
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   )rL   z'Invalid Qt API %r, valid values are: %sz, c                 S   s   g | ]}d | qS )z%rr   )r`   kr   r   r   
<listcomp>v  s    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
    )r+   rX   r,   ri   r-   rV   r.   r\   r2   rY   r0   rM   r1   r   rD   RuntimeErrorjoinkeysrF   r(   osenvironr   r   r>   )Zapi_optionsloadersr&   resultr   r   r   load_qtI  sF   

ru   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__)ra   rm   r3   r   r   _enum  s
   
zenum_factory.<locals>._enum)r   )rm   r3   r|   r   r{   r   enum_factory  s   r}   )r)   )&r!   importlib.abc	importlibr   rq   rQ   	functoolsr   r   rw   r+   r,   r-   r.   r0   r1   r2   rD   r"   abcMetaPathFinderr
   r%   	meta_pathinsertr(   r   r>   r/   rF   rM   rV   rX   rY   r\   ri   ru   r}   r   r   r   r   <module>   sP    
(
$	T