<commit_before>"""
Provide a common way to import Qt classes used by pytest-qt in a unique manner,
abstracting API differences between PyQt4, PyQt5 and PySide.

.. note:: This module is not part of pytest-qt public API, hence its interface
may change between releases and users should not rely on it.

Based on from https://github.com/epage/PythonUtils.
"""

from __future__ import with_statement
from __future__ import division
from collections import namedtuple
import os

on_rtd = os.environ.get('READTHEDOCS', None) == 'True'

if not on_rtd:  # pragma: no cover

    def _try_import(name):
        try:
            __import__(name)
            return True
        except ImportError:
            return False

    def _guess_qt_api():
        if _try_import('PySide'):
            return 'pyside'
        elif _try_import('PyQt4'):
            return 'pyqt4'
        elif _try_import('PyQt5'):
            return 'pyqt5'
        else:
            msg = 'pytest-qt requires either PySide, PyQt4 or PyQt5 to be installed'
            raise ImportError(msg)

    # backward compatibility support: PYTEST_QT_FORCE_PYQT
    if os.environ.get('PYTEST_QT_FORCE_PYQT', 'false') == 'true':
        QT_API = 'pyqt4'
    else:
        QT_API = os.environ.get('PYTEST_QT_API')
        if QT_API is not None:
            QT_API = QT_API.lower()
            if QT_API not in ('pyside', 'pyqt4', 'pyqt5'):
                msg = 'Invalid value for $PYTEST_QT_API: %s'
                raise RuntimeError(msg % QT_API)
        else:
            QT_API = _guess_qt_api()

    # backward compatibility
    USING_PYSIDE = QT_API == 'pyside'

    def _import_module(module_name):
        m = __import__(_root_module, globals(), locals(), [module_name], 0)
        return getattr(m, module_name)

    _root_modules = {
        'pyside': 'PySide',
        'pyqt4': 'PyQt4',
        'pyqt5': 'PyQt5',
    }
    _root_module = _root_modules[QT_API]

    QtCore = _import_module('QtCore')
    QtGui = _import_module('QtGui')
    QtTest = _import_module('QtTest')
    Qt = QtCore.Qt
    QEvent = QtCore.QEvent

    qDebug = QtCore.qDebug
    qWarning = QtCore.qWarning
    qCritical = QtCore.qCritical
    qFatal = QtCore.qFatal
    QtDebugMsg = QtCore.QtDebugMsg
    QtWarningMsg = QtCore.QtWarningMsg
    QtCriticalMsg = QtCore.QtCriticalMsg
    QtFatalMsg = QtCore.QtFatalMsg

    # Qt4 and Qt5 have different functions to install a message handler;
    # the plugin will try to use the one that is not None
    qInstallMsgHandler = None
    qInstallMessageHandler = None

    VersionTuple = namedtuple('VersionTuple',
                              'qt_api, qt_api_version, runtime, compiled')

    if QT_API == 'pyside':
        import PySide
        Signal = QtCore.Signal
        Slot = QtCore.Slot
        Property = QtCore.Property
        QApplication = QtGui.QApplication
        QWidget = QtGui.QWidget
        qInstallMsgHandler = QtCore.qInstallMsgHandler

        QStandardItem = QtGui.QStandardItem
        QStandardItemModel = QtGui.QStandardItemModel
        QFileSystemModel = QtGui.QFileSystemModel
        QStringListModel = QtGui.QStringListModel
        QSortFilterProxyModel = QtGui.QSortFilterProxyModel
        QAbstractListModel = QtCore.QAbstractListModel

        def cast(obj, typ):
            """no cast operation is available in PySide"""
            return obj

        def extract_from_variant(variant):
            """returns python object from the given QVariant"""
            return variant

        def get_versions():
            return VersionTuple('PySide', PySide.__version__, QtCore.qVersion(),
                                QtCore.__version__)

    elif QT_API in ('pyqt4', 'pyqt5'):
        import sip
        Signal = QtCore.pyqtSignal
        Slot = QtCore.pyqtSlot
        Property = QtCore.pyqtProperty

        if QT_API == 'pyqt5':
            _QtWidgets = _import_module('QtWidgets')
            QApplication = _QtWidgets.QApplication
            QWidget = _QtWidgets.QWidget
            qInstallMessageHandler = QtCore.qInstallMessageHandler
            qt_api_name = 'PyQt5'

            QStandardItem = QtGui.QStandardItem
            QStandardItemModel = QtGui.QStandardItemModel
            QFileSystemModel = _QtWidgets.QFileSystemModel
            QStringListModel = QtCore.QStringListModel
            QSortFilterProxyModel = QtCore.QSortFilterProxyModel
            QAbstractListModel = QtCore.QAbstractListModel

        else:
            QApplication = QtGui.QApplication
            QWidget = QtGui.QWidget
            qInstallMsgHandler = QtCore.qInstallMsgHandler
            qt_api_name = 'PyQt4'

            QStandardItem = QtGui.QStandardItem
            QStandardItemModel = QtGui.QStandardItemModel
            QFileSystemModel = QtGui.QFileSystemModel
            QStringListModel = QtGui.QStringListModel
            QSortFilterProxyModel = QtGui.QSortFilterProxyModel
            QAbstractListModel = QtCore.QAbstractListModel


        def get_versions():
            return VersionTuple(qt_api_name, QtCore.PYQT_VERSION_STR,
                                QtCore.qVersion(), QtCore.QT_VERSION_STR)

        def cast(obj, typ):
            """casts from a subclass to a parent class"""
            return sip.cast(obj, typ)

        def extract_from_variant(variant):
            """returns python object from the given QVariant"""
            return variant.toPyObject() if variant is not None else None

else:  # pragma: no cover
    USING_PYSIDE = True

    # mock Qt when we are generating documentation at readthedocs.org<commit_msg>Refactor some common PyQt4 and PyQt5 imports<commit_after>"""
Provide a common way to import Qt classes used by pytest-qt in a unique manner,
abstracting API differences between PyQt4, PyQt5 and PySide.

.. note:: This module is not part of pytest-qt public API, hence its interface
may change between releases and users should not rely on it.

Based on from https://github.com/epage/PythonUtils.
"""

from __future__ import with_statement
from __future__ import division
from collections import namedtuple
import os

on_rtd = os.environ.get('READTHEDOCS', None) == 'True'

if not on_rtd:  # pragma: no cover

    def _try_import(name):
        try:
            __import__(name)
            return True
        except ImportError:
            return False

    def _guess_qt_api():
        if _try_import('PySide'):
            return 'pyside'
        elif _try_import('PyQt4'):
            return 'pyqt4'
        elif _try_import('PyQt5'):
            return 'pyqt5'
        else:
            msg = 'pytest-qt requires either PySide, PyQt4 or PyQt5 to be installed'
            raise ImportError(msg)

    # backward compatibility support: PYTEST_QT_FORCE_PYQT
    if os.environ.get('PYTEST_QT_FORCE_PYQT', 'false') == 'true':
        QT_API = 'pyqt4'
    else:
        QT_API = os.environ.get('PYTEST_QT_API')
        if QT_API is not None:
            QT_API = QT_API.lower()
            if QT_API not in ('pyside', 'pyqt4', 'pyqt5'):
                msg = 'Invalid value for $PYTEST_QT_API: %s'
                raise RuntimeError(msg % QT_API)
        else:
            QT_API = _guess_qt_api()

    # backward compatibility
    USING_PYSIDE = QT_API == 'pyside'

    def _import_module(module_name):
        m = __import__(_root_module, globals(), locals(), [module_name], 0)
        return getattr(m, module_name)

    _root_modules = {
        'pyside': 'PySide',
        'pyqt4': 'PyQt4',
        'pyqt5': 'PyQt5',
    }
    _root_module = _root_modules[QT_API]

    QtCore = _import_module('QtCore')
    QtGui = _import_module('QtGui')
    QtTest = _import_module('QtTest')
    Qt = QtCore.Qt
    QEvent = QtCore.QEvent

    qDebug = QtCore.qDebug
    qWarning = QtCore.qWarning
    qCritical = QtCore.qCritical
    qFatal = QtCore.qFatal
    QtDebugMsg = QtCore.QtDebugMsg
    QtWarningMsg = QtCore.QtWarningMsg
    QtCriticalMsg = QtCore.QtCriticalMsg
    QtFatalMsg = QtCore.QtFatalMsg

    # Qt4 and Qt5 have different functions to install a message handler;
    # the plugin will try to use the one that is not None
    qInstallMsgHandler = None
    qInstallMessageHandler = None

    VersionTuple = namedtuple('VersionTuple',
                              'qt_api, qt_api_version, runtime, compiled')

    if QT_API == 'pyside':
        import PySide
        Signal = QtCore.Signal
        Slot = QtCore.Slot
        Property = QtCore.Property
        QApplication = QtGui.QApplication
        QWidget = QtGui.QWidget
        qInstallMsgHandler = QtCore.qInstallMsgHandler

        QStandardItem = QtGui.QStandardItem
        QStandardItemModel = QtGui.QStandardItemModel
        QFileSystemModel = QtGui.QFileSystemModel
        QStringListModel = QtGui.QStringListModel
        QSortFilterProxyModel = QtGui.QSortFilterProxyModel
        QAbstractListModel = QtCore.QAbstractListModel

        def cast(obj, typ):
            """no cast operation is available in PySide"""
            return obj

        def extract_from_variant(variant):
            """returns python object from the given QVariant"""
            return variant

        def get_versions():
            return VersionTuple('PySide', PySide.__version__, QtCore.qVersion(),
                                QtCore.__version__)

    elif QT_API in ('pyqt4', 'pyqt5'):
        import sip
        Signal = QtCore.pyqtSignal
        Slot = QtCore.pyqtSlot
        Property = QtCore.pyqtProperty

        if QT_API == 'pyqt5':
            qt_api_name = 'PyQt5'

            _QtWidgets = _import_module('QtWidgets')
            QApplication = _QtWidgets.QApplication
            QWidget = _QtWidgets.QWidget
            qInstallMessageHandler = QtCore.qInstallMessageHandler

            QFileSystemModel = _QtWidgets.QFileSystemModel
            QStringListModel = QtCore.QStringListModel
            QSortFilterProxyModel = QtCore.QSortFilterProxyModel
        else:
            qt_api_name = 'PyQt4'

            QApplication = QtGui.QApplication
            QWidget = QtGui.QWidget
            qInstallMsgHandler = QtCore.qInstallMsgHandler

            QFileSystemModel = QtGui.QFileSystemModel
            QStringListModel = QtGui.QStringListModel
            QSortFilterProxyModel = QtGui.QSortFilterProxyModel

        QStandardItem = QtGui.QStandardItem
        QStandardItemModel = QtGui.QStandardItemModel
        QAbstractListModel = QtCore.QAbstractListModel

        def get_versions():
            return VersionTuple(qt_api_name, QtCore.PYQT_VERSION_STR,
                                QtCore.qVersion(), QtCore.QT_VERSION_STR)

        def cast(obj, typ):
            """casts from a subclass to a parent class"""
            return sip.cast(obj, typ)

        def extract_from_variant(variant):
            """returns python object from the given QVariant"""
            return variant.toPyObject() if variant is not None else None

else:  # pragma: no cover
    USING_PYSIDE = True

    # mock Qt when we are generating documentation at readthedocs.org
{'fraction_non_alphanumeric': 0.07407407407407407, 'fraction_numerical': 0.004346644933441999, 'mean_word_length': 2.3466666666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '315258', 'n_tokens_mistral': 3281, 'n_tokens_neox': 3165, 'n_words': 817}