Skip to content

Commit e62ca12

Browse files
committed
Remove interface classes.
1 parent a24027a commit e62ca12

File tree

19 files changed

+83
-357
lines changed

19 files changed

+83
-357
lines changed

API_changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ PyModbus - API changes.
55
-------------
66
Version 3.2.0
77
-------------
8+
- pymodbus/interfaces/IModbusDecoder removed.
9+
- pymodbus/interfaces/IModbusFramer removed.
10+
- pymodbus/interfaces/IModbusSlaveContext -> pymodbus/datastore/ModbusBaseSlaveContext.
811
- StartAsync<type>Server, removed defer_start argument, return is None.
912
instead of using defer_start instantiate the Modbus<type>Server directly.
1013
- `ReturnSlaveNoReponseCountResponse` has been corrected to

doc/source/library/pymodbus.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ Extra functions
4747
:undoc-members:
4848
:show-inheritance:
4949

50-
.. automodule:: pymodbus.interfaces
51-
:members:
52-
:undoc-members:
53-
:show-inheritance:
54-
5550
.. automodule:: pymodbus.mei_message
5651
:members:
5752
:undoc-members:

examples/v2.5.3/bcd_payload.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from pymodbus.constants import Endian
1111
from pymodbus.exceptions import ParameterException
12-
from pymodbus.interfaces import IPayloadBuilder
1312
from pymodbus.payload import BinaryPayloadDecoder
1413
from pymodbus.utilities import pack_bitstring, unpack_bitstring
1514

@@ -57,7 +56,7 @@ def count_bcd_digits(bcd):
5756
return count
5857

5958

60-
class BcdPayloadBuilder(IPayloadBuilder):
59+
class BcdPayloadBuilder:
6160
"""A utility that helps build binary coded decimal payload messages
6261
6362
to be written with the various modbus messages.

examples/v2.5.3/modicon_payload.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99

1010
from pymodbus.constants import Endian
1111
from pymodbus.exceptions import ParameterException
12-
from pymodbus.interfaces import IPayloadBuilder
1312
from pymodbus.utilities import pack_bitstring, unpack_bitstring
1413

1514

16-
class ModiconPayloadBuilder(IPayloadBuilder):
15+
class ModiconPayloadBuilder:
1716
"""A utility that helps build modicon encoded payload messages.
1817
1918
to be written with the various modbus messages.

examples/v2.5.3/remote_server_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"""
2323
import logging
2424

25+
from pymodbus.datastore import ModbusBaseSlaveContext
2526
from pymodbus.exceptions import NotImplementedException
26-
from pymodbus.interfaces import IModbusSlaveContext
2727

2828

2929
# -------------------------------------------------------------------------- #
@@ -40,7 +40,7 @@
4040
# -------------------------------------------------------------------------- #
4141

4242

43-
class RemoteSingleSlaveContext(IModbusSlaveContext):
43+
class RemoteSingleSlaveContext(ModbusBaseSlaveContext):
4444
"""This is a remote server context,
4545
4646
that allows one to create a server context backed by a single client that

pymodbus/datastore/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Datastore."""
2-
from pymodbus.datastore.context import ModbusServerContext, ModbusSlaveContext
2+
from pymodbus.datastore.context import (
3+
ModbusBaseSlaveContext,
4+
ModbusServerContext,
5+
ModbusSlaveContext,
6+
)
37
from pymodbus.datastore.database.redis_datastore import RedisSlaveContext
48
from pymodbus.datastore.database.sql_datastore import SqlSlaveContext
59
from pymodbus.datastore.simulator import ModbusSimulatorContext
@@ -13,6 +17,7 @@
1317
# Exported symbols
1418
# ---------------------------------------------------------------------------#
1519
__all__ = [
20+
"ModbusBaseSlaveContext",
1621
"ModbusSequentialDataBlock",
1722
"ModbusSparseDataBlock",
1823
"ModbusSlaveContext",

pymodbus/datastore/context.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,36 @@
33
from pymodbus.constants import Defaults
44
from pymodbus.datastore.store import ModbusSequentialDataBlock
55
from pymodbus.exceptions import NoSuchSlaveException
6-
from pymodbus.interfaces import IModbusSlaveContext
76
from pymodbus.logging import Log
87

98

9+
class ModbusBaseSlaveContext: # pylint: disable=too-few-public-methods
10+
"""Interface for a modbus slave data context.
11+
12+
Derived classes must implemented the following methods:
13+
reset(self)
14+
validate(self, fx, address, count=1)
15+
getValues(self, fx, address, count=1)
16+
setValues(self, fx, address, values)
17+
"""
18+
19+
__fx_mapper = {2: "d", 4: "i"}
20+
__fx_mapper.update([(i, "h") for i in (3, 6, 16, 22, 23)])
21+
__fx_mapper.update([(i, "c") for i in (1, 5, 15)])
22+
23+
def decode(self, fx): # pylint: disable=invalid-name
24+
"""Convert the function code to the datastore to.
25+
26+
:param fx: The function we are working with
27+
:returns: one of [d(iscretes),i(nputs),h(olding),c(oils)
28+
"""
29+
return self.__fx_mapper[fx]
30+
31+
1032
# ---------------------------------------------------------------------------#
1133
# Slave Contexts
1234
# ---------------------------------------------------------------------------#
13-
class ModbusSlaveContext(IModbusSlaveContext):
35+
class ModbusSlaveContext(ModbusBaseSlaveContext):
1436
"""This creates a modbus data model with each data access stored in a block."""
1537

1638
def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
@@ -91,9 +113,7 @@ def register(self, function_code, fc_as_hex, datablock=None):
91113
:param datablock: datablock to associate with this function code
92114
"""
93115
self.store[fc_as_hex] = datablock or ModbusSequentialDataBlock.create()
94-
self._IModbusSlaveContext__fx_mapper[ # pylint: disable=no-member
95-
function_code
96-
] = fc_as_hex
116+
self.__fx_mapper[function_code] = fc_as_hex
97117

98118

99119
class ModbusServerContext:

pymodbus/datastore/database/redis_datastore.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
except ImportError:
88
pass
99

10-
from pymodbus.interfaces import IModbusSlaveContext
10+
from pymodbus.datastore import ModbusBaseSlaveContext
1111
from pymodbus.logging import Log
1212
from pymodbus.utilities import pack_bitstring, unpack_bitstring
1313

1414

1515
# ---------------------------------------------------------------------------#
1616
# Context
1717
# ---------------------------------------------------------------------------#
18-
class RedisSlaveContext(IModbusSlaveContext):
18+
class RedisSlaveContext(ModbusBaseSlaveContext):
1919
"""This is a modbus slave context using redis as a backing store."""
2020

2121
def __init__(self, **kwargs):
@@ -42,40 +42,40 @@ def reset(self):
4242
"""Reset all the datastores to their default values."""
4343
self.client.flushall()
4444

45-
def validate(self, fx, address, count=1):
45+
def validate(self, fc, address, count=1):
4646
"""Validate the request to make sure it is in range.
4747
48-
:param fx: The function we are working with
48+
:param fc: The function we are working with
4949
:param address: The starting address
5050
:param count: The number of values to test
5151
:returns: True if the request in within range, False otherwise
5252
"""
5353
address = address + 1 # section 4.4 of specification
54-
Log.debug("validate[{}] {}:{}", fx, address, count)
55-
return self._val_callbacks[self.decode(fx)](address, count)
54+
Log.debug("validate[{}] {}:{}", fc, address, count)
55+
return self._val_callbacks[self.decode(fc)](address, count)
5656

57-
def getValues(self, fx, address, count=1):
57+
def getValues(self, fc, address, count=1):
5858
"""Get `count` values from datastore.
5959
60-
:param fx: The function we are working with
60+
:param fc: The function we are working with
6161
:param address: The starting address
6262
:param count: The number of values to retrieve
6363
:returns: The requested values from a:a+c
6464
"""
6565
address = address + 1 # section 4.4 of specification
66-
Log.debug("getValues[{}] {}:{}", fx, address, count)
67-
return self._get_callbacks[self.decode(fx)](address, count)
66+
Log.debug("getValues[{}] {}:{}", fc, address, count)
67+
return self._get_callbacks[self.decode(fc)](address, count)
6868

69-
def setValues(self, fx, address, values):
69+
def setValues(self, fc, address, values):
7070
"""Set the datastore with the supplied values.
7171
72-
:param fx: The function we are working with
72+
:param fc: The function we are working with
7373
:param address: The starting address
7474
:param values: The new values to be set
7575
"""
7676
address = address + 1 # section 4.4 of specification
77-
Log.debug("setValues[{}] {}:{}", fx, address, len(values))
78-
self._set_callbacks[self.decode(fx)](address, values)
77+
Log.debug("setValues[{}] {}:{}", fc, address, len(values))
78+
self._set_callbacks[self.decode(fc)](address, values)
7979

8080
# --------------------------------------------------------------------------#
8181
# Redis Helper Methods

pymodbus/datastore/database/sql_datastore.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
except ImportError:
1111
pass
1212

13-
from pymodbus.interfaces import IModbusSlaveContext
13+
from pymodbus.datastore import ModbusBaseSlaveContext
1414
from pymodbus.logging import Log
1515

1616

1717
# --------------------------------------------------------------------------- #
1818
# Context
1919
# --------------------------------------------------------------------------- #
20-
class SqlSlaveContext(IModbusSlaveContext):
20+
class SqlSlaveContext(ModbusBaseSlaveContext):
2121
"""This creates a modbus data model with each data access in its a block."""
2222

2323
def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
@@ -45,44 +45,44 @@ def reset(self):
4545
self._metadata.drop_all(None)
4646
self._db_create(self.table, self.database)
4747

48-
def validate(self, fx, address, count=1):
48+
def validate(self, fc, address, count=1):
4949
"""Validate the request to make sure it is in range.
5050
51-
:param fx: The function we are working with
51+
:param fc: The function we are working with
5252
:param address: The starting address
5353
:param count: The number of values to test
5454
:returns: True if the request in within range, False otherwise
5555
"""
5656
address = address + 1 # section 4.4 of specification
57-
Log.debug("validate[{}] {}:{}", fx, address, count)
58-
return self._validate(self.decode(fx), address, count)
57+
Log.debug("validate[{}] {}:{}", fc, address, count)
58+
return self._validate(self.decode(fc), address, count)
5959

60-
def getValues(self, fx, address, count=1):
60+
def getValues(self, fc, address, count=1):
6161
"""Get `count` values from datastore.
6262
63-
:param fx: The function we are working with
63+
:param fc: The function we are working with
6464
:param address: The starting address
6565
:param count: The number of values to retrieve
6666
:returns: The requested values from a:a+c
6767
"""
6868
address = address + 1 # section 4.4 of specification
69-
Log.debug("get-values[{}] {}:{}", fx, address, count)
70-
return self._get(self.decode(fx), address, count)
69+
Log.debug("get-values[{}] {}:{}", fc, address, count)
70+
return self._get(self.decode(fc), address, count)
7171

72-
def setValues(self, fx, address, values, update=True):
72+
def setValues(self, fc, address, values, update=True):
7373
"""Set the datastore with the supplied values.
7474
75-
:param fx: The function we are working with
75+
:param fc: The function we are working with
7676
:param address: The starting address
7777
:param values: The new values to be set
7878
:param update: Update existing register in the db
7979
"""
8080
address = address + 1 # section 4.4 of specification
81-
Log.debug("set-values[{}] {}:{}", fx, address, len(values))
81+
Log.debug("set-values[{}] {}:{}", fc, address, len(values))
8282
if update:
83-
self._update(self.decode(fx), address, values)
83+
self._update(self.decode(fc), address, values)
8484
else:
85-
self._set(self.decode(fx), address, values)
85+
self._set(self.decode(fc), address, values)
8686

8787
# ----------------------------------------------------------------------- #
8888
# Sqlite Helper Methods

pymodbus/datastore/remote.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Remote datastore."""
22
# pylint: disable=missing-type-doc
3+
from pymodbus.datastore import ModbusBaseSlaveContext
34
from pymodbus.exceptions import NotImplementedException
4-
from pymodbus.interfaces import IModbusSlaveContext
55
from pymodbus.logging import Log
66

77

88
# ---------------------------------------------------------------------------#
99
# Context
1010
# ---------------------------------------------------------------------------#
11-
class RemoteSlaveContext(IModbusSlaveContext):
11+
class RemoteSlaveContext(ModbusBaseSlaveContext):
1212
"""TODO.
1313
1414
This creates a modbus data model that connects to
@@ -49,7 +49,7 @@ def validate(self, fc_as_hex, address, count=1):
4949
self.result = func_fc(address, count)
5050
return not self.result.isError()
5151

52-
def getValues(self, fc_as_hex, address, count=1):
52+
def getValues(self, fc_as_hex, _address, _count=1):
5353
"""Get values from real call in validate"""
5454
if fc_as_hex in self._write_fc:
5555
return [0]

0 commit comments

Comments
 (0)