From 4bab430c32b51b8c6f0bca6d3792115341366375 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 16 Sep 2014 07:16:15 +0200 Subject: [PATCH] utils.version: Use programmatic imports. --- qutebrowser/utils/version.py | 65 ++++++++++++++---------------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py index e9048c564..7c523d71d 100644 --- a/qutebrowser/utils/version.py +++ b/qutebrowser/utils/version.py @@ -24,6 +24,8 @@ import glob import os.path import platform import subprocess +import importlib +import collections from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR, qVersion from PyQt5.QtWebKit import qWebKitVersion @@ -122,8 +124,6 @@ def _module_versions(): Return: 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 = [] try: import sipconfig @@ -137,44 +137,29 @@ def _module_versions(): log.misc.warning("Error while getting SIP version: {}: {}".format( e.__class__.__name__, e)) lines.append('SIP: ?') - - try: - import colorlog - except ImportError: - pass - else: - lines.append('colorlog: yes') - - try: - import colorama - except ImportError: - pass - else: - ver = getattr(colorama, 'VERSION', None) - if ver is None: - ver = getattr(colorama, '__version__', 'yes') - lines.append('colorama: {}'.format(ver)) - try: - import pypeg2 - except ImportError: - pass - else: - ver = getattr(pypeg2, '__version__', 'yes') - 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)) + modules = collections.OrderedDict([ + ('colorlog', []), + ('colorama', ['VERSION', '__version__']), + ('pypeg2', ['__version__']), + ('jinja2', ['__version__']), + ('pygments', ['__version__']), + ]) + for name, attributes in modules.items(): + try: + module = importlib.import_module(name) + except ImportError: + text = '{}: no'.format(name) + else: + for attr in attributes: + try: + text = '{}: {}'.format(name, getattr(module, attr)) + except AttributeError: + pass + else: + break + else: + text = '{}: yes'.format(name) + lines.append(text) return lines