Skip to content

Commit 7361275

Browse files
authored
Remove unused methods in datastore. (#2888)
1 parent 8cdb1d9 commit 7361275

File tree

8 files changed

+16
-145
lines changed

8 files changed

+16
-145
lines changed

examples/server_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def setup_server(description=None, context=None, cmdline=None):
8989
# Alternately, use the factory methods to initialize the DataBlocks
9090
# or simply do not pass them to have them initialized to 0x00 on the
9191
# full address range::
92-
datablock = lambda : ModbusSequentialDataBlock.create() # pylint: disable=unnecessary-lambda-assignment,unnecessary-lambda
92+
datablock = lambda : ModbusSequentialDataBlock(0x00, [0x00] * 65536) # pylint: disable=unnecessary-lambda-assignment
9393

9494
if args.device_ids > 1: # pragma: no cover
9595
# The server then makes use of a server context that allows the server
@@ -197,7 +197,7 @@ async def run_async_server(args) -> None:
197197
certfile=helper.get_certificate(
198198
"crt"
199199
), # The cert file path for TLS (used if sslctx is None)
200-
# sslctx=sslctx, # The SSLContext to use for TLS (default None and auto create)
200+
# sslctx=sslctx, # The SSLContext to use for TLS (default None and auto)
201201
keyfile=helper.get_certificate(
202202
"key"
203203
), # The key file path for TLS (used if sslctx is None)

pymodbus/datastore/context.py

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from ..constants import ExcCodes
66
from ..exceptions import NoSuchIdException
77
from ..logging import Log
8-
from .sequential import ModbusSequentialDataBlock
98
from .store import BaseModbusDataBlock
109

1110

@@ -67,23 +66,12 @@ def __init__(self, *_args,
6766
hr: BaseModbusDataBlock | None = None,
6867
):
6968
"""Initialize the datastores."""
70-
self.store = {}
71-
self.store["d"] = di if di is not None else ModbusSequentialDataBlock.create()
72-
self.store["c"] = co if co is not None else ModbusSequentialDataBlock.create()
73-
self.store["i"] = ir if ir is not None else ModbusSequentialDataBlock.create()
74-
self.store["h"] = hr if hr is not None else ModbusSequentialDataBlock.create()
75-
76-
def __str__(self):
77-
"""Return a string representation of the context.
78-
79-
:returns: A string representation of the context
80-
"""
81-
return "Modbus device Context"
82-
83-
def reset(self):
84-
"""Reset all the datastores to their default values."""
85-
for datastore in iter(self.store.values()):
86-
datastore.reset()
69+
self.store = {
70+
"d": di,
71+
"c": co,
72+
"i": ir,
73+
"h": hr,
74+
}
8775

8876
async def async_getValues(self, func_code, address, count=1) -> list[int] | list[bool] | ExcCodes:
8977
"""Get `count` values from datastore.
@@ -95,7 +83,9 @@ async def async_getValues(self, func_code, address, count=1) -> list[int] | list
9583
"""
9684
address += 1
9785
Log.debug("getValues: fc-[{}] address-{}: count-{}", func_code, address, count)
98-
return await self.store[self.decode(func_code)].async_getValues(address, count)
86+
if dt := self.store[self.decode(func_code)]:
87+
return await dt.async_getValues(address, count)
88+
return ExcCodes.ILLEGAL_ADDRESS
9989

10090
async def async_setValues(self, func_code, address, values) -> None | ExcCodes:
10191
"""Set the datastore with the supplied values.
@@ -106,7 +96,9 @@ async def async_setValues(self, func_code, address, values) -> None | ExcCodes:
10696
"""
10797
address += 1
10898
Log.debug("setValues[{}] address-{}: count-{}", func_code, address, len(values))
109-
return await self.store[self.decode(func_code)].async_setValues(address, values)
99+
if dt := self.store[self.decode(func_code)]:
100+
return await dt.async_setValues(address, values)
101+
return ExcCodes.ILLEGAL_ADDRESS
110102

111103

112104
class ModbusServerContext:
@@ -162,17 +154,6 @@ async def async_setValues(self, device_id: int, func_code: int, address: int, va
162154
dev = self.__get_device(device_id)
163155
return await dev.async_setValues(func_code, address, values)
164156

165-
def __getitem__(self, device_id):
166-
"""Use to get access to a device_id context.
167-
168-
:param device_id: The device context to get
169-
:returns: The requested device context
170-
:raises NoSuchIdException:
171-
"""
172-
if self.single:
173-
device_id = 0
174-
return self.__get_device(device_id)
175-
176157
def device_ids(self):
177158
"""Get the configured device ids."""
178159
return list(self._devices.keys())

pymodbus/datastore/sequential.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,6 @@ def __init__(self, address, values):
2020
self.values = list(values)
2121
else:
2222
self.values = [values]
23-
self.default_value = self.values[0].__class__()
24-
25-
@classmethod
26-
def create(cls):
27-
"""Create a datastore.
28-
29-
With the full address space initialized to 0x00
30-
31-
:returns: An initialized datastore
32-
"""
33-
return cls(0x00, [0x00] * 65536)
34-
35-
def default(self, count, value=False):
36-
"""Use to initialize a store to one value.
37-
38-
:param count: The number of fields to set
39-
:param value: The default value to set to the fields
40-
"""
41-
self.default_value = value
42-
self.values = [self.default_value] * count
43-
self.address = 0x00
44-
45-
def reset(self):
46-
"""Reset the datastore to the initialized default value."""
47-
self.values = [self.default_value] * len(self.values)
4823

4924
async def async_getValues(self, address, count=1) -> list[int] | list[bool] | ExcCodes:
5025
"""Return the requested values of the datastore.

pymodbus/datastore/sparse.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@ def __init__(self, values=None, mutable=True):
1717
self.values = {}
1818
self._process_values(values)
1919
self.mutable = mutable
20-
self.default_value = self.values.copy()
21-
22-
@classmethod
23-
def create(cls, values=None):
24-
"""Create sparse datastore.
25-
26-
Use setValues to initialize registers.
27-
28-
:param values: Either a list or a dictionary of values
29-
:returns: An initialized datastore
30-
"""
31-
return cls(values)
32-
33-
def reset(self):
34-
"""Reset the store to the initially provided defaults."""
35-
self.values = self.default_value.copy()
3620

3721
async def async_getValues(self, address, count=1) -> list[int] | list[bool] | ExcCodes:
3822
"""Return the requested values of the datastore.
@@ -71,12 +55,11 @@ def _process_as_dict(values):
7155
)
7256
_process_as_dict(values)
7357

74-
async def async_setValues(self, address, values, use_as_default=False) -> None | ExcCodes:
58+
async def async_setValues(self, address, values) -> None | ExcCodes:
7559
"""Set the requested values of the datastore.
7660
7761
:param address: The register starting address
7862
:param values: The new values to be set.
79-
:param use_as_default: Use the values as default
8063
8164
Values can be given in different formats:
8265
- a single register value or
@@ -98,9 +81,6 @@ async def async_setValues(self, address, values, use_as_default=False) -> None |
9881
if address + idx not in self.values and not self.mutable:
9982
raise ParameterException("Offset {address+idx} not in range")
10083
self.values[address + idx] = val
101-
if use_as_default:
102-
for idx, val in iter(self.values.items()):
103-
self.default_value[idx] = val
10484
except KeyError:
10585
return ExcCodes.ILLEGAL_ADDRESS
10686
return None

pymodbus/datastore/store.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,3 @@ async def async_setValues(self, address: int, values: list[int] | list[bool]) ->
3737
:param values: The values to store
3838
:raises TypeError:
3939
"""
40-
41-
def reset(self):
42-
"""Reset the datastore to the initialized default value."""
43-
44-
def __str__(self):
45-
"""Build a representation of the datastore.
46-
47-
:returns: A string representation of the datastore
48-
"""
49-
return f"DataStore({len(self.values)}, {self.default_value})"
50-
51-
def __iter__(self):
52-
"""Iterate over the data block data.
53-
54-
:returns: An iterator of the data block data
55-
"""
56-
if isinstance(self.values, dict):
57-
return iter(self.values.items())
58-
return enumerate(self.values, self.address)

test/datastore/test_context.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
"""Test datastore context."""
2-
import pytest
32

43
from pymodbus.datastore import (
54
ModbusBaseDeviceContext,
65
ModbusDeviceContext,
76
ModbusServerContext,
87
)
9-
from pymodbus.exceptions import NoSuchIdException
108

119

1210
class TestContextDataStore:
@@ -18,12 +16,6 @@ async def test_datastore_base_device(self):
1816
await dev.async_getValues(0x01, 0x01)
1917
await dev.async_setValues(0x05, 0x01, [0])
2018

21-
def test_datastore_device(self):
22-
"""Test ModbusDeviceContext."""
23-
dev = ModbusDeviceContext()
24-
str(dev)
25-
dev.reset()
26-
2719
async def test_datastore_device_Values(self):
2820
"""Test ModbusDeviceContext."""
2921
dev = ModbusDeviceContext()
@@ -43,13 +35,3 @@ def test_datastore_server_ids(self):
4335
srv = ModbusServerContext()
4436
assert isinstance(srv.device_ids(), list)
4537

46-
def test_datastore_get(self):
47-
"""Test ModbusServerContext."""
48-
server = ModbusServerContext(devices={1: {}}, single=False)
49-
with pytest.raises(NoSuchIdException):
50-
server[5]
51-
server = ModbusServerContext(devices={1: {}, 0: {}}, single=False)
52-
assert isinstance(server[5], dict)
53-
server = ModbusServerContext(devices={1: {}}, single=True)
54-
assert isinstance(server[5], dict)
55-

test/datastore/test_sequential.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def test_datastore_Sequential(self):
1111
"""Test ModbusDeviceContext."""
1212
ModbusSequentialDataBlock(0x01, [17])
1313
ModbusSequentialDataBlock(0x01, 17)
14-
ModbusSequentialDataBlock(0x01, 17).default(112)
1514

1615
async def test_datastore_Sequential_get(self):
1716
"""Test ModbusDeviceContext."""
@@ -24,9 +23,3 @@ async def test_datastore_Sequential_set(self):
2423
await block.async_setValues(1, [19])
2524
await block.async_setValues(1, 19)
2625
assert await block.async_setValues(13, [17]) == ExcCodes.ILLEGAL_ADDRESS
27-
28-
def test_datastore_Sequential_iter(self):
29-
"""Test check frame."""
30-
block = ModbusSequentialDataBlock(0x01, [17])
31-
str(block)
32-
_ = list(block)

test/datastore/test_sparse_datastore.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,9 @@ async def test_sparse_datastore_check(self):
4949
assert await datablock.async_getValues(key, 1) == [value]
5050
key += 1
5151

52-
async def test_sparse_datastore_create(self):
53-
"""Test check frame."""
54-
datablock = ModbusSparseDataBlock.create(self.data_in_block)
55-
for key, entry in self.data_in_block.items():
56-
if isinstance(entry, int):
57-
entry = [entry]
58-
for value in entry:
59-
assert await datablock.async_getValues(key, 1) == [value]
60-
key += 1
61-
62-
def test_sparse_datastore_reset(self):
63-
"""Test check frame."""
64-
datablock = ModbusSparseDataBlock.create()
65-
datablock.reset()
66-
6752
async def test_sparse_datastore_get(self):
6853
"""Test check frame."""
69-
datablock = ModbusSparseDataBlock.create()
54+
datablock = ModbusSparseDataBlock()
7055
assert await datablock.async_getValues(117) == ExcCodes.ILLEGAL_ADDRESS
7156

7257
async def test_sparse_datastore_set(self):
@@ -75,7 +60,6 @@ async def test_sparse_datastore_set(self):
7560
assert not await datablock.async_setValues(1, {1: 5})
7661
assert not await datablock.async_setValues(1, [5])
7762
assert not await datablock.async_setValues(1, 5)
78-
assert not await datablock.async_setValues(1, 5, use_as_default=True)
7963

8064
async def test_sparse_datastore_async_set(self):
8165
"""Test check frame."""
@@ -92,8 +76,3 @@ async def test_sparse_datastore_set_not_ok(self):
9276
datablock = ModbusSparseDataBlock(self.data_in_block)
9377
datablock._process_values = mock.Mock(side_effect=KeyError) # type: ignore[method-assign]
9478
assert await datablock.async_setValues(30, {17: 0}) == ExcCodes.ILLEGAL_ADDRESS
95-
96-
def test_sparse_datastore_iter(self):
97-
"""Test check frame."""
98-
datablock = ModbusSparseDataBlock(self.data_in_block)
99-
_ = list(datablock)

0 commit comments

Comments
 (0)