FIX: python 2.7 compatibility

This commit is contained in:
Daniel Pizetta 2018-11-14 11:51:30 -02:00
parent 697b77f1f0
commit 6af5efd669

View File

@ -45,20 +45,17 @@ Enjoy!
""" """
importlib_error = ""
try:
import importlib
except ImportError:
importlib_error = "Not available in Python 2.7"
import logging import logging
import os import os
import platform import platform
import sys import sys
import warnings import warnings
import copy
__version__ = "2.6.3" if sys.version_info >= (3, 4):
import importlib
__version__ = "2.6.4"
QT_BINDINGS = ['PyQt4', 'PyQt5', 'PySide', 'PySide2'] QT_BINDINGS = ['PyQt4', 'PyQt5', 'PySide', 'PySide2']
@ -225,7 +222,7 @@ def load_stylesheet(pyside=True):
# Detect the PySide version available # Detect the PySide version available
try: try:
import PySide import PySide
except ModuleNotFoundError: except ImportError: # Compatible with py27
import PySide2 import PySide2
pyside_ver = 2 pyside_ver = 2
else: else:
@ -400,41 +397,61 @@ def information():
def qt_bindings(): def qt_bindings():
"""Return a list of qt bindings available.""" """Return a list of qt bindings available."""
if importlib_error: return _check_imports(import_list=QT_BINDINGS)
print(importlib_error)
return []
bindings = QT_BINDINGS
for binding in bindings:
# check import
spec = importlib.util.find_spec(binding)
if spec is None:
# remove if not available
bindings.remove(binding)
return bindings
def qt_abstractions(): def qt_abstractions():
"""Return a list of qt abstraction layers available.""" """Return a list of qt abstraction layers available."""
if importlib_error: return _check_imports(import_list=QT_ABSTRACTIONS)
print(importlib_error)
return []
def _check_imports(import_list):
"""Return a list of imports available."""
# Disable warnings here
warnings.filterwarnings("ignore")
import_list_return = copy.deepcopy(import_list)
# Using import_list_return var in for, does not work in py2.7
# when removing the element, it reflects on for list
# so it skips next element
for current_import in import_list:
spec = True
# Copy the sys path to make sure to not insert anything
sys_path = sys.path
# Check import
if sys.version_info >= (3, 4):
spec = importlib.util.find_spec(current_import)
else:
try:
__import__(current_import)
except RuntimeWarning:
spec = True
except Exception:
spec = None
else:
spec = True
abstractions = QT_ABSTRACTIONS
for abstraction in abstractions:
# check import
spec = importlib.util.find_spec(abstraction)
if spec is None: if spec is None:
# remove if not available # Remove if not available
abstractions.remove(abstraction) import_list_return.remove(current_import)
return abstractions
# Restore sys path
sys.path = sys_path
# Restore warnings
warnings.resetwarnings()
return import_list_return
def import_qt_modules_from(use_binding='pyqt5', use_abstraction='qtpy'): def _import_qt_modules_from(use_binding='pyqt5', use_abstraction='qtpy'):
"""New approach to import modules using importlib.""" """New approach to import modules using importlib."""
if importlib_error:
print(importlib_error) if not sys.version_info >= (3, 4):
return [] print('Function not available for Python < 3.4')
spec_binding = importlib.util.find_spec(use_binding) spec_binding = importlib.util.find_spec(use_binding)
spec_abstraction = importlib.util.find_spec(use_abstraction) spec_abstraction = importlib.util.find_spec(use_abstraction)