| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import logging |
| 4 |
import os |
| 5 |
import platform |
| 6 |
import sys |
| 7 |
from types import ModuleType |
| 8 |
from typing import Any, Callable, cast |
| 9 |
|
| 10 |
from typing_extensions import Literal, TypeAlias, get_args |
| 11 |
|
| 12 |
from webview import WebViewException |
| 13 |
|
| 14 |
GUIType: TypeAlias = Literal['qt', 'gtk', 'cef', 'mshtml', 'edgechromium', 'android', 'cocoa'] |
| 15 |
GUI_TYPES = list(get_args(GUIType)) |
| 16 |
|
| 17 |
logger = logging.getLogger('pywebview') |
| 18 |
guilib: ModuleType | None = None |
| 19 |
forced_gui_: GUIType | None = None |
| 20 |
|
| 21 |
|
| 22 |
def initialize(forced_gui: GUIType | None = None): |
| 23 |
def import_android(): |
| 24 |
global guilib |
| 25 |
|
| 26 |
try: |
| 27 |
import webview.platforms.android as guilib |
| 28 |
|
| 29 |
logger.debug('Using Kivy') |
| 30 |
return True |
| 31 |
except (ImportError, ValueError): |
| 32 |
logger.exception('Kivy cannot be loaded') |
| 33 |
return False |
| 34 |
|
| 35 |
def import_gtk(): |
| 36 |
global guilib |
| 37 |
|
| 38 |
try: |
| 39 |
import webview.platforms.gtk as guilib |
| 40 |
|
| 41 |
logger.debug('Using GTK') |
| 42 |
return True |
| 43 |
except (ImportError, ValueError): |
| 44 |
logger.exception('GTK cannot be loaded') |
| 45 |
return False |
| 46 |
|
| 47 |
def import_qt(): |
| 48 |
global guilib |
| 49 |
|
| 50 |
try: |
| 51 |
import webview.platforms.qt as guilib |
| 52 |
|
| 53 |
return True |
| 54 |
except ImportError: |
| 55 |
logger.exception('QT cannot be loaded') |
| 56 |
return False |
| 57 |
|
| 58 |
def import_cocoa(): |
| 59 |
global guilib |
| 60 |
|
| 61 |
try: |
| 62 |
import webview.platforms.cocoa as guilib |
| 63 |
|
| 64 |
return True |
| 65 |
except ImportError: |
| 66 |
logger.exception('PyObjC cannot be loaded') |
| 67 |
|
| 68 |
return False |
| 69 |
|
| 70 |
def import_winforms(): |
| 71 |
global guilib |
| 72 |
|
| 73 |
try: |
| 74 |
import webview.platforms.winforms as guilib |
| 75 |
|
| 76 |
return True |
| 77 |
except ImportError: |
| 78 |
logger.exception('pythonnet cannot be loaded') |
| 79 |
return False |
| 80 |
|
| 81 |
def try_import(guis: list[Callable[[], Any]]) -> bool: |
| 82 |
while guis: |
| 83 |
import_func = guis.pop(0) |
| 84 |
|
| 85 |
if import_func(): |
| 86 |
return True |
| 87 |
|
| 88 |
return False |
| 89 |
|
| 90 |
global forced_gui_ |
| 91 |
|
| 92 |
if not forced_gui: |
| 93 |
forced_gui = 'qt' if 'KDE_FULL_SESSION' in os.environ else None |
| 94 |
forced_gui = cast( |
| 95 |
GUIType, |
| 96 |
os.environ['PYWEBVIEW_GUI'].lower() |
| 97 |
if 'PYWEBVIEW_GUI' in os.environ and os.environ['PYWEBVIEW_GUI'].lower() in GUI_TYPES |
| 98 |
else forced_gui, |
| 99 |
) |
| 100 |
|
| 101 |
forced_gui_ = forced_gui |
| 102 |
|
| 103 |
if platform.system() == 'Darwin': |
| 104 |
if forced_gui == 'qt': |
| 105 |
guis = [import_qt, import_cocoa] |
| 106 |
else: |
| 107 |
guis = [import_cocoa, import_qt] |
| 108 |
|
| 109 |
if not try_import(guis): |
| 110 |
raise WebViewException( |
| 111 |
'You must have either PyObjC (for Cocoa support) or Qt with Python bindings installed in order to use pywebview.' |
| 112 |
) |
| 113 |
|
| 114 |
elif hasattr(sys, 'getandroidapilevel'): |
| 115 |
try_import([import_android]) |
| 116 |
|
| 117 |
elif platform.system() == 'Linux' or platform.system() == 'OpenBSD': |
| 118 |
if forced_gui == 'qt': |
| 119 |
guis = [import_qt, import_gtk] |
| 120 |
else: |
| 121 |
guis = [import_gtk, import_qt] |
| 122 |
|
| 123 |
if not try_import(guis): |
| 124 |
raise WebViewException( |
| 125 |
'You must have either QT or GTK with Python extensions installed in order to use pywebview.' |
| 126 |
) |
| 127 |
|
| 128 |
elif platform.system() == 'Windows': |
| 129 |
if forced_gui == 'qt': |
| 130 |
guis = [import_qt, import_winforms] |
| 131 |
else: |
| 132 |
guis = [import_winforms] |
| 133 |
|
| 134 |
if not try_import(guis): |
| 135 |
raise WebViewException('You must have pythonnet installed in order to use pywebview.') |
| 136 |
else: |
| 137 |
raise WebViewException( |
| 138 |
'Unsupported platform. Only Windows, Linux, OS X, OpenBSD are supported.' |
| 139 |
) |
| 140 |
|
| 141 |
guilib.setup_app() |
| 142 |
return guilib |