jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/563709 )
Change subject: [IMPR] Provide a version with tools.has_module ......................................................................
[IMPR] Provide a version with tools.has_module
Change-Id: I0b5d467f3e53e5314831f1f54ee6ebb07c9d5c8a --- M pywikibot/tools/__init__.py 1 file changed, 18 insertions(+), 5 deletions(-)
Approvals: Framawiki: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py index 9339ae7..2d9b6d5 100644 --- a/pywikibot/tools/__init__.py +++ b/pywikibot/tools/__init__.py @@ -1,13 +1,14 @@ # -*- coding: utf-8 -*- """Miscellaneous helper functions (not wiki-dependent).""" # -# (C) Pywikibot team, 2008-2019 +# (C) Pywikibot team, 2008-2020 # # Distributed under the terms of the MIT license. # from __future__ import absolute_import, division, unicode_literals
import collections +from distutils.version import LooseVersion import gzip import hashlib from importlib import import_module @@ -372,14 +373,26 @@ '%s: %s' % (self.__class__.__name__, self.__doc__))
-def has_module(module): +def has_module(module, version=None): """Check whether a module can be imported.""" try: - import_module(module) + m = import_module(module) except ImportError: - return False + pass else: - return True + if version is None: + return True + try: + module_version = LooseVersion(m.__version__) + except AttributeError: + pass + else: + if module_version >= LooseVersion(version): + return True + else: + warn('Module version {} is lower than requested version {}' + .format(module_version, version), ImportWarning) + return False
def empty_iterator():
pywikibot-commits@lists.wikimedia.org