Skip to content

Commit

Permalink
update Python, allow overriding binding order
Browse files Browse the repository at this point in the history
  • Loading branch information
dirk-thomas committed Aug 2, 2013
1 parent cd65505 commit a6ef126
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
12 changes: 11 additions & 1 deletion src/python_qt_binding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

"""
Abstraction for different Python Qt bindings.
Expand All @@ -38,7 +39,16 @@
from python_qt_binding.QtCore import QObject
from python_qt_binding import QtGui, loadUi
All available modules are listed in QT_BINDING_MODULES.
The name of the selected binding is available in QT_BINDING.
The version of the selected binding is available in QT_BINDING_VERSION.
All available Qt modules are listed in QT_BINDING_MODULES.
The default binding order ('pyqt', 'pyside') can be overridden with a
SELECT_QT_BINDING_ORDER attribute on sys:
setattr(sys, 'SELECT_QT_BINDING_ORDER', [FIRST_NAME, NEXT_NAME, ..])
A specific binding can be selected with a SELECT_QT_BINDING attribute on sys:
setattr(sys, 'SELECT_QT_BINDING', MY_BINDING_NAME)
"""

import sys
Expand Down
39 changes: 22 additions & 17 deletions src/python_qt_binding/binding_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,28 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import __builtin__
import os
import sys
import __builtin__


def _select_qt_binding(binding_name=None):
QT_BINDING = None
QT_BINDING_MODULES = {}
QT_BINDING_VERSION = None


def _select_qt_binding(binding_name=None, binding_order=None):
global QT_BINDING, QT_BINDING_VERSION

# order of default bindings can be changed here
DEFAULT_BINDING_ORDER = ['pyqt', 'pyside']
binding_order = binding_order or DEFAULT_BINDING_ORDER

# determine binding preference
if binding_name:
if binding_name not in DEFAULT_BINDING_ORDER:
raise ImportError('Qt binding "%s" is unknown' % binding_name)
DEFAULT_BINDING_ORDER = [binding_name]
if binding_name not in binding_order:
raise ImportError("Qt binding '%s' is unknown" % binding_name)
binding_order = [binding_name]

required_modules = [
'QtCore',
Expand All @@ -68,20 +74,20 @@ def _select_qt_binding(binding_name=None):

# try to load preferred bindings
error_msgs = []
for binding_name in DEFAULT_BINDING_ORDER:
for binding_name in binding_order:
try:
binding_loader = getattr(sys.modules[__name__], '_load_%s' % binding_name, None)
if binding_loader:
QT_BINDING_VERSION = binding_loader(required_modules, optional_modules)
QT_BINDING = binding_name
break
else:
error_msgs.append(' Binding loader "_load_%s" not found.' % binding_name)
except ImportError, e:
error_msgs.append(' ImportError for "%s": %s' % (binding_name, e))
error_msgs.append(" Binding loader '_load_%s' not found." % binding_name)
except ImportError as e:
error_msgs.append(" ImportError for '%s': %s" % (binding_name, e))

if not QT_BINDING:
raise ImportError('Could not find Qt binding (looked for "%s"):\n%s' % (DEFAULT_BINDING_ORDER, '\n'.join(error_msgs)))
raise ImportError("Could not find Qt binding (looked for: %s):\n%s" % (', '.join(["'%s'" % b for b in binding_order]), '\n'.join(error_msgs)))


def _register_binding_module(module_name, module):
Expand Down Expand Up @@ -112,7 +118,7 @@ def _load_pyqt(required_modules, optional_modules):
# set environment variable QT_API for matplotlib
os.environ['QT_API'] = 'pyqt'

# select PyQt4 API, see http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/incompatible_apis.html
# select PyQt4 API, see http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
import sip
try:
sip.setapi('QDate', 2)
Expand All @@ -122,7 +128,7 @@ def _load_pyqt(required_modules, optional_modules):
sip.setapi('QTime', 2)
sip.setapi('QUrl', 2)
sip.setapi('QVariant', 2)
except ValueError, e:
except ValueError as e:
raise RuntimeError('Could not set API version (%s): did you import PyQt4 directly?' % e)

# register required and optional PyQt4 modules
Expand Down Expand Up @@ -251,8 +257,7 @@ class is in use. (Note: this is only necessary
return _loadUi(uifile, baseinstance, custom_widgets)


QT_BINDING = None
QT_BINDING_MODULES = {}
QT_BINDING_VERSION = None

_select_qt_binding(getattr(sys, 'SELECT_QT_BINDING', None))
_select_qt_binding(
getattr(sys, 'SELECT_QT_BINDING', None),
getattr(sys, 'SELECT_QT_BINDING_ORDER', None),
)

0 comments on commit a6ef126

Please sign in to comment.