Skip to content

Commit dc8a4ca

Browse files
authored
Mirror some OD access API to the SdoBase / SdoVariable classes. (#423)
1 parent a973919 commit dc8a4ca

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

canopen/sdo/base.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import binascii
2-
from typing import Iterable, Union
2+
from typing import Iterable, Optional, Union
33
try:
44
from collections.abc import Mapping
55
except ImportError:
@@ -67,6 +67,19 @@ def __len__(self) -> int:
6767
def __contains__(self, key: Union[int, str]) -> bool:
6868
return key in self.od
6969

70+
def get_variable(
71+
self, index: Union[int, str], subindex: int = 0
72+
) -> Optional["SdoVariable"]:
73+
"""Get the variable object at specified index (and subindex if applicable).
74+
75+
:return: SdoVariable if found, else `None`
76+
"""
77+
obj = self.get(index)
78+
if isinstance(obj, SdoVariable):
79+
return obj
80+
elif isinstance(obj, (SdoRecord, SdoArray)):
81+
return obj.get(subindex)
82+
7083
def upload(self, index: int, subindex: int) -> bytes:
7184
raise NotImplementedError()
7285

@@ -132,6 +145,14 @@ def set_data(self, data: bytes):
132145
force_segment = self.od.data_type == objectdictionary.DOMAIN
133146
self.sdo_node.download(self.od.index, self.od.subindex, data, force_segment)
134147

148+
@property
149+
def writable(self) -> bool:
150+
return self.od.writable
151+
152+
@property
153+
def readable(self) -> bool:
154+
return self.od.readable
155+
135156
def open(self, mode="rb", encoding="ascii", buffering=1024, size=None,
136157
block_transfer=False, request_crc_support=True):
137158
"""Open the data stream as a file like object.

0 commit comments

Comments
 (0)