|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 try: |
|
7 import PyQt5 # __IGNORE_WARNING__ |
|
8 except ImportError: |
|
9 import sys |
|
10 |
|
11 # TODO: adapt this for Python2 |
|
12 class PyQt4Importer(object): |
|
13 def __init__(self): |
|
14 """ |
|
15 Constructor |
|
16 """ |
|
17 self.__path = None |
|
18 |
|
19 def find_module(self, fullname, path=None): |
|
20 """ |
|
21 Public method returning the module loader. |
|
22 |
|
23 @param fullname name of the module to be loaded (string) |
|
24 @param path path to resolve the module name (string) |
|
25 @return module loader object |
|
26 """ |
|
27 if fullname.startswith("PyQt5"): |
|
28 self.__path = path |
|
29 return self |
|
30 |
|
31 return None |
|
32 |
|
33 def load_module(self, fullname): |
|
34 """ |
|
35 Public method to load a module. |
|
36 |
|
37 @param fullname name of the module to be loaded (string) |
|
38 @return reference to the loaded module (module) |
|
39 """ |
|
40 if fullname in ["PyQt5.QtWidgets", "PyQt5.QtPrintSupport"]: |
|
41 newname = "PyQt4.QtGui" |
|
42 elif fullname in ["PyQt5.QtWebKitWidgets"]: |
|
43 newname = "PyQt4.QtWebKit" |
|
44 else: |
|
45 newname = fullname.replace("PyQt5", "PyQt4") |
|
46 |
|
47 import importlib |
|
48 loader = importlib.find_loader(newname, self.__path) |
|
49 module = loader.load_module(newname) |
|
50 sys.modules[fullname] = module |
|
51 if fullname == "PyQt5.QtCore": |
|
52 import PyQt4.QtGui |
|
53 module.qInstallMessageHandler = module.qInstallMsgHandler |
|
54 module.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel |
|
55 module.QItemSelection = PyQt4.QtGui.QItemSelection |
|
56 module.QSortFilterProxyModel = \ |
|
57 PyQt4.QtGui.QSortFilterProxyModel |
|
58 module.QAbstractProxyModel = PyQt4.QtGui.QAbstractProxyModel |
|
59 module.QStringListModel = PyQt4.QtGui.QStringListModel |
|
60 return module |
|
61 |
|
62 sys.meta_path.insert(0, PyQt4Importer()) |