1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  
     | 
    
      """
**pyqode.qt** is a shim over the various qt bindings. It is used to write
qt bindings indenpendent library or application.
The shim will automatically select the first available API (PyQt5, PyQt4 and
finally PySide).
You can force the use of one specific bindings (e.g. if your application is
using one specific bindings and you need to use library that use pyqode.qt) by
setting up the ``QT_API`` environment variable.
PyQt5
+++++
For pyqt5, you don't have to set anything as it will be used automatically::
    >>> from pyqode.qt import QtGui, QtWidgets, QtCore
    >>> print(QtWidgets.QWidget)
PyQt4
+++++
Set the ``QT_API`` environment variable to 'PyQt4' (case insensitive) before
importing any python package::
    >>> import os
    >>> os.environ['QT_API'] = 'PyQt4'
    >>> from pyqode.qt import QtGui, QtWidgets, QtCore
    >>> print(QtWidgets.QWidget)
.. warning:: This requires to set the SIP api to version 2 (for strings and
    covariants). If you're using python2 you have to make sure the correct sip
    api is set before importing any PyQt4 module (pyqode.qt can take care of
    that for you but it must be imported before any PyQt4 module).
PySide
++++++
Set the QT_API environment variable to 'PySide' (case insensitive) before
importing pyqode::
    >>> import os
    >>> os.environ['QT_API'] = 'PySide'
    >>> from pyqode.qt import QtGui, QtWidgets, QtCore
    >>> print(QtWidgets.QWidget)
"""
import os
import sys
import logging
__version__ = '2.10.0'
#: Qt API environment variable name
QT_API = 'QT_API'
#: names of the expected PyQt5 api
PYQT5_API = ['pyqt5']
#: names of the expected PyQt4 api
PYQT4_API = [
    'pyqt',  # name used in IPython.qt
    'pyqt4'  # pyqode.qt original name
]
#: names of the expected PySide api
PYSIDE_API = ['pyside']
class PythonQtError(Exception):
    """
    Error raise if no bindings could be selected
    """
    pass
def setup_apiv2():
    """
    Setup apiv2 when using PyQt4 and Python2.
    """
    # setup PyQt api to version 2
    if sys.version_info[0] == 2:
        logging.getLogger(__name__).debug(
            'setting up SIP API to version 2')
        try:
            from PyQt5 import sip
        except:
            import sip
        try:
            sip.setapi("QString", 2)
            sip.setapi("QVariant", 2)
        except ValueError:
            logging.getLogger(__name__).critical(
                "failed to set up sip api to version 2 for PyQt4")
            raise ImportError('PyQt4')
def autodetect():
    """
    Auto-detects and use the first available QT_API by importing them in the
    following order:
    1) PyQt5
    2) PyQt4
    3) PySide
    """
    logging.getLogger(__name__).debug('auto-detecting QT_API')
    try:
        logging.getLogger(__name__).debug('trying PyQt5')
        import PyQt5
        os.environ[QT_API] = PYQT5_API[0]
        logging.getLogger(__name__).debug('imported PyQt5')
    except ImportError:
        try:
            logging.getLogger(__name__).debug('trying PyQt4')
            setup_apiv2()
            import PyQt4
            os.environ[QT_API] = PYQT4_API[0]
            logging.getLogger(__name__).debug('imported PyQt4')
        except ImportError:
            try:
                logging.getLogger(__name__).debug('trying PySide')
                import PySide
                os.environ[QT_API] = PYSIDE_API[0]
                logging.getLogger(__name__).debug('imported PySide')
            except ImportError:
                raise PythonQtError('No Qt bindings could be found')
if QT_API in os.environ:
    # check if the selected QT_API is available
    try:
        if os.environ[QT_API].lower() in PYQT5_API:
            logging.getLogger(__name__).debug('importing PyQt5')
            import PyQt5
            os.environ[QT_API] = PYQT5_API[0]
            logging.getLogger(__name__).debug('imported PyQt5')
        elif os.environ[QT_API].lower() in PYQT4_API:
            logging.getLogger(__name__).debug('importing PyQt4')
            setup_apiv2()
            import PyQt4
            os.environ[QT_API] = PYQT4_API[0]
            logging.getLogger(__name__).debug('imported PyQt4')
        elif os.environ[QT_API].lower() in PYSIDE_API:
            logging.getLogger(__name__).debug('importing PySide')
            import PySide
            os.environ[QT_API] = PYSIDE_API[0]
            logging.getLogger(__name__).debug('imported PySide')
    except ImportError:
        logging.getLogger(__name__).warning(
            'failed to import the selected QT_API: %s',
            os.environ[QT_API])
        # use the auto-detected API if possible
        autodetect()
else:
    # user did not select a qt api, let's perform auto-detection
    autodetect()
logging.getLogger(__name__).info('using %s' % os.environ[QT_API])
 
     |