File tree Expand file tree Collapse file tree 4 files changed +28
-14
lines changed Expand file tree Collapse file tree 4 files changed +28
-14
lines changed Original file line number Diff line number Diff line change 3
3
4
4
import asyncio
5
5
import time
6
- from contextlib import suppress
7
6
from functools import partial
8
7
from typing import Any
9
8
15
14
from pymodbus .utilities import ModbusTransactionState
16
15
17
16
18
- with suppress ( ImportError ) :
17
+ try :
19
18
import serial
20
19
20
+ PYSERIAL_MISSING = False
21
+ except ImportError :
22
+ PYSERIAL_MISSING = True
23
+
21
24
22
25
class AsyncModbusSerialClient (ModbusBaseClient , asyncio .Protocol ):
23
26
"""**AsyncModbusSerialClient**.
@@ -74,6 +77,11 @@ def __init__(
74
77
** kwargs : Any ,
75
78
) -> None :
76
79
"""Initialize Asyncio Modbus Serial Client."""
80
+ if PYSERIAL_MISSING :
81
+ raise RuntimeError (
82
+ "Serial client requires pyserial "
83
+ 'Please install with "pip install pyserial" and try again.'
84
+ )
77
85
asyncio .Protocol .__init__ (self )
78
86
ModbusBaseClient .__init__ (
79
87
self ,
Original file line number Diff line number Diff line change 17
17
from pymodbus .transport import CommParams , CommType , ModbusProtocol
18
18
19
19
20
- with suppress (ImportError ):
21
- pass
22
-
23
-
24
20
# --------------------------------------------------------------------------- #
25
21
# Protocol Handlers
26
22
# --------------------------------------------------------------------------- #
Original file line number Diff line number Diff line change 14
14
15
15
try :
16
16
from aiohttp import web
17
+
18
+ AIOHTTP_MISSING = False
17
19
except ImportError :
18
- print (
19
- "Reactive server requires aiohttp. "
20
- 'Please install with "pip install aiohttp" and try again.'
21
- )
22
- sys .exit (1 )
20
+ AIOHTTP_MISSING = True
23
21
24
22
from pymodbus import __version__ as pymodbus_version
25
23
from pymodbus .datastore import ModbusServerContext , ModbusSlaveContext
@@ -199,6 +197,11 @@ class ReactiveServer:
199
197
200
198
def __init__ (self , host , port , modbus_server ):
201
199
"""Initialize."""
200
+ if AIOHTTP_MISSING :
201
+ raise RuntimeError (
202
+ "Reactive server requires aiohttp. "
203
+ 'Please install with "pip install aiohttp" and try again.'
204
+ )
202
205
self ._web_app = web .Application ()
203
206
self ._runner = web .AppRunner (self ._web_app )
204
207
self ._host = host
Original file line number Diff line number Diff line change 9
9
from typing import List
10
10
11
11
12
- with contextlib . suppress ( ImportError ) :
12
+ try :
13
13
from aiohttp import web
14
14
15
+ AIOHTTP_MISSING = False
16
+ except ImportError :
17
+ AIOHTTP_MISSING = True
18
+
15
19
from pymodbus .datastore import ModbusServerContext , ModbusSimulatorContext
16
20
from pymodbus .datastore .simulator import Label
17
21
from pymodbus .device import ModbusDeviceIdentification
@@ -117,8 +121,11 @@ def __init__(
117
121
custom_actions_module : str = None ,
118
122
):
119
123
"""Initialize http interface."""
120
- if not web :
121
- raise RuntimeError ("aiohttp not installed!" )
124
+ if AIOHTTP_MISSING :
125
+ raise RuntimeError (
126
+ "Simulator server requires aiohttp. "
127
+ 'Please install with "pip install aiohttp" and try again.'
128
+ )
122
129
with open (json_file , encoding = "utf-8" ) as file :
123
130
setup = json .load (file )
124
131
You can’t perform that action at this time.
0 commit comments