From b97c64f9ada5a8976f86f3713e82b3e27da65226 Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sun, 24 Nov 2024 22:56:53 +0100 Subject: [PATCH 1/2] remove more Python2 hybridation --- examples/BusmasterRestbus.py | 1 - src/canmatrix/__init__.py | 1 - src/canmatrix/cancluster.py | 1 - src/canmatrix/cli/compare.py | 4 +--- src/canmatrix/cli/convert.py | 4 +--- src/canmatrix/compare.py | 6 ++---- src/canmatrix/convert.py | 2 -- src/canmatrix/copy.py | 2 -- src/canmatrix/formats/__init__.py | 1 - src/canmatrix/formats/arxml.py | 2 -- src/canmatrix/formats/csv.py | 20 ++++++-------------- src/canmatrix/formats/dbc.py | 2 -- src/canmatrix/formats/dbf.py | 1 - src/canmatrix/formats/fibex.py | 2 -- src/canmatrix/formats/json.py | 25 +++++++------------------ src/canmatrix/formats/kcd.py | 2 -- src/canmatrix/formats/ldf.py | 2 -- src/canmatrix/formats/odx.py | 2 -- src/canmatrix/formats/scapy.py | 2 -- src/canmatrix/formats/sym.py | 2 -- src/canmatrix/formats/xls.py | 2 -- src/canmatrix/formats/xls_common.py | 2 -- src/canmatrix/formats/xlsx.py | 2 -- src/canmatrix/formats/yaml.py | 2 -- src/canmatrix/j1939_decoder.py | 2 -- src/canmatrix/join.py | 1 - src/canmatrix/log.py | 2 -- src/canmatrix/utils.py | 5 +---- tests/createTestMatrix.py | 8 ++------ tests/test.py | 2 -- 30 files changed, 20 insertions(+), 92 deletions(-) diff --git a/examples/BusmasterRestbus.py b/examples/BusmasterRestbus.py index 96bc2b599..61941f827 100755 --- a/examples/BusmasterRestbus.py +++ b/examples/BusmasterRestbus.py @@ -19,7 +19,6 @@ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. -from __future__ import division import math from struct import * import zipfile diff --git a/src/canmatrix/__init__.py b/src/canmatrix/__init__.py index f8ddc5c8d..b8cb9a9b5 100644 --- a/src/canmatrix/__init__.py +++ b/src/canmatrix/__init__.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import import logging import canmatrix._version diff --git a/src/canmatrix/cancluster.py b/src/canmatrix/cancluster.py index 54155bff3..81e0bcd32 100644 --- a/src/canmatrix/cancluster.py +++ b/src/canmatrix/cancluster.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import, division, print_function import typing from builtins import * diff --git a/src/canmatrix/cli/compare.py b/src/canmatrix/cli/compare.py index 26a8788ae..c34c374dd 100644 --- a/src/canmatrix/cli/compare.py +++ b/src/canmatrix/cli/compare.py @@ -21,8 +21,6 @@ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. -from __future__ import absolute_import, division, print_function - import logging import sys import typing @@ -105,4 +103,4 @@ def cli_compare(matrix1, matrix2, verbosity, silent, check_comments, check_attri # to be run as module `python -m canmatrix.compare`, NOT as script with argument `canmatrix/compare.py` if __name__ == '__main__': - sys.exit(cli_compare()) \ No newline at end of file + sys.exit(cli_compare()) diff --git a/src/canmatrix/cli/convert.py b/src/canmatrix/cli/convert.py index 60595bd96..834c34ee8 100644 --- a/src/canmatrix/cli/convert.py +++ b/src/canmatrix/cli/convert.py @@ -21,8 +21,6 @@ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. -from __future__ import absolute_import, division, print_function - import logging import sys @@ -160,4 +158,4 @@ def cli_convert(infile, outfile, silent, verbosity, **options): if __name__ == '__main__': - sys.exit(cli_convert()) \ No newline at end of file + sys.exit(cli_convert()) diff --git a/src/canmatrix/compare.py b/src/canmatrix/compare.py index c18a83161..24ce0dccc 100644 --- a/src/canmatrix/compare.py +++ b/src/canmatrix/compare.py @@ -19,8 +19,6 @@ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. -from __future__ import absolute_import, division, print_function - import logging import sys import typing @@ -36,7 +34,7 @@ @attr.s -class CompareResult(object): +class CompareResult: """Hold comparison results in logical tree.""" result = attr.ib(default=None) # type: typing.Optional[str] # any of equal, added, deleted, changed type = attr.ib(default=None) # type: typing.Optional[str] # db, ecu, frame, signal, signalGroup or attribute @@ -508,4 +506,4 @@ def dump_result(res, depth=0): " new: " + str(res.changes[1])) for child in res.children: - dump_result(child, depth + 1) \ No newline at end of file + dump_result(child, depth + 1) diff --git a/src/canmatrix/convert.py b/src/canmatrix/convert.py index ddc3ea548..4c4151ca4 100644 --- a/src/canmatrix/convert.py +++ b/src/canmatrix/convert.py @@ -19,8 +19,6 @@ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. -from __future__ import absolute_import, division, print_function - import copy import logging import sys diff --git a/src/canmatrix/copy.py b/src/canmatrix/copy.py index 6ceeca4ab..a2a881a64 100644 --- a/src/canmatrix/copy.py +++ b/src/canmatrix/copy.py @@ -19,8 +19,6 @@ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. -from __future__ import absolute_import, division, print_function - import copy import logging import typing diff --git a/src/canmatrix/formats/__init__.py b/src/canmatrix/formats/__init__.py index d3d3b979f..28dd6b9e0 100644 --- a/src/canmatrix/formats/__init__.py +++ b/src/canmatrix/formats/__init__.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import, division, print_function import importlib import logging diff --git a/src/canmatrix/formats/arxml.py b/src/canmatrix/formats/arxml.py index 6ea6df74f..bff1970e3 100644 --- a/src/canmatrix/formats/arxml.py +++ b/src/canmatrix/formats/arxml.py @@ -25,8 +25,6 @@ # currently Support for Autosar 3.2 and 4.0-4.3 is planned # AUTOSAR 4.2.2 is partial support -> 2024/05/20 -from __future__ import absolute_import, division, print_function - import copy import decimal import logging diff --git a/src/canmatrix/formats/csv.py b/src/canmatrix/formats/csv.py index 5f475acd8..e5e151ac5 100644 --- a/src/canmatrix/formats/csv.py +++ b/src/canmatrix/formats/csv.py @@ -23,8 +23,6 @@ # this script exports canmatrix-objects to a CSV file. (Based on xlsx) # Author: Martin Hoffmann (m8ddin@gmail.com) -from __future__ import absolute_import, division, print_function - import collections import csv import logging @@ -47,9 +45,6 @@ def __getitem__(self, key): # type: (int) -> CsvDataType return self._row_dict[key] def __setitem__(self, key, item): # type: (int, CsvDataType) -> None - if sys.version_info <= (3, 0): - if type(item).__name__ == "unicode": - item = item.encode('utf-8') self._row_dict[key] = item def __add__(self, other): # type: (typing.Iterable[CsvDataType]) -> CsvRow @@ -263,13 +258,10 @@ def dump(db, file_object, delimiter=',', **options): # loop over signals ends here # loop over frames ends here - if sys.version_info > (3, 0): + try: import io temp = io.TextIOWrapper(file_object, encoding='UTF-8') - else: - temp = file_object - - try: + writer = csv.writer(temp, delimiter=delimiter) for csv_row in csv_table: writer.writerow(csv_row.as_list) @@ -279,7 +271,7 @@ def dump(db, file_object, delimiter=',', **options): # [row.toCSV(delimiter) for row in csv_table]) # print(finalTableString) finally: - if sys.version_info > (3, 0): - # When TextIOWrapper is garbage collected, it closes the raw stream - # unless the raw stream is detached first - temp.detach() + # When TextIOWrapper is garbage collected, it closes the raw stream + # unless the raw stream is detached first + temp.detach() + diff --git a/src/canmatrix/formats/dbc.py b/src/canmatrix/formats/dbc.py index 965459e29..844002181 100644 --- a/src/canmatrix/formats/dbc.py +++ b/src/canmatrix/formats/dbc.py @@ -23,8 +23,6 @@ # this script exports dbc-files from a canmatrix-object # dbc-files are the can-matrix-definitions of the CANoe (Vector Informatic) -from __future__ import absolute_import, division, print_function - import collections import copy import decimal diff --git a/src/canmatrix/formats/dbf.py b/src/canmatrix/formats/dbf.py index ee45bae2b..8b5a780f9 100644 --- a/src/canmatrix/formats/dbf.py +++ b/src/canmatrix/formats/dbf.py @@ -23,7 +23,6 @@ # this script imports dbf-files in a canmatrix-object # dbf-files are the can-matrix-definitions of the busmaster-project (http://rbei-etas.github.io/busmaster/) # -from __future__ import absolute_import, division, print_function import copy import decimal diff --git a/src/canmatrix/formats/fibex.py b/src/canmatrix/formats/fibex.py index 95a7601dc..173bffaf2 100644 --- a/src/canmatrix/formats/fibex.py +++ b/src/canmatrix/formats/fibex.py @@ -25,8 +25,6 @@ # only (fibex: Field Bus Exchange Format // # https://de.wikipedia.org/wiki/Field_Bus_Exchange_Format) -from __future__ import absolute_import, division, print_function - import os import typing from builtins import * diff --git a/src/canmatrix/formats/json.py b/src/canmatrix/formats/json.py index bffc43305..7ce56b047 100644 --- a/src/canmatrix/formats/json.py +++ b/src/canmatrix/formats/json.py @@ -24,10 +24,7 @@ # json-files are the can-matrix-definitions of the CANard-project # (https://github.com/ericevenchick/CANard) -from __future__ import absolute_import, division, print_function - import json -import sys import typing from builtins import * import decimal @@ -181,20 +178,16 @@ def dump(db, f, **options): "header_id": frame.header_id, "pdu_name": frame.pdu_name, "transmitters": frame.transmitters}) - if sys.version_info > (3, 0): - import io - temp = io.TextIOWrapper(f, encoding='UTF-8') - else: - temp = f + import io + temp = io.TextIOWrapper(f, encoding='UTF-8') try: json.dump(export_dict, temp, sort_keys=True, indent=4, separators=(',', ': ')) finally: - if sys.version_info > (3, 0): - # When TextIOWrapper is garbage collected, it closes the raw stream - # unless the raw stream is detached first - temp.detach() + # When TextIOWrapper is garbage collected, it closes the raw stream + # unless the raw stream is detached first + temp.detach() def load(f, **_options): @@ -202,12 +195,8 @@ def load(f, **_options): db = canmatrix.CanMatrix() - if sys.version_info > (3, 0): - import io - json_data = json.load(io.TextIOWrapper(f, encoding='UTF-8')) - else: - - json_data = json.load(f) + import io + json_data = json.load(io.TextIOWrapper(f, encoding='UTF-8')) if "enumerations" in json_data: for val_tab_name, val_tab_dict in json_data['enumerations'].items(): diff --git a/src/canmatrix/formats/kcd.py b/src/canmatrix/formats/kcd.py index 929f5b354..2abf64c71 100644 --- a/src/canmatrix/formats/kcd.py +++ b/src/canmatrix/formats/kcd.py @@ -24,8 +24,6 @@ # kcd-files are the can-matrix-definitions of the kayak # (http://kayak.2codeornot2code.org/) -from __future__ import absolute_import, division, print_function - import decimal import os import re diff --git a/src/canmatrix/formats/ldf.py b/src/canmatrix/formats/ldf.py index c239724ef..51de6da8d 100644 --- a/src/canmatrix/formats/ldf.py +++ b/src/canmatrix/formats/ldf.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import - import ldfparser import canmatrix import ldfparser.encoding diff --git a/src/canmatrix/formats/odx.py b/src/canmatrix/formats/odx.py index 763bcdba9..27e976a16 100644 --- a/src/canmatrix/formats/odx.py +++ b/src/canmatrix/formats/odx.py @@ -1,5 +1,3 @@ -from __future__ import print_function - from lxml import etree import canmatrix.formats import decimal diff --git a/src/canmatrix/formats/scapy.py b/src/canmatrix/formats/scapy.py index 7a5c47d4a..0f8219aee 100644 --- a/src/canmatrix/formats/scapy.py +++ b/src/canmatrix/formats/scapy.py @@ -22,8 +22,6 @@ # this script exports scapy python files # https://scapy.readthedocs.io/en/latest/advanced_usage.html#automotive-usage -from __future__ import absolute_import, division, print_function - import textwrap import typing from builtins import * diff --git a/src/canmatrix/formats/sym.py b/src/canmatrix/formats/sym.py index 60baa48db..4ce3ca3bd 100644 --- a/src/canmatrix/formats/sym.py +++ b/src/canmatrix/formats/sym.py @@ -23,8 +23,6 @@ # this script exports sym-files from a canmatrix-object # sym-files are the can-matrix-definitions of the Peak Systems Tools -from __future__ import absolute_import, division, print_function - import collections import decimal import logging diff --git a/src/canmatrix/formats/xls.py b/src/canmatrix/formats/xls.py index e9a7612f0..b25bb2808 100644 --- a/src/canmatrix/formats/xls.py +++ b/src/canmatrix/formats/xls.py @@ -23,8 +23,6 @@ # this script exports xls-files from a canmatrix-object # xls-files are the can-matrix-definitions displayed in Excel -from __future__ import absolute_import, division, print_function - import decimal import logging import typing diff --git a/src/canmatrix/formats/xls_common.py b/src/canmatrix/formats/xls_common.py index a4a3c9a6b..dc792233d 100644 --- a/src/canmatrix/formats/xls_common.py +++ b/src/canmatrix/formats/xls_common.py @@ -19,8 +19,6 @@ # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. -from __future__ import absolute_import, division, print_function - import typing from builtins import * diff --git a/src/canmatrix/formats/xlsx.py b/src/canmatrix/formats/xlsx.py index 5f24348a8..68d7ce1dd 100644 --- a/src/canmatrix/formats/xlsx.py +++ b/src/canmatrix/formats/xlsx.py @@ -23,8 +23,6 @@ # this script exports xls-files from a canmatrix-object # xls-files are the can-matrix-definitions displayed in Excel -from __future__ import absolute_import, division, print_function - import logging import typing from builtins import * diff --git a/src/canmatrix/formats/yaml.py b/src/canmatrix/formats/yaml.py index 0cb656b29..0efdeeb96 100644 --- a/src/canmatrix/formats/yaml.py +++ b/src/canmatrix/formats/yaml.py @@ -23,8 +23,6 @@ # yaml-files are just object-dumps human readable. # This export is complete, no information lost -from __future__ import absolute_import, division, print_function - import copy import typing from builtins import * diff --git a/src/canmatrix/j1939_decoder.py b/src/canmatrix/j1939_decoder.py index 3cd03c6f4..840047dca 100644 --- a/src/canmatrix/j1939_decoder.py +++ b/src/canmatrix/j1939_decoder.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import, division, print_function - from builtins import * import attr diff --git a/src/canmatrix/join.py b/src/canmatrix/join.py index a2b921ab8..3b143b62e 100644 --- a/src/canmatrix/join.py +++ b/src/canmatrix/join.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import, division, print_function import typing from builtins import * diff --git a/src/canmatrix/log.py b/src/canmatrix/log.py index 3a38e0f10..9db1f693a 100644 --- a/src/canmatrix/log.py +++ b/src/canmatrix/log.py @@ -22,8 +22,6 @@ # Configurable logging # Author: Martin Hoffmann (m8ddin@gmail.com) -from __future__ import absolute_import, division, print_function - import logging diff --git a/src/canmatrix/utils.py b/src/canmatrix/utils.py index edf699669..2074481ca 100644 --- a/src/canmatrix/utils.py +++ b/src/canmatrix/utils.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import absolute_import, division, print_function import csv import shlex @@ -16,9 +15,7 @@ def quote_aware_space_split(in_line): # type: (str) -> typing.List[str] - if sys.version_info >= (3, 0): # is there a clean way to to it? - return shlex.split(in_line.strip()) - return [item.decode('utf-8') for item in shlex.split(in_line.strip().encode('utf-8'))] + return shlex.split(in_line.strip()) # https://stackoverflow.com/questions/18092354/python-split-string-without-splitting-escaped-character diff --git a/tests/createTestMatrix.py b/tests/createTestMatrix.py index 1645e963a..fbfa5cb14 100644 --- a/tests/createTestMatrix.py +++ b/tests/createTestMatrix.py @@ -17,12 +17,8 @@ myFrame = Frame("testFrame1", Id=0x123, dlc=8, transmitter="testBU") -if sys.version_info > (3, 0): - unit = u"specialCharUnit°$" - comment = u"Multi \n Line \n Signal comment with a-umlaut: ä" -else: - unit = "specialCharUnit°$".decode("utf-8") - comment = "Multi \n Line \n Signal comment with a-umlaut: ä".decode("utf-8") +unit = "specialCharUnit°$" +comment = "Multi \n Line \n Signal comment with a-umlaut: ä" mySignal = Signal("someTestSignal", signalSize=11, diff --git a/tests/test.py b/tests/test.py index 90f0983a5..1b9887d93 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,7 +1,5 @@ #!/usr/bin/env python3 -from __future__ import absolute_import, division, print_function - import copy import os import shutil From 6e08f36623573ab9422842a0ca995ec8433c947a Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sun, 24 Nov 2024 23:15:45 +0100 Subject: [PATCH 2/2] re-reduce scope of try/catch block --- src/canmatrix/formats/csv.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/canmatrix/formats/csv.py b/src/canmatrix/formats/csv.py index e5e151ac5..90918fafa 100644 --- a/src/canmatrix/formats/csv.py +++ b/src/canmatrix/formats/csv.py @@ -258,10 +258,10 @@ def dump(db, file_object, delimiter=',', **options): # loop over signals ends here # loop over frames ends here + import io + temp = io.TextIOWrapper(file_object, encoding='UTF-8') + try: - import io - temp = io.TextIOWrapper(file_object, encoding='UTF-8') - writer = csv.writer(temp, delimiter=delimiter) for csv_row in csv_table: writer.writerow(csv_row.as_list)