From cbdd5da8599dde8af0f3a40a3c8c09c02f5d381c Mon Sep 17 00:00:00 2001 From: jan iversen Date: Wed, 8 May 2024 12:12:16 +0200 Subject: [PATCH] Show error if example is run without support files. --- examples/client_async.py | 10 +++++++++- examples/client_async_calls.py | 9 ++++++++- examples/client_calls.py | 11 ++++++++++- examples/client_payload.py | 10 +++++++++- examples/client_sync.py | 10 +++++++++- examples/modbus_forwarder.py | 10 +++++++++- examples/server_async.py | 10 +++++++++- examples/server_callback.py | 10 +++++++++- examples/server_payload.py | 11 ++++++++++- examples/server_sync.py | 12 ++++++++++-- examples/server_updating.py | 10 +++++++++- 11 files changed, 101 insertions(+), 12 deletions(-) diff --git a/examples/client_async.py b/examples/client_async.py index 6c4fa5804..cc6bfae0f 100755 --- a/examples/client_async.py +++ b/examples/client_async.py @@ -28,8 +28,16 @@ """ import asyncio import logging +import sys -import helper + +try: + import helper +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) import pymodbus.client as modbusClient from pymodbus import ModbusException diff --git a/examples/client_async_calls.py b/examples/client_async_calls.py index 2f84c57b0..482a9ec40 100755 --- a/examples/client_async_calls.py +++ b/examples/client_async_calls.py @@ -31,9 +31,16 @@ """ import asyncio import logging +import sys -import client_async +try: + import client_async +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) _logger = logging.getLogger(__file__) _logger.setLevel("DEBUG") diff --git a/examples/client_calls.py b/examples/client_calls.py index ab9d740d4..50a58bc3d 100755 --- a/examples/client_calls.py +++ b/examples/client_calls.py @@ -30,8 +30,17 @@ ./server_async.py """ import logging +import sys + + +try: + import client_sync +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) -import client_sync _logger = logging.getLogger(__file__) diff --git a/examples/client_payload.py b/examples/client_payload.py index c5d82b082..70eb53ac6 100755 --- a/examples/client_payload.py +++ b/examples/client_payload.py @@ -7,9 +7,17 @@ Works out of the box together with payload_server.py """ import asyncio +import sys from collections import OrderedDict -import client_async + +try: + import client_async +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) from pymodbus.constants import Endian from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder diff --git a/examples/client_sync.py b/examples/client_sync.py index c236988ac..55f03935b 100755 --- a/examples/client_sync.py +++ b/examples/client_sync.py @@ -31,8 +31,16 @@ """ import logging +import sys -import helper + +try: + import helper +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) import pymodbus.client as modbusClient from pymodbus import ModbusException diff --git a/examples/modbus_forwarder.py b/examples/modbus_forwarder.py index 9ada899de..b29070f25 100755 --- a/examples/modbus_forwarder.py +++ b/examples/modbus_forwarder.py @@ -18,8 +18,16 @@ """ import asyncio import logging +import sys -import helper + +try: + import helper +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) from pymodbus.client import ModbusTcpClient from pymodbus.datastore import ModbusServerContext diff --git a/examples/server_async.py b/examples/server_async.py index 8cb2ad787..676fa8ba5 100755 --- a/examples/server_async.py +++ b/examples/server_async.py @@ -34,8 +34,16 @@ """ import asyncio import logging +import sys -import helper + +try: + import helper +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) from pymodbus import __version__ as pymodbus_version from pymodbus.datastore import ( diff --git a/examples/server_callback.py b/examples/server_callback.py index f5cd574bd..6be7da107 100755 --- a/examples/server_callback.py +++ b/examples/server_callback.py @@ -6,8 +6,16 @@ """ import asyncio import logging +import sys -import server_async + +try: + import server_async +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) from pymodbus.datastore import ( ModbusSequentialDataBlock, diff --git a/examples/server_payload.py b/examples/server_payload.py index c56d5a035..abd0b444e 100755 --- a/examples/server_payload.py +++ b/examples/server_payload.py @@ -6,8 +6,17 @@ """ import asyncio import logging +import sys + + +try: + import server_async +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) -import server_async from pymodbus.constants import Endian from pymodbus.datastore import ( diff --git a/examples/server_sync.py b/examples/server_sync.py index 52192b4a0..0b6d3bf6d 100755 --- a/examples/server_sync.py +++ b/examples/server_sync.py @@ -35,9 +35,17 @@ a lot slower. """ import logging +import sys -import helper -import server_async + +try: + import helper + import server_async +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) # --------------------------------------------------------------------------- # # import the various client implementations diff --git a/examples/server_updating.py b/examples/server_updating.py index dae9e7f61..5ece64f23 100755 --- a/examples/server_updating.py +++ b/examples/server_updating.py @@ -33,8 +33,16 @@ """ import asyncio import logging +import sys -import server_async + +try: + import server_async +except ImportError: + print("*** ERROR --> THIS EXAMPLE needs the example directory, please see \n\ + https://pymodbus.readthedocs.io/en/latest/source/examples.html\n\ + for more information.") + sys.exit(-1) from pymodbus.datastore import ( ModbusSequentialDataBlock,