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

Solve relative path in examples #1828

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: 7 additions & 5 deletions examples/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import asyncio
import logging

import helper

# --------------------------------------------------------------------------- #
# import the various client implementations
# --------------------------------------------------------------------------- #
Expand All @@ -40,8 +42,6 @@
)
from pymodbus.exceptions import ModbusIOException

from .helper import get_certificate, get_commandline


logging.basicConfig()
_logger = logging.getLogger(__file__)
Expand All @@ -50,7 +50,9 @@

def setup_async_client(description=None, cmdline=None):
"""Run client setup."""
args = get_commandline(server=False, description=description, cmdline=cmdline)
args = helper.get_commandline(
server=False, description=description, cmdline=cmdline
)
_logger.info("### Create client object")
if args.comm == "tcp":
client = AsyncModbusTcpClient(
Expand Down Expand Up @@ -112,8 +114,8 @@ def setup_async_client(description=None, cmdline=None):
# strict=True,
# TLS setup parameters
# sslctx=sslctx,
certfile=get_certificate("crt"),
keyfile=get_certificate("key"),
certfile=helper.get_certificate("crt"),
keyfile=helper.get_certificate("key"),
# password="none",
server_hostname="localhost",
)
Expand Down
8 changes: 4 additions & 4 deletions examples/client_async_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
import asyncio
import logging

import client_async

import pymodbus.diag_message as req_diag
import pymodbus.mei_message as req_mei
import pymodbus.other_message as req_other
from pymodbus.exceptions import ModbusException

from .client_async import run_async_client, setup_async_client


logging.basicConfig()
_logger = logging.getLogger(__file__)
Expand Down Expand Up @@ -231,10 +231,10 @@ async def run_async_calls(client):

async def main(cmdline=None):
"""Combine setup and run"""
testclient = setup_async_client(
testclient = client_async.setup_async_client(
description="Run asynchronous client.", cmdline=cmdline
)
await run_async_client(testclient, modbus_calls=run_async_calls)
await client_async.run_async_client(testclient, modbus_calls=run_async_calls)


if __name__ == "__main__":
Expand Down
10 changes: 6 additions & 4 deletions examples/client_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
"""
import logging

import client_sync

import pymodbus.diag_message as req_diag
import pymodbus.mei_message as req_mei
import pymodbus.other_message as req_other
from pymodbus.exceptions import ModbusException

from .client_sync import run_sync_client, setup_sync_client


logging.basicConfig()
_logger = logging.getLogger(__file__)
Expand Down Expand Up @@ -226,8 +226,10 @@ def run_sync_calls(client):

def main(cmdline=None):
"""Combine setup and run."""
client = setup_sync_client(description="Run synchronous client.", cmdline=cmdline)
run_sync_client(client, modbus_calls=run_sync_calls)
client = client_sync.setup_sync_client(
description="Run synchronous client.", cmdline=cmdline
)
client_sync.run_sync_client(client, modbus_calls=run_sync_calls)


if __name__ == "__main__":
Expand Down
10 changes: 6 additions & 4 deletions examples/client_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import logging
from collections import OrderedDict

import client_async

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadBuilder, BinaryPayloadDecoder

from .client_async import run_async_client, setup_async_client


logging.basicConfig()
_logger = logging.getLogger(__file__)
Expand Down Expand Up @@ -139,8 +139,10 @@ async def run_payload_calls(client):

async def main(cmdline=None):
"""Combine the setup and run"""
client = setup_async_client(description="Run asynchronous client.", cmdline=cmdline)
await run_async_client(client, modbus_calls=run_payload_calls)
client = client_async.setup_async_client(
description="Run asynchronous client.", cmdline=cmdline
)
await client_async.run_async_client(client, modbus_calls=run_payload_calls)


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions examples/client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"""
import logging

import helper

# --------------------------------------------------------------------------- #
# import the various client implementations
# --------------------------------------------------------------------------- #
Expand All @@ -42,8 +44,6 @@
ModbusUdpClient,
)

from .helper import get_certificate, get_commandline


logging.basicConfig()
_logger = logging.getLogger(__file__)
Expand All @@ -52,7 +52,7 @@

def setup_sync_client(description=None, cmdline=None):
"""Run client setup."""
args = get_commandline(
args = helper.get_commandline(
server=False,
description=description,
cmdline=cmdline,
Expand Down Expand Up @@ -116,8 +116,8 @@ def setup_sync_client(description=None, cmdline=None):
# strict=True,
# TLS setup parameters
# sslctx=None,
certfile=get_certificate("crt"),
keyfile=get_certificate("key"),
certfile=helper.get_certificate("crt"),
keyfile=helper.get_certificate("key"),
# password=None,
server_hostname="localhost",
)
Expand Down
10 changes: 5 additions & 5 deletions examples/server_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import asyncio
import logging

import helper

from pymodbus import __version__ as pymodbus_version
from pymodbus.datastore import (
ModbusSequentialDataBlock,
Expand All @@ -50,8 +52,6 @@
StartAsyncUdpServer,
)

from .helper import get_certificate, get_commandline


logging.basicConfig()
_logger = logging.getLogger(__file__)
Expand All @@ -60,7 +60,7 @@

def setup_server(description=None, context=None, cmdline=None):
"""Run server setup."""
args = get_commandline(server=True, description=description, cmdline=cmdline)
args = helper.get_commandline(server=True, description=description, cmdline=cmdline)
if context:
args.context = context
if not args.context:
Expand Down Expand Up @@ -206,11 +206,11 @@ async def run_async_server(args):
# custom_functions=[], # allow custom handling
address=address, # listen address
framer=args.framer, # The framer strategy to use
certfile=get_certificate(
certfile=helper.get_certificate(
"crt"
), # The cert file path for TLS (used if sslctx is None)
# sslctx=sslctx, # The SSLContext to use for TLS (default None and auto create)
keyfile=get_certificate(
keyfile=helper.get_certificate(
"key"
), # The key file path for TLS (used if sslctx is None)
# password="none", # The password for for decrypting the private key file
Expand Down
8 changes: 4 additions & 4 deletions examples/server_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import asyncio
import logging

import server_async

from pymodbus.datastore import (
ModbusSequentialDataBlock,
ModbusServerContext,
ModbusSlaveContext,
)

from .server_async import run_async_server, setup_server


_logger = logging.getLogger(__name__)
_logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -59,10 +59,10 @@ async def run_callback_server(cmdline=None):
block.setValues(1, 15)
store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)
context = ModbusServerContext(slaves=store, single=True)
run_args = setup_server(
run_args = server_async.setup_server(
description="Run callback server.", cmdline=cmdline, context=context
)
await run_async_server(run_args)
await server_async.run_async_server(run_args)


if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions examples/server_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import asyncio
import logging

import server_async

from pymodbus.constants import Endian
from pymodbus.datastore import (
ModbusSequentialDataBlock,
Expand All @@ -15,8 +17,6 @@
)
from pymodbus.payload import BinaryPayloadBuilder

from .server_async import run_async_server, setup_server


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,15 +54,15 @@ def setup_payload_server(cmdline=None):
block = ModbusSequentialDataBlock(1, builder.to_registers())
store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)
context = ModbusServerContext(slaves=store, single=True)
return setup_server(
return server_async.setup_server(
description="Run payload server.", cmdline=cmdline, context=context
)


async def main(cmdline=None):
"""Combine setup and run"""
run_args = setup_payload_server(cmdline=cmdline)
await run_async_server(run_args)
await server_async.run_async_server(run_args)


if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions examples/server_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"""
import logging

import helper
import server_async

# --------------------------------------------------------------------------- #
# import the various client implementations
# --------------------------------------------------------------------------- #
Expand All @@ -46,9 +49,6 @@
StartUdpServer,
)

from .helper import get_certificate
from .server_async import setup_server


_logger = logging.getLogger(__name__)
_logger.setLevel("DEBUG")
Expand Down Expand Up @@ -115,11 +115,11 @@ def run_sync_server(args):
# custom_functions=[], # allow custom handling
address=address, # listen address
framer=args.framer, # The framer strategy to use
certfile=get_certificate(
certfile=helper.get_certificate(
"crt"
), # The cert file path for TLS (used if sslctx is None)
# sslctx=None, # The SSLContext to use for TLS (default None and auto create)
keyfile=get_certificate(
keyfile=helper.get_certificate(
"key"
), # The key file path for TLS (used if sslctx is None)
# password=None, # The password for for decrypting the private key file
Expand All @@ -133,7 +133,7 @@ def run_sync_server(args):

def sync_helper():
"""Combine setup and run."""
run_args = setup_server(description="Run synchronous server.")
run_args = server_async.setup_server(description="Run synchronous server.")
server = run_sync_server(run_args)
server.shutdown()

Expand Down
8 changes: 4 additions & 4 deletions examples/server_updating.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
import asyncio
import logging

import server_async

from pymodbus.datastore import (
ModbusSequentialDataBlock,
ModbusServerContext,
ModbusSlaveContext,
)

from .server_async import run_async_server, setup_server


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,15 +97,15 @@ def setup_updating_server(cmdline=None):
datablock = ModbusSequentialDataBlock(0x00, [17] * 100)
context = ModbusSlaveContext(di=datablock, co=datablock, hr=datablock, ir=datablock)
context = ModbusServerContext(slaves=context, single=True)
return setup_server(
return server_async.setup_server(
description="Run asynchronous server.", context=context, cmdline=cmdline
)


async def run_updating_server(args):
"""Start updating_task concurrently with the current task"""
task = asyncio.create_task(updating_task(args.context))
await run_async_server(args) # start the server
await server_async.run_async_server(args) # start the server
task.cancel()


Expand Down
10 changes: 9 additions & 1 deletion test/sub_examples/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
"""Fixtures for examples tests."""
import asyncio
import sys

import pytest
import pytest_asyncio

from examples.server_async import run_async_server, setup_server
from pymodbus.server import ServerAsyncStop
from pymodbus.transport import NULLMODEM_HOST


sys.path.extend(["examples", "../examples", "../../examples"])

from examples.server_async import ( # noqa: E402 # pylint: disable=wrong-import-position
run_async_server,
setup_server,
)


@pytest.fixture(name="use_host")
def define_use_host():
"""Set default host"""
Expand Down