From 94f337d779c9294db99083821a6a834fe58b167a Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Sun, 26 May 2024 13:19:35 +0200 Subject: [PATCH] Minor linting nitpicks and Python 2 removal (#441) * Removed py2 handling in import_eds * Made file open more robust in import_eds * Additional whitespace removals --- canopen/lss.py | 4 ++-- canopen/network.py | 2 +- canopen/objectdictionary/eds.py | 18 ++++++++---------- canopen/sdo/client.py | 8 ++++---- canopen/sdo/server.py | 2 +- canopen/variable.py | 6 ++---- examples/eds/e35.eds | 10 +++++----- test/test_eds.py | 4 ++-- 8 files changed, 25 insertions(+), 29 deletions(-) diff --git a/canopen/lss.py b/canopen/lss.py index d197a5a7..83d225c5 100644 --- a/canopen/lss.py +++ b/canopen/lss.py @@ -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 """ diff --git a/canopen/network.py b/canopen/network.py index 4a5a23e0..9e9a252e 100644 --- a/canopen/network.py +++ b/canopen/network.py @@ -298,7 +298,7 @@ def __init__( bus, remote: bool = False, ): - """ + """ :param can_id: CAN-ID of the message :param data: diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index a952c8a5..b718c945 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -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() @@ -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: diff --git a/canopen/sdo/client.py b/canopen/sdo/client.py index 519bbd5f..e1a57acd 100644 --- a/canopen/sdo/client.py +++ b/canopen/sdo/client.py @@ -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)) @@ -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 @@ -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 @@ -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. " diff --git a/canopen/sdo/server.py b/canopen/sdo/server.py index e9574feb..3707d92c 100644 --- a/canopen/sdo/server.py +++ b/canopen/sdo/server.py @@ -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. diff --git a/canopen/variable.py b/canopen/variable.py index a75b68c0..892818ad 100644 --- a/canopen/variable.py +++ b/canopen/variable.py @@ -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` | +---------------------------+----------------------------+ diff --git a/examples/eds/e35.eds b/examples/eds/e35.eds index 17b24879..c8a02f9a 100644 --- a/examples/eds/e35.eds +++ b/examples/eds/e35.eds @@ -5083,7 +5083,7 @@ DefaultValue=0 PDOMapping=0x0 [2C01subB] -ParameterName=Accumulator divide register +ParameterName=Accumulator divide register ObjectType=0x7 DataType=0x0004 AccessType=wo @@ -5099,7 +5099,7 @@ DefaultValue=0 PDOMapping=0x0 [2C01subD] -ParameterName=Accumulator multiply register +ParameterName=Accumulator multiply register ObjectType=0x7 DataType=0x0004 AccessType=wo @@ -5131,7 +5131,7 @@ DefaultValue=0 PDOMapping=0x0 [2C01sub11] -ParameterName=Shift left accumulator by register +ParameterName=Shift left accumulator by register ObjectType=0x7 DataType=0x0004 AccessType=wo @@ -5139,7 +5139,7 @@ DefaultValue=0 PDOMapping=0x0 [2C01sub12] -ParameterName=Shift right accumulator by register +ParameterName=Shift right accumulator by register ObjectType=0x7 DataType=0x0004 AccessType=wo @@ -7835,7 +7835,7 @@ PDOMapping=0x0 ParameterValue=0x2 [1A02sub1] -ParameterName=Position Actual Value +ParameterName=Position Actual Value ObjectType=0x7 DataType=0x0007 AccessType=rw diff --git a/test/test_eds.py b/test/test_eds.py index a35cbb99..d86a6ae3 100644 --- a/test/test_eds.py +++ b/test/test_eds.py @@ -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) @@ -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):