Toolbox/PyQt4ImportHook.py

Mon, 22 Dec 2014 18:36:37 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 22 Dec 2014 18:36:37 +0100
changeset 3991
73731c4bf5bd
parent 3937
607dd5b0079b
child 4021
195a471c327b
permissions
-rw-r--r--

Corrected some coding style issues.

3776
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de>
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
3991
73731c4bf5bd Corrected some coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3937
diff changeset
6 """
73731c4bf5bd Corrected some coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3937
diff changeset
7 Module implementing an import hook converting PyQt5 imports to PyQt4 imports.
73731c4bf5bd Corrected some coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3937
diff changeset
8 """
73731c4bf5bd Corrected some coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3937
diff changeset
9
3936
93fb2fd523f1 Completed the PyQt4 override installation and startup.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3857
diff changeset
10 import sys
3776
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 try:
3936
93fb2fd523f1 Completed the PyQt4 override installation and startup.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3857
diff changeset
12 if "--pyqt4" in sys.argv:
3937
607dd5b0079b Refined the PyQt4 compatibility stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3936
diff changeset
13 sys.argv.remove("--pyqt4")
3936
93fb2fd523f1 Completed the PyQt4 override installation and startup.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3857
diff changeset
14 # fake a failed PyQt5 import
93fb2fd523f1 Completed the PyQt4 override installation and startup.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3857
diff changeset
15 raise ImportError
3776
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 import PyQt5 # __IGNORE_WARNING__
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 except ImportError:
3777
0c47cbb5b199 Modified the PyQt4Importer to work with Python2 as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3776
diff changeset
18 import importlib
3776
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 class PyQt4Importer(object):
3991
73731c4bf5bd Corrected some coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3937
diff changeset
21 """
73731c4bf5bd Corrected some coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3937
diff changeset
22 Class implementing an importer converting PyQt5 imports to PyQt4
73731c4bf5bd Corrected some coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3937
diff changeset
23 imports.
73731c4bf5bd Corrected some coding style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3937
diff changeset
24 """
3776
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 def __init__(self):
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Constructor
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 self.__path = None
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 def find_module(self, fullname, path=None):
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 """
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 Public method returning the module loader.
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @param fullname name of the module to be loaded (string)
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 @param path path to resolve the module name (string)
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 @return module loader object
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 """
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 if fullname.startswith("PyQt5"):
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 self.__path = path
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 return self
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 return None
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 def load_module(self, fullname):
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 """
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 Public method to load a module.
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @param fullname name of the module to be loaded (string)
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 @return reference to the loaded module (module)
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 """
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 if fullname in ["PyQt5.QtWidgets", "PyQt5.QtPrintSupport"]:
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 newname = "PyQt4.QtGui"
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 elif fullname in ["PyQt5.QtWebKitWidgets"]:
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 newname = "PyQt4.QtWebKit"
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 else:
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 newname = fullname.replace("PyQt5", "PyQt4")
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
3777
0c47cbb5b199 Modified the PyQt4Importer to work with Python2 as well.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3776
diff changeset
59 module = importlib.import_module(newname)
3776
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 sys.modules[fullname] = module
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 if fullname == "PyQt5.QtCore":
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 import PyQt4.QtGui
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 module.qInstallMessageHandler = module.qInstallMsgHandler
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 module.QItemSelectionModel = PyQt4.QtGui.QItemSelectionModel
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 module.QItemSelection = PyQt4.QtGui.QItemSelection
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 module.QSortFilterProxyModel = \
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 PyQt4.QtGui.QSortFilterProxyModel
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 module.QAbstractProxyModel = PyQt4.QtGui.QAbstractProxyModel
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 module.QStringListModel = PyQt4.QtGui.QStringListModel
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 return module
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
ccb6eacb50e5 Started adding code to make eric6 usable with PyQt5 and PyQt4.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 sys.meta_path.insert(0, PyQt4Importer())
3857
f8b548d399bc Python2 compatibility with PyQt4 restored.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3777
diff changeset
73
f8b548d399bc Python2 compatibility with PyQt4 restored.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3777
diff changeset
74 if sys.version_info[0] == 2:
f8b548d399bc Python2 compatibility with PyQt4 restored.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3777
diff changeset
75 import sip
f8b548d399bc Python2 compatibility with PyQt4 restored.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3777
diff changeset
76 sip.setapi('QString', 2)
f8b548d399bc Python2 compatibility with PyQt4 restored.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3777
diff changeset
77 sip.setapi('QVariant', 2)
f8b548d399bc Python2 compatibility with PyQt4 restored.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3777
diff changeset
78 sip.setapi('QTextStream', 2)

eric ide

mercurial