From ffbd10f44da89e407622c82b8587fcc64e3f2f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Fri, 18 Oct 2024 09:20:29 +0200 Subject: [PATCH] Clean up imports ordering. Follow recommendations from flake8 regarding import order and grouping. Put two blank lines after the top import block in each file. Import CanError directly from can package in canopen.sdo.client instead of from canopen.network, where it was not even used. --- canopen/__init__.py | 12 +++++++++--- canopen/emcy.py | 2 +- canopen/lss.py | 4 ++-- canopen/network.py | 14 +++++++------- canopen/nmt.py | 2 +- canopen/node/__init__.py | 2 +- canopen/node/local.py | 11 ++++++----- canopen/node/remote.py | 11 ++++++----- canopen/objectdictionary/__init__.py | 6 ++++-- canopen/objectdictionary/datatypes.py | 1 + canopen/objectdictionary/eds.py | 3 ++- canopen/objectdictionary/epf.py | 3 ++- canopen/pdo/base.py | 12 +++++++----- canopen/profiles/p402.py | 1 + canopen/profiles/tools/test_p402_states.py | 2 +- canopen/sdo/__init__.py | 6 +++--- canopen/sdo/base.py | 2 +- canopen/sdo/client.py | 12 +++++++----- canopen/sdo/server.py | 1 + canopen/timestamp.py | 3 ++- canopen/utils.py | 1 + canopen/variable.py | 3 ++- examples/simple_ds402_node.py | 7 ++++--- test/test_eds.py | 3 ++- test/test_emcy.py | 1 + test/test_local.py | 1 + test/test_network.py | 5 +++-- test/test_nmt.py | 4 +++- test/test_od.py | 1 + test/test_pdo.py | 1 + test/test_sdo.py | 3 ++- test/test_sync.py | 1 + test/test_time.py | 1 + test/test_utils.py | 1 + 34 files changed, 89 insertions(+), 54 deletions(-) diff --git a/canopen/__init__.py b/canopen/__init__.py index 9bd19b1d..ab1ccf37 100644 --- a/canopen/__init__.py +++ b/canopen/__init__.py @@ -1,8 +1,14 @@ from canopen.network import Network, NodeScanner -from canopen.node import RemoteNode, LocalNode -from canopen.sdo import SdoCommunicationError, SdoAbortedError -from canopen.objectdictionary import import_od, export_od, ObjectDictionary, ObjectDictionaryError +from canopen.node import LocalNode, RemoteNode +from canopen.objectdictionary import ( + ObjectDictionary, + ObjectDictionaryError, + export_od, + import_od, +) from canopen.profiles.p402 import BaseNode402 +from canopen.sdo import SdoAbortedError, SdoCommunicationError + try: from canopen._version import version as __version__ except ImportError: diff --git a/canopen/emcy.py b/canopen/emcy.py index 520cfdc0..ec2c489f 100644 --- a/canopen/emcy.py +++ b/canopen/emcy.py @@ -1,5 +1,5 @@ -import struct import logging +import struct import threading import time from typing import Callable, List, Optional diff --git a/canopen/lss.py b/canopen/lss.py index 5ffcc11b..7c0b92a6 100644 --- a/canopen/lss.py +++ b/canopen/lss.py @@ -1,7 +1,7 @@ import logging -import time -import struct import queue +import struct +import time import canopen.network diff --git a/canopen/network.py b/canopen/network.py index 9d6e66c6..02bec899 100644 --- a/canopen/network.py +++ b/canopen/network.py @@ -1,21 +1,21 @@ from __future__ import annotations -from collections.abc import MutableMapping import logging import threading +from collections.abc import MutableMapping from typing import Callable, Dict, Final, Iterator, List, Optional, Union import can from can import Listener -from can import CanError -from canopen.node import RemoteNode, LocalNode -from canopen.sync import SyncProducer -from canopen.timestamp import TimeProducer -from canopen.nmt import NmtMaster from canopen.lss import LssMaster -from canopen.objectdictionary.eds import import_from_node +from canopen.nmt import NmtMaster +from canopen.node import LocalNode, RemoteNode from canopen.objectdictionary import ObjectDictionary +from canopen.objectdictionary.eds import import_from_node +from canopen.sync import SyncProducer +from canopen.timestamp import TimeProducer + logger = logging.getLogger(__name__) diff --git a/canopen/nmt.py b/canopen/nmt.py index 6f29e917..622e0a33 100644 --- a/canopen/nmt.py +++ b/canopen/nmt.py @@ -1,6 +1,6 @@ -import threading import logging import struct +import threading import time from typing import Callable, Optional, TYPE_CHECKING diff --git a/canopen/node/__init__.py b/canopen/node/__init__.py index 31fed19e..704c8564 100644 --- a/canopen/node/__init__.py +++ b/canopen/node/__init__.py @@ -1,2 +1,2 @@ -from canopen.node.remote import RemoteNode from canopen.node.local import LocalNode +from canopen.node.remote import RemoteNode diff --git a/canopen/node/local.py b/canopen/node/local.py index eb614601..8f2493d9 100644 --- a/canopen/node/local.py +++ b/canopen/node/local.py @@ -4,13 +4,14 @@ from typing import Dict, Union import canopen.network -from canopen.node.base import BaseNode -from canopen.sdo import SdoServer, SdoAbortedError -from canopen.pdo import PDO, TPDO, RPDO -from canopen.nmt import NmtSlave +from canopen import objectdictionary from canopen.emcy import EmcyProducer +from canopen.nmt import NmtSlave +from canopen.node.base import BaseNode from canopen.objectdictionary import ObjectDictionary -from canopen import objectdictionary +from canopen.pdo import PDO, RPDO, TPDO +from canopen.sdo import SdoAbortedError, SdoServer + logger = logging.getLogger(__name__) diff --git a/canopen/node/remote.py b/canopen/node/remote.py index b354b8f9..226c0c0f 100644 --- a/canopen/node/remote.py +++ b/canopen/node/remote.py @@ -1,15 +1,16 @@ from __future__ import annotations import logging -from typing import Union, TextIO +from typing import TextIO, Union import canopen.network -from canopen.sdo import SdoClient, SdoCommunicationError, SdoAbortedError -from canopen.nmt import NmtMaster from canopen.emcy import EmcyConsumer -from canopen.pdo import TPDO, RPDO, PDO -from canopen.objectdictionary import ODRecord, ODArray, ODVariable, ObjectDictionary +from canopen.nmt import NmtMaster from canopen.node.base import BaseNode +from canopen.objectdictionary import ODArray, ODRecord, ODVariable, ObjectDictionary +from canopen.pdo import PDO, RPDO, TPDO +from canopen.sdo import SdoAbortedError, SdoClient, SdoCommunicationError + logger = logging.getLogger(__name__) diff --git a/canopen/objectdictionary/__init__.py b/canopen/objectdictionary/__init__.py index 1e80283b..09fe6e03 100644 --- a/canopen/objectdictionary/__init__.py +++ b/canopen/objectdictionary/__init__.py @@ -1,17 +1,19 @@ """ Object Dictionary module """ + from __future__ import annotations +import logging import struct +from collections.abc import Mapping, MutableMapping from typing import Dict, Iterator, List, Optional, TextIO, Union -from collections.abc import MutableMapping, Mapping -import logging from canopen.objectdictionary.datatypes import * from canopen.objectdictionary.datatypes import IntegerN, UnsignedN from canopen.utils import pretty_index + logger = logging.getLogger(__name__) diff --git a/canopen/objectdictionary/datatypes.py b/canopen/objectdictionary/datatypes.py index ad37fe6f..e4afd673 100644 --- a/canopen/objectdictionary/datatypes.py +++ b/canopen/objectdictionary/datatypes.py @@ -1,5 +1,6 @@ import struct + BOOLEAN = 0x1 INTEGER8 = 0x2 INTEGER16 = 0x3 diff --git a/canopen/objectdictionary/eds.py b/canopen/objectdictionary/eds.py index 3884d809..b77f9782 100644 --- a/canopen/objectdictionary/eds.py +++ b/canopen/objectdictionary/eds.py @@ -1,12 +1,13 @@ import copy import logging import re -from configparser import RawConfigParser, NoOptionError, NoSectionError +from configparser import NoOptionError, NoSectionError, RawConfigParser from canopen import objectdictionary from canopen.objectdictionary import ObjectDictionary, datatypes from canopen.sdo import SdoClient + logger = logging.getLogger(__name__) # Object type. Don't confuse with Data type diff --git a/canopen/objectdictionary/epf.py b/canopen/objectdictionary/epf.py index 46ffe59e..02d62033 100644 --- a/canopen/objectdictionary/epf.py +++ b/canopen/objectdictionary/epf.py @@ -1,9 +1,10 @@ -import xml.etree.ElementTree as etree import logging +import xml.etree.ElementTree as etree from canopen import objectdictionary from canopen.objectdictionary import ObjectDictionary + logger = logging.getLogger(__name__) DATA_TYPES = { diff --git a/canopen/pdo/base.py b/canopen/pdo/base.py index dc81aa60..0ba65199 100644 --- a/canopen/pdo/base.py +++ b/canopen/pdo/base.py @@ -1,21 +1,23 @@ from __future__ import annotations -import threading + +import binascii +import logging import math -from typing import Callable, Dict, Iterator, List, Optional, Union, TYPE_CHECKING +import threading from collections.abc import Mapping -import logging -import binascii +from typing import Callable, Dict, Iterator, List, Optional, TYPE_CHECKING, Union import canopen.network -from canopen.sdo import SdoAbortedError from canopen import objectdictionary from canopen import variable +from canopen.sdo import SdoAbortedError if TYPE_CHECKING: from canopen import LocalNode, RemoteNode from canopen.pdo import RPDO, TPDO from canopen.sdo import SdoRecord + PDO_NOT_VALID = 1 << 31 RTR_NOT_ALLOWED = 1 << 30 diff --git a/canopen/profiles/p402.py b/canopen/profiles/p402.py index 10156d01..88d86aad 100644 --- a/canopen/profiles/p402.py +++ b/canopen/profiles/p402.py @@ -7,6 +7,7 @@ from canopen.pdo import PdoMap from canopen.sdo import SdoCommunicationError + logger = logging.getLogger(__name__) diff --git a/canopen/profiles/tools/test_p402_states.py b/canopen/profiles/tools/test_p402_states.py index 721865db..6bb147ad 100644 --- a/canopen/profiles/tools/test_p402_states.py +++ b/canopen/profiles/tools/test_p402_states.py @@ -7,7 +7,7 @@ """ from canopen.objectdictionary import ObjectDictionary -from canopen.profiles.p402 import State402, BaseNode402 +from canopen.profiles.p402 import BaseNode402, State402 if __name__ == '__main__': diff --git a/canopen/sdo/__init__.py b/canopen/sdo/__init__.py index 39b8478c..25128605 100644 --- a/canopen/sdo/__init__.py +++ b/canopen/sdo/__init__.py @@ -1,7 +1,7 @@ -from canopen.sdo.base import SdoVariable, SdoRecord, SdoArray +from canopen.sdo.base import SdoArray, SdoRecord, SdoVariable from canopen.sdo.client import SdoClient -from canopen.sdo.server import SdoServer from canopen.sdo.exceptions import SdoAbortedError, SdoCommunicationError +from canopen.sdo.server import SdoServer # Compatibility -from canopen.sdo.base import Variable, Record, Array +from canopen.sdo.base import Array, Record, Variable diff --git a/canopen/sdo/base.py b/canopen/sdo/base.py index 39b10634..ddc75ed9 100644 --- a/canopen/sdo/base.py +++ b/canopen/sdo/base.py @@ -1,8 +1,8 @@ from __future__ import annotations import binascii -from typing import Iterator, Optional, Union from collections.abc import Mapping +from typing import Iterator, Optional, Union import canopen.network from canopen import objectdictionary diff --git a/canopen/sdo/client.py b/canopen/sdo/client.py index e4c50aa8..421fc3d4 100644 --- a/canopen/sdo/client.py +++ b/canopen/sdo/client.py @@ -1,15 +1,17 @@ -import struct -import logging import io -import time +import logging import queue +import struct +import time + +from can import CanError -from canopen.network import CanError from canopen import objectdictionary from canopen.sdo.base import SdoBase -from canopen.utils import pretty_index from canopen.sdo.constants import * from canopen.sdo.exceptions import * +from canopen.utils import pretty_index + logger = logging.getLogger(__name__) diff --git a/canopen/sdo/server.py b/canopen/sdo/server.py index e2a16e61..c6a4c27c 100644 --- a/canopen/sdo/server.py +++ b/canopen/sdo/server.py @@ -4,6 +4,7 @@ from canopen.sdo.constants import * from canopen.sdo.exceptions import * + logger = logging.getLogger(__name__) diff --git a/canopen/timestamp.py b/canopen/timestamp.py index f3004da2..21dcc636 100644 --- a/canopen/timestamp.py +++ b/canopen/timestamp.py @@ -1,7 +1,8 @@ -import time import struct +import time from typing import Optional + # 1 Jan 1984 OFFSET = 441763200 diff --git a/canopen/utils.py b/canopen/utils.py index 37feda93..7ddffda3 100644 --- a/canopen/utils.py +++ b/canopen/utils.py @@ -1,4 +1,5 @@ """Additional utility functions for canopen.""" + from typing import Optional, Union diff --git a/canopen/variable.py b/canopen/variable.py index 3ec67c79..d2538c3f 100644 --- a/canopen/variable.py +++ b/canopen/variable.py @@ -1,10 +1,11 @@ import logging -from typing import Union from collections.abc import Mapping +from typing import Union from canopen import objectdictionary from canopen.utils import pretty_index + logger = logging.getLogger(__name__) diff --git a/examples/simple_ds402_node.py b/examples/simple_ds402_node.py index b9b96180..7b24b9b5 100644 --- a/examples/simple_ds402_node.py +++ b/examples/simple_ds402_node.py @@ -1,9 +1,10 @@ -import canopen -import sys import os +import sys +import time import traceback -import time +import canopen + try: diff --git a/test/test_eds.py b/test/test_eds.py index 12c3c2fa..68f5ad3c 100644 --- a/test/test_eds.py +++ b/test/test_eds.py @@ -4,7 +4,8 @@ import canopen from canopen.objectdictionary.eds import _signed_int_from_hex from canopen.utils import pretty_index -from .util import SAMPLE_EDS, DATATYPES_EDS, tmp_file + +from .util import DATATYPES_EDS, SAMPLE_EDS, tmp_file class TestEDS(unittest.TestCase): diff --git a/test/test_emcy.py b/test/test_emcy.py index f9ad87e4..d883e9c8 100644 --- a/test/test_emcy.py +++ b/test/test_emcy.py @@ -4,6 +4,7 @@ from contextlib import contextmanager import can + import canopen from canopen.emcy import EmcyError diff --git a/test/test_local.py b/test/test_local.py index 5f6d477b..e184c040 100644 --- a/test/test_local.py +++ b/test/test_local.py @@ -2,6 +2,7 @@ import unittest import canopen + from .util import SAMPLE_EDS diff --git a/test/test_network.py b/test/test_network.py index c08baaca..1d45a1c2 100644 --- a/test/test_network.py +++ b/test/test_network.py @@ -1,10 +1,11 @@ import logging -import threading import time import unittest -import canopen import can + +import canopen + from .util import SAMPLE_EDS diff --git a/test/test_nmt.py b/test/test_nmt.py index 85efb11d..636126dc 100644 --- a/test/test_nmt.py +++ b/test/test_nmt.py @@ -3,8 +3,10 @@ import unittest import can + import canopen -from canopen.nmt import COMMAND_TO_STATE, NMT_STATES, NMT_COMMANDS, NmtError +from canopen.nmt import COMMAND_TO_STATE, NMT_COMMANDS, NMT_STATES, NmtError + from .util import SAMPLE_EDS diff --git a/test/test_od.py b/test/test_od.py index 2c2b9591..52de86f8 100644 --- a/test/test_od.py +++ b/test/test_od.py @@ -1,4 +1,5 @@ import unittest + from canopen import objectdictionary as od diff --git a/test/test_pdo.py b/test/test_pdo.py index e3ad219f..1badc89d 100644 --- a/test/test_pdo.py +++ b/test/test_pdo.py @@ -1,6 +1,7 @@ import unittest import canopen + from .util import SAMPLE_EDS, tmp_file diff --git a/test/test_sdo.py b/test/test_sdo.py index 993d4acc..6fe8544c 100644 --- a/test/test_sdo.py +++ b/test/test_sdo.py @@ -3,7 +3,8 @@ import canopen import canopen.objectdictionary.datatypes as dt from canopen.objectdictionary import ODVariable -from .util import SAMPLE_EDS, DATATYPES_EDS + +from .util import DATATYPES_EDS, SAMPLE_EDS TX = 1 diff --git a/test/test_sync.py b/test/test_sync.py index f376d2a1..93633538 100644 --- a/test/test_sync.py +++ b/test/test_sync.py @@ -2,6 +2,7 @@ import unittest import can + import canopen diff --git a/test/test_time.py b/test/test_time.py index 71a84622..acd2b490 100644 --- a/test/test_time.py +++ b/test/test_time.py @@ -1,4 +1,5 @@ import unittest + import canopen diff --git a/test/test_utils.py b/test/test_utils.py index b412e365..a17cce92 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,4 +1,5 @@ import unittest + from canopen.utils import pretty_index