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 33
44import asyncio
55import time
6- from contextlib import suppress
76from functools import partial
87from typing import Any
98
1514from pymodbus .utilities import ModbusTransactionState
1615
1716
18- with suppress ( ImportError ) :
17+ try :
1918 import serial
2019
20+ PYSERIAL_MISSING = False
21+ except ImportError :
22+ PYSERIAL_MISSING = True
23+
2124
2225class AsyncModbusSerialClient (ModbusBaseClient , asyncio .Protocol ):
2326 """**AsyncModbusSerialClient**.
@@ -74,6 +77,11 @@ def __init__(
7477 ** kwargs : Any ,
7578 ) -> None :
7679 """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+ )
7785 asyncio .Protocol .__init__ (self )
7886 ModbusBaseClient .__init__ (
7987 self ,
Original file line number Diff line number Diff line change 1717from pymodbus .transport import CommParams , CommType , ModbusProtocol
1818
1919
20- with suppress (ImportError ):
21- pass
22-
23-
2420# --------------------------------------------------------------------------- #
2521# Protocol Handlers
2622# --------------------------------------------------------------------------- #
Original file line number Diff line number Diff line change 1414
1515try :
1616 from aiohttp import web
17+
18+ AIOHTTP_MISSING = False
1719except 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
2321
2422from pymodbus import __version__ as pymodbus_version
2523from pymodbus .datastore import ModbusServerContext , ModbusSlaveContext
@@ -199,6 +197,11 @@ class ReactiveServer:
199197
200198 def __init__ (self , host , port , modbus_server ):
201199 """Initialize."""
200+ if AIOHTTP_MISSING :
201+ raise RuntimeError (
202+ "Reactive server requires aiohttp. "
203+ 'Please install with "pip install aiohttp" and try again.'
204+ )
202205 self ._web_app = web .Application ()
203206 self ._runner = web .AppRunner (self ._web_app )
204207 self ._host = host
Original file line number Diff line number Diff line change 99from typing import List
1010
1111
12- with contextlib . suppress ( ImportError ) :
12+ try :
1313 from aiohttp import web
1414
15+ AIOHTTP_MISSING = False
16+ except ImportError :
17+ AIOHTTP_MISSING = True
18+
1519from pymodbus .datastore import ModbusServerContext , ModbusSimulatorContext
1620from pymodbus .datastore .simulator import Label
1721from pymodbus .device import ModbusDeviceIdentification
@@ -117,8 +121,11 @@ def __init__(
117121 custom_actions_module : str = None ,
118122 ):
119123 """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+ )
122129 with open (json_file , encoding = "utf-8" ) as file :
123130 setup = json .load (file )
124131
You can’t perform that action at this time.
0 commit comments