jenkins-bot merged this change.

View Change

Approvals: Framawiki: Looks good to me, approved jenkins-bot: Verified
[IMPR] Provide a version with tools.has_module

Change-Id: I0b5d467f3e53e5314831f1f54ee6ebb07c9d5c8a
---
M pywikibot/tools/__init__.py
1 file changed, 18 insertions(+), 5 deletions(-)

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():

To view, visit change 563709. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I0b5d467f3e53e5314831f1f54ee6ebb07c9d5c8a
Gerrit-Change-Number: 563709
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: jenkins-bot (75)