diff --git a/.github/actions/linux_armv7l/Dockerfile b/.github/actions/linux_armv7l/Dockerfile index e1abeb77..6cb05aa7 100644 --- a/.github/actions/linux_armv7l/Dockerfile +++ b/.github/actions/linux_armv7l/Dockerfile @@ -3,4 +3,4 @@ FROM ghcr.io/nikteliy/manylinux_2_24_armv7l:python3.7 COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/entrypoint.sh"] diff --git a/.github/actions/linux_armv7l/action.yml b/.github/actions/linux_armv7l/action.yml index 3696e22b..937006ea 100644 --- a/.github/actions/linux_armv7l/action.yml +++ b/.github/actions/linux_armv7l/action.yml @@ -25,4 +25,4 @@ runs: - ${{ inputs.platform }} - ${{ inputs.makefile }} - ${{ inputs.python }} - - ${{ inputs.wheeldir }} \ No newline at end of file + - ${{ inputs.wheeldir }} diff --git a/.github/actions/linux_armv7l/entrypoint.sh b/.github/actions/linux_armv7l/entrypoint.sh index 78227523..000725cb 100755 --- a/.github/actions/linux_armv7l/entrypoint.sh +++ b/.github/actions/linux_armv7l/entrypoint.sh @@ -4,4 +4,4 @@ set -o errexit set -o pipefail set -o nounset -exec "$INPUT_SCRIPT" \ No newline at end of file +exec "$INPUT_SCRIPT" diff --git a/.github/actions/manylinux_2_24_aarch64/Dockerfile b/.github/actions/manylinux_2_24_aarch64/Dockerfile index 07792519..5c304e38 100644 --- a/.github/actions/manylinux_2_24_aarch64/Dockerfile +++ b/.github/actions/manylinux_2_24_aarch64/Dockerfile @@ -3,4 +3,4 @@ FROM quay.io/pypa/manylinux_2_24_aarch64:latest COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/entrypoint.sh"] diff --git a/.github/actions/manylinux_2_24_aarch64/action.yml b/.github/actions/manylinux_2_24_aarch64/action.yml index dc7e8de5..8235ee53 100644 --- a/.github/actions/manylinux_2_24_aarch64/action.yml +++ b/.github/actions/manylinux_2_24_aarch64/action.yml @@ -25,4 +25,4 @@ runs: - ${{ inputs.platform }} - ${{ inputs.makefile }} - ${{ inputs.python }} - - ${{ inputs.wheeldir }} \ No newline at end of file + - ${{ inputs.wheeldir }} diff --git a/.github/actions/manylinux_2_24_x86_64/Dockerfile b/.github/actions/manylinux_2_24_x86_64/Dockerfile index c31cf96e..1460d38e 100644 --- a/.github/actions/manylinux_2_24_x86_64/Dockerfile +++ b/.github/actions/manylinux_2_24_x86_64/Dockerfile @@ -3,4 +3,4 @@ FROM quay.io/pypa/manylinux_2_24_x86_64:latest COPY /entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/entrypoint.sh"] diff --git a/.github/actions/manylinux_2_24_x86_64/action.yml b/.github/actions/manylinux_2_24_x86_64/action.yml index 1215d8a4..72688a51 100644 --- a/.github/actions/manylinux_2_24_x86_64/action.yml +++ b/.github/actions/manylinux_2_24_x86_64/action.yml @@ -25,4 +25,4 @@ runs: - ${{ inputs.platform }} - ${{ inputs.makefile }} - ${{ inputs.python }} - - ${{ inputs.wheeldir }} \ No newline at end of file + - ${{ inputs.wheeldir }} diff --git a/.github/actions/prepare_snap7/action.yml b/.github/actions/prepare_snap7/action.yml index b1df1ac1..2669003c 100644 --- a/.github/actions/prepare_snap7/action.yml +++ b/.github/actions/prepare_snap7/action.yml @@ -14,7 +14,7 @@ runs: with: path: snap7-full-1.4.2.7z key: ${{ inputs.snap7-archive-url }} - + - name: Install choco packages if: steps.snap7-archive.outputs.cache-hit != 'true' && runner.os == 'Windows' shell: bash @@ -31,4 +31,4 @@ runs: - name: Update wheel shell: bash - run: python3 -m pip install --upgrade pip wheel build \ No newline at end of file + run: python3 -m pip install --upgrade pip wheel build diff --git a/.github/build_scripts/build_package.sh b/.github/build_scripts/build_package.sh index 9eedcd04..40599006 100755 --- a/.github/build_scripts/build_package.sh +++ b/.github/build_scripts/build_package.sh @@ -10,4 +10,3 @@ ${INPUT_PYTHON} -m pip install wheel build auditwheel patchelf ${INPUT_PYTHON} -m build . --wheel -C="--build-option=--plat-name=${INPUT_PLATFORM}" auditwheel repair dist/*${INPUT_PLATFORM}.whl --plat ${INPUT_PLATFORM} -w ${INPUT_WHEELDIR} - diff --git a/.github/workflows/build-and-test-arm32v7.yml b/.github/workflows/build-and-test-arm32v7.yml index a9540249..5c91caa8 100644 --- a/.github/workflows/build-and-test-arm32v7.yml +++ b/.github/workflows/build-and-test-arm32v7.yml @@ -11,7 +11,7 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 - + - name: Prepare snap7 archive uses: ./.github/actions/prepare_snap7 @@ -19,7 +19,7 @@ jobs: uses: docker/setup-qemu-action@v2 with: platforms: arm - + - name: Build wheel uses: ./.github/actions/linux_armv7l with: @@ -55,14 +55,13 @@ jobs: uses: docker/setup-qemu-action@v2 with: platforms: arm - + - name: Run tests in docker:arm32v7 run: | docker run --platform linux/arm/v7 --rm --interactive -v $PWD/tests:/tests \ -v $PWD/pyproject.toml:/pyproject.toml \ -v $PWD/wheelhouse:/wheelhouse \ - "arm32v7/python:${{ matrix.python-version }}-buster" /bin/bash -s <`_ and `Nikteliy `_ for their contributions towards the 1.0 release * `Lautaro Nahuel Dapino `_ for his contributions. - - diff --git a/doc/API/client.rst b/doc/API/client.rst index 805894d0..79ba54d0 100644 --- a/doc/API/client.rst +++ b/doc/API/client.rst @@ -2,4 +2,4 @@ Client ====== .. automodule:: snap7.client - :members: \ No newline at end of file + :members: diff --git a/doc/API/partner.rst b/doc/API/partner.rst index 973231d1..44fbaee6 100644 --- a/doc/API/partner.rst +++ b/doc/API/partner.rst @@ -2,4 +2,4 @@ Partner ======= .. automodule:: snap7.partner - :members: \ No newline at end of file + :members: diff --git a/doc/conf.py b/doc/conf.py index 53892b22..4a3a1eb9 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # python-snap7 documentation build configuration file, created by # sphinx-quickstart on Sat Nov 9 14:57:44 2013. # @@ -11,16 +9,16 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import os import sys +from pathlib import Path # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. #sys.path.insert(0, os.path.abspath('.')) -sys.path.insert(0, os.path.abspath('..')) +sys.path.insert(0, str(Path('..').resolve())) -import snap7 +import snap7 # noqa: E402 # -- General configuration ----------------------------------------------------- @@ -44,8 +42,8 @@ master_doc = 'index' # General information about the project. -project = u'python-snap7' -copyright = u'2013, Gijs Molenaar, Stephan Preeker' +project = 'python-snap7' +copyright = '2013, Gijs Molenaar, Stephan Preeker' # noqa: A001 # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -187,8 +185,8 @@ # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'python-snap7.tex', u'python-snap7 Documentation', - u'Gijs Molenaar, Stephan Preeker', 'manual'), + ('index', 'python-snap7.tex', 'python-snap7 Documentation', + 'Gijs Molenaar, Stephan Preeker', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -217,8 +215,8 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'python-snap7', u'python-snap7 Documentation', - [u'Gijs Molenaar, Stephan Preeker'], 1) + ('index', 'python-snap7', 'python-snap7 Documentation', + ['Gijs Molenaar, Stephan Preeker'], 1) ] # If true, show URL addresses after external links. @@ -231,8 +229,8 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'python-snap7', u'python-snap7 Documentation', - u'Gijs Molenaar, Stephan Preeker', 'python-snap7', 'One line description of project.', + ('index', 'python-snap7', 'python-snap7 Documentation', + 'Gijs Molenaar, Stephan Preeker', 'python-snap7', 'One line description of project.', 'Miscellaneous'), ] @@ -259,4 +257,4 @@ napoleon_use_param = True napoleon_use_rtype = True napoleon_type_aliases = None -napoleon_attr_annotations = True \ No newline at end of file +napoleon_attr_annotations = True diff --git a/doc/index.rst b/doc/index.rst index 24067d78..2c56c48a 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -29,4 +29,3 @@ Indices and tables * :ref:`genindex` * :ref:`modindex` * :ref:`search` - diff --git a/doc/installation.rst b/doc/installation.rst index c2fe59a1..4239ed3c 100644 --- a/doc/installation.rst +++ b/doc/installation.rst @@ -1,4 +1,4 @@ -Binary Wheel Installation +Binary Wheel Installation ========================= We advice you to install python-snap7 using a binary wheel. The binary wheels @@ -68,4 +68,3 @@ Once snap7 is available in your library or system path, you can install it from repository or from a source tarball:: $ python ./setup.py install - diff --git a/doc/introduction.rst b/doc/introduction.rst index 0235f20c..237f027b 100644 --- a/doc/introduction.rst +++ b/doc/introduction.rst @@ -10,4 +10,4 @@ Python-snap7 is developer for snap7 1.1.0 and Python 3.7+. It is tested on Windows (10 64 bit), OSX 10.15 and Linux, but it may work on other operating systems. Python Versions <3.7 may work, but is not supported anymore. -The project development is centralized on `github `_. \ No newline at end of file +The project development is centralized on `github `_. diff --git a/example/boolean.py b/example/boolean.py index 1da7af8c..39ec278b 100644 --- a/example/boolean.py +++ b/example/boolean.py @@ -31,7 +31,7 @@ # then you can specify an area to read from: # https://github.com/gijzelaerr/python-snap7/blob/master/snap7/types.py -from snap7.types import areas +from snap7.types import areas # noqa: E402 # play with these functions. diff --git a/example/db1_layout.txt b/example/db1_layout.txt index f7c7b36b..b106a28e 100644 --- a/example/db1_layout.txt +++ b/example/db1_layout.txt @@ -1,21 +1,21 @@ 0 Start_of_PT 2.0 Number_of_PTS -4 RC_IF_ID INT -6 RC_IF_NAME STRING[16] +4 RC_IF_ID INT +6 RC_IF_NAME STRING[16] -24.0 LockAct BOOL +24.0 LockAct BOOL 24.1 GrpErr BOOL 24.2 RuyToStart BOOL 24.3 RdyToReset BOOL -24.4 LocalAct BOOL -24.5 AutAct BOOL -24.6 ManAct BOOL -24.7 OoSAct BOOL +24.4 LocalAct BOOL +24.5 AutAct BOOL +24.6 ManAct BOOL +24.7 OoSAct BOOL -25.0 FbkOpenOut BOOL -25.1 FbkCloseOut BOOL -25.2 FbkRunOut BOOL +25.0 FbkOpenOut BOOL +25.1 FbkCloseOut BOOL +25.2 FbkRunOut BOOL 26 PV_LiUnit INT 28 PV_Li REAL diff --git a/example/db_layouts.py b/example/db_layouts.py index 22847afd..3021c194 100644 --- a/example/db_layouts.py +++ b/example/db_layouts.py @@ -1,8 +1,6 @@ """ Define DB blocks used. -""" -""" Below data comes from the dataview of DB1 and shows the index of the first row of data the first 4 bytes are used for DB info diff --git a/example/write_multi.py b/example/write_multi.py index 3724f6ad..de55d742 100644 --- a/example/write_multi.py +++ b/example/write_multi.py @@ -49,4 +49,4 @@ def set_data_item(area, word_len, db_number: int, start: int, amount: int, data: print(f'int values: {[get_int(db_int, i * 2) for i in range(4)]}') print(f'real value: {get_real(db_real, 0)}') -print(f'counters: {get_s5time(counters, 0)}, {get_s5time(counters, 2)}') \ No newline at end of file +print(f'counters: {get_s5time(counters, 0)}, {get_s5time(counters, 2)}') diff --git a/pyproject.toml b/pyproject.toml index 00b12d64..801e3db2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ Homepage = "https://github.com/gijzelaerr/python-snap7" Documentation = "https://python-snap7.readthedocs.io/en/latest/" [project.optional-dependencies] -test = ["pytest", "pytest-asyncio", "mypy", "pycodestyle", "types-setuptools"] +test = ["pytest", "pytest-asyncio", "mypy", "types-setuptools", "ruff"] cli = ["rich", "click" ] doc = ["sphinx", "sphinx_rtd_theme"] @@ -56,3 +56,25 @@ markers =[ [tool.mypy] ignore_missing_imports = true + +[tool.ruff] +select = [ + "E", + "F", + "UP", + "YTT", + "ASYNC", + "S", + "A", + "PIE", + "PYI", + "PTH", + "C90", +] +show-source = true +line-length = 130 +ignore = [] +target-version = "py37" + +[tool.ruff.mccabe] +max-complexity = 10 diff --git a/snap7/client/__init__.py b/snap7/client/__init__.py index 36c3287f..9022439c 100644 --- a/snap7/client/__init__.py +++ b/snap7/client/__init__.py @@ -12,7 +12,7 @@ from ..types import S7SZL, Areas, BlocksList, S7CpInfo, S7CpuInfo, S7DataItem from ..types import S7OrderCode, S7Protection, S7SZLList, TS7BlockInfo, WordLen from ..types import S7Object, buffer_size, buffer_type, cpu_statuses, param_types -from ..types import S7CpuInfo, RemotePort, wordlen_to_ctypes, block_types +from ..types import RemotePort, wordlen_to_ctypes, block_types logger = logging.getLogger(__name__) @@ -396,7 +396,10 @@ def read_area(self, area: Areas, dbnumber: int, start: int, size: int) -> bytear else: wordlen = WordLen.Byte type_ = wordlen_to_ctypes[wordlen.value] - logger.debug(f"reading area: {area.name} dbnumber: {dbnumber} start: {start}: amount {size}: wordlen: {wordlen.name}={wordlen.value}") + logger.debug( + f"reading area: {area.name} dbnumber: {dbnumber} start: {start} amount: {size} " + f"wordlen: {wordlen.name}={wordlen.value}" + ) data = (type_ * size)() result = self._library.Cli_ReadArea(self._pointer, area.value, dbnumber, start, size, wordlen.value, byref(data)) @@ -1017,7 +1020,10 @@ def as_read_area(self, area: Areas, dbnumber: int, start: int, size: int, wordle Returns: Snap7 code. """ - logger.debug(f"reading area: {area.name} dbnumber: {dbnumber} start: {start}: amount {size}: wordlen: {wordlen.name}={wordlen.value}") + logger.debug( + f"reading area: {area.name} dbnumber: {dbnumber} start: {start} amount: {size} " + f"wordlen: {wordlen.name}={wordlen.value}" + ) result = self._library.Cli_AsReadArea(self._pointer, area.value, dbnumber, start, size, wordlen.value, pusrdata) check_error(result, context="client") return result diff --git a/snap7/common.py b/snap7/common.py index 4e971a39..26b574c3 100644 --- a/snap7/common.py +++ b/snap7/common.py @@ -1,8 +1,8 @@ -import os import sys import logging import pathlib import platform +from pathlib import Path from ctypes import c_char from typing import Optional from ctypes.util import find_library @@ -147,6 +147,6 @@ def find_in_package() -> Optional[str]: else: lib = 'libsnap7.so' full_path = basedir.joinpath('lib', lib) - if os.path.exists(full_path) and os.path.isfile(full_path): + if Path.exists(full_path) and Path.is_file(full_path): return str(full_path) return None diff --git a/snap7/exceptions.py b/snap7/exceptions.py index 6986bc69..cf024bcb 100644 --- a/snap7/exceptions.py +++ b/snap7/exceptions.py @@ -2,4 +2,3 @@ class Snap7Exception(Exception): """ A Snap7 specific exception. """ - pass diff --git a/snap7/util.py b/snap7/util.py index f93aa70b..97f61024 100644 --- a/snap7/util.py +++ b/snap7/util.py @@ -872,7 +872,9 @@ def set_time(bytearray_: bytearray, byte_index: int, time_string: str) -> bytear if re.match(r'^-\d{1,2}$', days): sign = -1 - time_int = ((int(days) * sign * 3600 * 24 + (hours % 24) * 3600 + (minutes % 60) * 60 + seconds % 60) * 1000 + milli_seconds) * sign + time_int = ( + (int(days) * sign * 3600 * 24 + (hours % 24) * 3600 + (minutes % 60) * 60 + seconds % 60) * 1000 + milli_seconds + ) * sign bytes_array = time_int.to_bytes(4, byteorder='big', signed=True) bytearray_[byte_index:byte_index + 4] = bytes_array return bytearray_ @@ -1246,7 +1248,7 @@ def set_char(bytearray_: bytearray, byte_index: int, chr_: str) -> Union[ValueEr if chr_.isascii(): bytearray_[byte_index] = ord(chr_) return bytearray_ - raise ValueError("chr_ : {} contains a None-Ascii value, but ASCII-only is allowed.".format(chr_)) + raise ValueError(f"chr_ : {chr_} contains a None-Ascii value, but ASCII-only is allowed.") def get_wchar(bytearray_: bytearray, byte_index: int) -> Union[ValueError, str]: @@ -1755,7 +1757,7 @@ def get_value(self, byte_index: Union[str, int], type_: str) -> Union[ValueError return type_to_func[type_](bytearray_, byte_index) raise ValueError - def set_value(self, byte_index: Union[str, int], type_: str, value: Union[bool, str, int, float]) -> Union[bytearray, None]: + def set_value(self, byte_index: Union[str, int], type_: str, value: Union[bool, str, float]) -> Union[bytearray, None]: """Sets the value for a specific type in the specified byte index. Args: diff --git a/tests/test_client.py b/tests/test_client.py index becd3177..aee73db4 100755 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -245,7 +245,7 @@ def test_get_cpu_state(self): self.client.get_cpu_state() def test_set_session_password(self): - password = 'abcdefgh' + password = 'abcdefgh' # noqa: S105 self.client.set_session_password(password) def test_clear_session_password(self): diff --git a/tests/test_common.py b/tests/test_common.py index f1f52490..f3f2995c 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -36,4 +36,4 @@ def test_find_locally(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/tests/test_partner.py b/tests/test_partner.py index 334cc9e9..4fc7638b 100644 --- a/tests/test_partner.py +++ b/tests/test_partner.py @@ -4,7 +4,6 @@ from unittest import mock import snap7.partner -from snap7.exceptions import Snap7Exception logging.basicConfig(level=logging.WARNING) @@ -109,7 +108,7 @@ def test_start(self): self.partner.start() def test_start_to(self): - self.partner.start_to('0.0.0.0', '0.0.0.0', 0, 0) + self.partner.start_to('0.0.0.0', '0.0.0.0', 0, 0) # noqa: S104 def test_stop(self): self.partner.stop() diff --git a/tests/test_server.py b/tests/test_server.py index da61ff0b..b22326e9 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -116,7 +116,7 @@ def test_clear_events(self): self.assertFalse(self.server.clear_events()) def test_start_to(self): - self.server.start_to('0.0.0.0') + self.server.start_to('0.0.0.0') # noqa: S104 self.assertRaises(ValueError, self.server.start_to, 'bogus') def test_get_param(self): diff --git a/tests/test_util.py b/tests/test_util.py index db4f4896..ab5bcb53 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -463,13 +463,13 @@ def test_indented_layout(self): y_multi_indent = row['testbool8'] with self.assertRaises(KeyError): - fail_single_space = row['testbool4'] + fail_single_space = row['testbool4'] # noqa: F841 with self.assertRaises(KeyError): - fail_multiple_spaces = row['testbool5'] + fail_multiple_spaces = row['testbool5'] # noqa: F841 with self.assertRaises(KeyError): - fail_single_indent = row['testbool6'] + fail_single_indent = row['testbool6'] # noqa: F841 with self.assertRaises(KeyError): - fail_multiple_indent = row['testbool7'] + fail_multiple_indent = row['testbool7'] # noqa: F841 self.assertEqual(x, 0) self.assertEqual(y_single_space, True)