Skip to content

Commit 319f82a

Browse files
authored
Merge pull request #339 from riptideio/sneaky-twisted
Avoid unnecessary import of deprecated module #338
2 parents 2b88038 + 938e9b1 commit 319f82a

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

pymodbus/client/async/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,14 @@
3131
# For asyncio the actual client is returned and event loop is asyncio loop
3232
3333
"""
34-
from pymodbus.client.async.deprecated.async import *
34+
from pymodbus.compat import is_installed
35+
36+
installed = is_installed('twisted')
37+
if installed:
38+
# Import deprecated async client only if twisted is installed #338
39+
from pymodbus.client.async.deprecated.async import *
40+
else:
41+
import logging
42+
logger = logging.getLogger(__name__)
43+
logger.warning("Not Importing deprecated clients. "
44+
"Dependency Twisted is not Installed")

pymodbus/client/async/deprecated/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@
4343
"""
4444

4545

46-
def deprecated(name): # pragma: no cover
46+
def deprecated(name): # pragma: no cover
4747
warnings.warn(WARNING.format(name), DeprecationWarning)

pymodbus/compat.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@
5858
implements_to_string = lambda x: x
5959

6060
byte2int = lambda b: b
61+
if PYTHON_VERSION >= (3, 4):
62+
def is_installed(module):
63+
import importlib.util
64+
found = importlib.util.find_spec(module)
65+
return found
66+
else:
67+
def is_installed(module):
68+
import importlib
69+
found = importlib.find_loader(module)
70+
return found
6171
# --------------------------------------------------------------------------- #
6272
# python > 2.5 compatability layer
6373
# --------------------------------------------------------------------------- #
@@ -76,3 +86,11 @@ def implements_to_string(klass):
7686
klass.__unicode__ = klass.__str__
7787
klass.__str__ = lambda x: x.__unicode__().encode('utf-8')
7888
return klass
89+
90+
def is_installed(module):
91+
import imp
92+
try:
93+
imp.find_module(module)
94+
return True
95+
except ImportError:
96+
return False

0 commit comments

Comments
 (0)