Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent messages if imports fail #1831

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pymodbus/client/serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import asyncio
import time
from contextlib import suppress
from functools import partial
from typing import Any

Expand All @@ -15,9 +14,13 @@
from pymodbus.utilities import ModbusTransactionState


with suppress(ImportError):
try:
import serial

PYSERIAL_MISSING = False
except ImportError:
PYSERIAL_MISSING = True


class AsyncModbusSerialClient(ModbusBaseClient, asyncio.Protocol):
"""**AsyncModbusSerialClient**.
Expand Down Expand Up @@ -74,6 +77,11 @@ def __init__(
**kwargs: Any,
) -> None:
"""Initialize Asyncio Modbus Serial Client."""
if PYSERIAL_MISSING:
raise RuntimeError(
"Serial client requires pyserial "
'Please install with "pip install pyserial" and try again.'
)
asyncio.Protocol.__init__(self)
ModbusBaseClient.__init__(
self,
Expand Down
4 changes: 0 additions & 4 deletions pymodbus/server/async_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
from pymodbus.transport import CommParams, CommType, ModbusProtocol


with suppress(ImportError):
pass


# --------------------------------------------------------------------------- #
# Protocol Handlers
# --------------------------------------------------------------------------- #
Expand Down
13 changes: 8 additions & 5 deletions pymodbus/server/reactive/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

try:
from aiohttp import web

AIOHTTP_MISSING = False
except ImportError:
print(
"Reactive server requires aiohttp. "
'Please install with "pip install aiohttp" and try again.'
)
sys.exit(1)
AIOHTTP_MISSING = True

from pymodbus import __version__ as pymodbus_version
from pymodbus.datastore import ModbusServerContext, ModbusSlaveContext
Expand Down Expand Up @@ -199,6 +197,11 @@ class ReactiveServer:

def __init__(self, host, port, modbus_server):
"""Initialize."""
if AIOHTTP_MISSING:
raise RuntimeError(
"Reactive server requires aiohttp. "
'Please install with "pip install aiohttp" and try again.'
)
self._web_app = web.Application()
self._runner = web.AppRunner(self._web_app)
self._host = host
Expand Down
13 changes: 10 additions & 3 deletions pymodbus/server/simulator/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
from typing import List


with contextlib.suppress(ImportError):
try:
from aiohttp import web

AIOHTTP_MISSING = False
except ImportError:
AIOHTTP_MISSING = True

from pymodbus.datastore import ModbusServerContext, ModbusSimulatorContext
from pymodbus.datastore.simulator import Label
from pymodbus.device import ModbusDeviceIdentification
Expand Down Expand Up @@ -117,8 +121,11 @@ def __init__(
custom_actions_module: str = None,
):
"""Initialize http interface."""
if not web:
raise RuntimeError("aiohttp not installed!")
if AIOHTTP_MISSING:
raise RuntimeError(
"Simulator server requires aiohttp. "
'Please install with "pip install aiohttp" and try again.'
)
with open(json_file, encoding="utf-8") as file:
setup = json.load(file)

Expand Down