Skip to content

Commit

Permalink
Minor linting nitpicks and Python 2 removal (#441)
Browse files Browse the repository at this point in the history
* Removed py2 handling in import_eds
* Made file open more robust in import_eds

* Additional whitespace removals
  • Loading branch information
sveinse authored May 26, 2024
1 parent 24df6e8 commit 94f337d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 29 deletions.
4 changes: 2 additions & 2 deletions canopen/lss.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,12 @@ def send_identify_non_configured_remote_slave(self):
self.__send_command(message)

def fast_scan(self):
"""This command sends a series of fastscan message
"""This command sends a series of fastscan message
to find unconfigured slave with lowest number of LSS idenities
:return:
True if a slave is found.
False if there is no candidate.
False if there is no candidate.
list is the LSS identities [vendor_id, product_code, revision_number, serial_number]
:rtype: bool, list
"""
Expand Down
2 changes: 1 addition & 1 deletion canopen/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def __init__(
bus,
remote: bool = False,
):
"""
"""
:param can_id:
CAN-ID of the message
:param data:
Expand Down
18 changes: 8 additions & 10 deletions canopen/objectdictionary/eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@
def import_eds(source, node_id):
eds = RawConfigParser(inline_comment_prefixes=(';',))
eds.optionxform = str
if hasattr(source, "read"):
fp = source
else:
fp = open(source)
opened_here = False
try:
# Python 3
if hasattr(source, "read"):
fp = source
else:
fp = open(source)
opened_here = True
eds.read_file(fp)
except AttributeError:
# Python 2
eds.readfp(fp)
finally:
# Only close object if opened in this fn
if not hasattr(source, "read"):
if opened_here:
fp.close()

od = ObjectDictionary()
Expand Down Expand Up @@ -208,7 +206,7 @@ def _calc_bit_length(data_type):
def _signed_int_from_hex(hex_str, bit_length):
number = int(hex_str, 0)
max_value = (1 << (bit_length - 1)) - 1

if number > max_value:
return number - (1 << bit_length)
else:
Expand Down
8 changes: 4 additions & 4 deletions canopen/sdo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def request_response(self, sdo_request):
except SdoCommunicationError as e:
retries_left -= 1
if not retries_left:
self.abort(0x5040000)
self.abort(0x5040000)
raise
logger.warning(str(e))

Expand Down Expand Up @@ -463,7 +463,7 @@ def __init__(self, sdo_client, index, subindex=0, request_crc_support=True):
:param int subindex:
Object dictionary sub-index to read from.
:param bool request_crc_support:
If crc calculation should be requested when using block transfer
If crc calculation should be requested when using block transfer
"""
self._done = False
self.sdo_client = sdo_client
Expand Down Expand Up @@ -628,7 +628,7 @@ def __init__(self, sdo_client, index, subindex=0, size=None, request_crc_support
:param int size:
Size of data in number of bytes if known in advance.
:param bool request_crc_support:
If crc calculation should be requested when using block transfer
If crc calculation should be requested when using block transfer
"""
self.sdo_client = sdo_client
self.size = size
Expand Down Expand Up @@ -755,7 +755,7 @@ def _block_ack(self):
logger.debug("Server requested a block size of %d", blksize)
self._blksize = blksize
self._seqno = 0

def _retransmit(self, ackseq, blksize):
"""Retransmit the failed block"""
logger.info(("%d of %d sequences were received. "
Expand Down
2 changes: 1 addition & 1 deletion canopen/sdo/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def download(
data: bytes,
force_segment: bool = False,
):
"""May be called to make a write operation without an Object Dictionary.
"""May be called to make a write operation without an Object Dictionary.
:param index:
Index of object to write.
Expand Down
6 changes: 2 additions & 4 deletions canopen/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ def raw(self) -> Union[int, bool, float, str, bytes]:
+---------------------------+----------------------------+
| REALxx | :class:`float` |
+---------------------------+----------------------------+
| VISIBLE_STRING | :class:`str` / |
| | ``unicode`` (Python 2) |
| VISIBLE_STRING | :class:`str` |
+---------------------------+----------------------------+
| UNICODE_STRING | :class:`str` / |
| | ``unicode`` (Python 2) |
| UNICODE_STRING | :class:`str` |
+---------------------------+----------------------------+
| OCTET_STRING | :class:`bytes` |
+---------------------------+----------------------------+
Expand Down
10 changes: 5 additions & 5 deletions examples/eds/e35.eds
Original file line number Diff line number Diff line change
Expand Up @@ -5083,7 +5083,7 @@ DefaultValue=0
PDOMapping=0x0

[2C01subB]
ParameterName=Accumulator divide register
ParameterName=Accumulator divide register
ObjectType=0x7
DataType=0x0004
AccessType=wo
Expand All @@ -5099,7 +5099,7 @@ DefaultValue=0
PDOMapping=0x0

[2C01subD]
ParameterName=Accumulator multiply register
ParameterName=Accumulator multiply register
ObjectType=0x7
DataType=0x0004
AccessType=wo
Expand Down Expand Up @@ -5131,15 +5131,15 @@ DefaultValue=0
PDOMapping=0x0

[2C01sub11]
ParameterName=Shift left accumulator by register
ParameterName=Shift left accumulator by register
ObjectType=0x7
DataType=0x0004
AccessType=wo
DefaultValue=0
PDOMapping=0x0

[2C01sub12]
ParameterName=Shift right accumulator by register
ParameterName=Shift right accumulator by register
ObjectType=0x7
DataType=0x0004
AccessType=wo
Expand Down Expand Up @@ -7835,7 +7835,7 @@ PDOMapping=0x0
ParameterValue=0x2

[1A02sub1]
ParameterName=Position Actual Value
ParameterName=Position Actual Value
ObjectType=0x7
DataType=0x0007
AccessType=rw
Expand Down
4 changes: 2 additions & 2 deletions test/test_eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_dummy_variable(self):
def test_dummy_variable_undefined(self):
with self.assertRaises(KeyError):
var_undef = self.od['Dummy0001']

def test_reading_factor(self):
var = self.od['EDS file extensions']['FactorAndDescription']
self.assertEqual(var.factor, 0.1)
Expand All @@ -167,7 +167,7 @@ def test_reading_factor(self):
self.assertEqual(var2.description, '')
self.assertEqual(var2.factor, 1)
self.assertEqual(var2.unit, '')



def test_comments(self):
Expand Down

0 comments on commit 94f337d

Please sign in to comment.