File: __init__.py

package info (click to toggle)
python-anyqt 0.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: python: 4,087; makefile: 192; sh: 3
file content (72 lines) | stat: -rw-r--r-- 1,977 bytes parent folder | download | duplicates (2)
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
import sys

__PREFERRED_API = None
__SELECTED_API = None

#: A string indicating which Qt api is used (will be `None` *until* a api is
#: selected and commited to.
USED_API = None


def setpreferredapi(api):
    """
    Set the preferred Qt API.

    Will raise a RuntimeError if a Qt API was already selected.

    Note that QT_API environment variable (if set) will take precedence.
    """
    global __PREFERRED_API
    if __SELECTED_API is not None:
        raise RuntimeError("A Qt api {} was already selected"
                           .format(__SELECTED_API))

    if api.lower() not in {"pyqt4", "pyqt5", "pyside", "pyside2"}:
        raise ValueError(api)
    __PREFERRED_API = api.lower()


def selectapi(api):
    """
    Select an Qt API to use.

    This can only be set once and before any of the Qt modules are explicitly
    imported.
    """
    global __SELECTED_API, USED_API
    if api.lower() not in {"pyqt4", "pyqt5", "pyside", "pyside2"}:
        raise ValueError(api)

    if __SELECTED_API is not None and __SELECTED_API.lower() != api.lower():
        raise RuntimeError("A Qt API {} was already selected"
                           .format(__SELECTED_API))
    elif __SELECTED_API is None:
        __SELECTED_API = api.lower()
        from . import _api
        USED_API = _api.USED_API


if sys.version_info < (3, 4):
    import imp as _imp
    def __islocatable(name):
        try:
            _imp.find_module(name)
        except ImportError:
            return False
        else:
            return True
else:
    import importlib.util as _importlibutil
    def __islocatable(name):
        try:
            return _importlibutil.find_spec(name) is not None
        except (ValueError, ImportError):
            return False


def availableapi():
    """
    Return a list of available Qt interfaces.
    """
    search = ["PyQt5", "PyQt6", "PyQt4", "PySide2", "PySide6", "PySide"]
    return [name for name in search if __islocatable(name)]