utils.version: Use programmatic imports.

This commit is contained in:
Florian Bruhin 2014-09-16 07:16:15 +02:00
parent d039a29fc9
commit 4bab430c32

View File

@ -24,6 +24,8 @@ import glob
import os.path import os.path
import platform import platform
import subprocess import subprocess
import importlib
import collections
from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, qVersion from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, qVersion
from PyQt5.QtWebKit import qWebKitVersion from PyQt5.QtWebKit import qWebKitVersion
@ -122,8 +124,6 @@ def _module_versions():
Return: Return:
A list of lines with version info. A list of lines with version info.
""" """
# We should find a way to rewrite this using __import__ or so.
# pylint: disable=import-error,unused-variable,too-many-branches
lines = [] lines = []
try: try:
import sipconfig import sipconfig
@ -137,44 +137,29 @@ def _module_versions():
log.misc.warning("Error while getting SIP version: {}: {}".format( log.misc.warning("Error while getting SIP version: {}: {}".format(
e.__class__.__name__, e)) e.__class__.__name__, e))
lines.append('SIP: ?') lines.append('SIP: ?')
modules = collections.OrderedDict([
try: ('colorlog', []),
import colorlog ('colorama', ['VERSION', '__version__']),
except ImportError: ('pypeg2', ['__version__']),
pass ('jinja2', ['__version__']),
else: ('pygments', ['__version__']),
lines.append('colorlog: yes') ])
for name, attributes in modules.items():
try: try:
import colorama module = importlib.import_module(name)
except ImportError: except ImportError:
pass text = '{}: no'.format(name)
else: else:
ver = getattr(colorama, 'VERSION', None) for attr in attributes:
if ver is None: try:
ver = getattr(colorama, '__version__', 'yes') text = '{}: {}'.format(name, getattr(module, attr))
lines.append('colorama: {}'.format(ver)) except AttributeError:
try: pass
import pypeg2 else:
except ImportError: break
pass else:
else: text = '{}: yes'.format(name)
ver = getattr(pypeg2, '__version__', 'yes') lines.append(text)
lines.append('pypeg2: {}'.format(ver))
try:
import jinja2
except ImportError:
pass
else:
ver = getattr(jinja2, '__version__', 'yes')
lines.append('jinja2: {}'.format(ver))
try:
import pygments
except ImportError:
pass
else:
ver = getattr(pygments, '__version__', 'yes')
lines.append('pygments: {}'.format(ver))
return lines return lines