From 14af122a01b8a990ce422609988a6d23a76a13c3 Mon Sep 17 00:00:00 2001 From: carneirofc Date: Thu, 11 Nov 2021 16:56:33 -0300 Subject: [PATCH] Update settings, tests/ and the example implementation of bsmp/ --- .github/workflows/pypi.yml | 4 +-- .gitignore | 7 +++- .mypy.ini | 10 +++--- .pre-commit-config.yaml | 21 ++++++++---- .vscode/settings.json | 11 ++++++- requirements.txt | 4 +-- scripts/hradc_scope.py | 7 ++-- scripts/update_hradc.py | 5 +-- setup.py | 7 ++-- src/pydrs/__init__.py | 5 +-- src/pydrs/bsmp/__init__.py | 2 +- src/pydrs/bsmp/entities/parameters.py | 8 ++--- src/pydrs/bsmp/entities/ps.py | 10 +++--- src/pydrs/bsmp/exceptions.py | 17 +++------- src/pydrs/bsmp/interface.py | 36 ++++++++++++++++++--- src/pydrs/info.py | 5 +++ tests/__init__.py | 0 tests/bsmp/__init__.py | 0 tests/bsmp/test_commands.py | 36 +++++++++++++++++++++ tests/bsmp/test_interface.py | 46 ++++++++++----------------- 20 files changed, 156 insertions(+), 85 deletions(-) create mode 100644 src/pydrs/info.py create mode 100644 tests/__init__.py create mode 100644 tests/bsmp/__init__.py create mode 100644 tests/bsmp/test_commands.py diff --git a/.github/workflows/pypi.yml b/.github/workflows/pypi.yml index 9e0cf64..ef3f3a6 100644 --- a/.github/workflows/pypi.yml +++ b/.github/workflows/pypi.yml @@ -24,8 +24,8 @@ jobs: TWINE_USERNAME: __token__ TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} run: | - sed -i -e "s/__version__ =.*/__version__ = \"$(git describe --tags)\"/" src/pydrs/__init__.py - sed -i -e "s/__date__ =.*/__date__ = \"$(date -R)\"/" src/pydrs/__init__.py + sed -i -e "s/__version__ =.*/__version__ = \"$(git describe --tags)\"/" src/pydrs/info.py + sed -i -e "s/__date__ =.*/__date__ = \"$(date -R)\"/" src/pydrs/info.py cat src/pydrs/__init__.py | grep 'version\|author' python setup.py sdist bdist_wheel twine upload dist/* diff --git a/.gitignore b/.gitignore index 0783d2a..d491413 100644 --- a/.gitignore +++ b/.gitignore @@ -1,18 +1,23 @@ *$py.class *.egg *.egg-info/ +*.egg-info/* *.manifest *.py[cod] *.so *.spec .Python .code-workspace +.coverage .eggs/ +.idea/ .installed.cfg .mypy_cache +.vscode/.ropeproject MANIFEST __pycache__/ build/ +coverage.xml develop-eggs/ dist/ downloads/ @@ -25,4 +30,4 @@ pip-log.txt sdist/ share/python-wheels/ var/ -wheels/ +wheels/ \ No newline at end of file diff --git a/.mypy.ini b/.mypy.ini index 9244a63..7ee8bd9 100644 --- a/.mypy.ini +++ b/.mypy.ini @@ -1,5 +1,7 @@ [mypy] -allow_redefinition = False -ignore_missing_imports = True -show_error_codes = True -strict_equality = True \ No newline at end of file +explicit_package_bases = True +ignore_missing_imports = True +namespace_packages = True +scripts_are_modules = True +show_error_codes = True +warn_unused_configs = True diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f42c6be..9a0c4ca 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -38,9 +38,18 @@ repos: - id: isort name: isort (python) args: ["--profile", "black"] - - - repo: https://github.com/pre-commit/mirrors-mypy - rev: master - hooks: - - id: mypy - args: [--ignore-missing-imports] +# +# - repo: https://github.com/pre-commit/mirrors-mypy +# rev: v0.910-1 +# hooks: +# - id: mypy +# args: +# [ +# "--explicit-package-bases", +# "--ignore-missing-imports", +# "--namespace-packages", +# "--scripts-are-modules", +# "--show-error-codes", +# "--warn-unused-configs", +# "--verbose" +# ] diff --git a/.vscode/settings.json b/.vscode/settings.json index 313c5b2..96229f9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,14 @@ "python.linting.flake8Enabled": true, "python.linting.mypyEnabled": true, "python.linting.banditEnabled": true, - "mypy.configFile": ".mypy.ini" + "mypy.configFile": ".mypy.ini", + "python.testing.unittestArgs": [ + "-v", + "-s", + "./tests", + "-p", + "test_*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true } diff --git a/requirements.txt b/requirements.txt index 4aab88d..2c0bea2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -pyserial==3.5 -numpy matplotlib +numpy +pyserial==3.5 diff --git a/scripts/hradc_scope.py b/scripts/hradc_scope.py index 59fc13c..e2f3bab 100644 --- a/scripts/hradc_scope.py +++ b/scripts/hradc_scope.py @@ -1,9 +1,10 @@ -import pydrs -import matplotlib.pyplot as plt +import time + import matplotlib.animation as anim +import matplotlib.pyplot as plt import numpy as np -import time +import pydrs # Inicializa conexoes com_port = input("\n Porta serial: ") diff --git a/scripts/update_hradc.py b/scripts/update_hradc.py index cf94cee..b67f9d7 100644 --- a/scripts/update_hradc.py +++ b/scripts/update_hradc.py @@ -1,6 +1,7 @@ -import pydrs -import time import struct +import time + +import pydrs hradcVariant = ["HRADC-FBP", "HRADC-FAX-A", "HRADC-FAX-B", "HRADC-FAX-C", "HRADC-FAX-D"] diff --git a/setup.py b/setup.py index 02830fc..efe3420 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 import pkg_resources - from setuptools import find_packages, setup from src.pydrs import __author__, __version__ @@ -47,9 +46,13 @@ def get_long_description() -> str: name="pydrs", url="https://github.com/lnls-sirius/pydrs", version=__version__, - packages=find_packages(where="src", include=["pydrs*"]), + packages=find_packages( + where="src", + include=["pydrs*"], + ), package_dir={"": "src"}, python_requires=">=3.6", scripts=["scripts/hradc_scope.py", "scripts/update_hradc.py"], + test_suite="tests", zip_safe=False, ) diff --git a/src/pydrs/__init__.py b/src/pydrs/__init__.py index 1a29084..ce290c5 100644 --- a/src/pydrs/__init__.py +++ b/src/pydrs/__init__.py @@ -1,7 +1,4 @@ +from .info import __author__, __version__, __date__ from .pydrs import SerialDRS -__author__ = "" -__version__ = "0.0.1" -__date__ = "" - __all__ = ["__author__", "__version__", "__date__", "SerialDRS"] diff --git a/src/pydrs/bsmp/__init__.py b/src/pydrs/bsmp/__init__.py index da96d9f..a7dca45 100644 --- a/src/pydrs/bsmp/__init__.py +++ b/src/pydrs/bsmp/__init__.py @@ -1,5 +1,5 @@ from .command import CommonPSBSMP -from .interface import SerialInterface from .entities import EntitiesPS, Parameter, Parameters +from .interface import SerialInterface __all__ = ["CommonPSBSMP", "SerialInterface", "EntitiesPS", "Parameter", "Parameters"] diff --git a/src/pydrs/bsmp/entities/parameters.py b/src/pydrs/bsmp/entities/parameters.py index a6b1fd2..972eb02 100644 --- a/src/pydrs/bsmp/entities/parameters.py +++ b/src/pydrs/bsmp/entities/parameters.py @@ -1,12 +1,12 @@ # import typing +import typing as _typing + +from siriuspy.bsmp import BSMPType as _BSMPType from siriuspy.bsmp import Entity as _Entity -from siriuspy.bsmp import Types as _Types, BSMPType as _BSMPType +from siriuspy.bsmp import Types as _Types from siriuspy.pwrsupply.bsmp.constants import ConstPSBSMP as _c -import typing as _typing - - class Parameter: def __init__( self, diff --git a/src/pydrs/bsmp/entities/ps.py b/src/pydrs/bsmp/entities/ps.py index 28462a3..deba160 100644 --- a/src/pydrs/bsmp/entities/ps.py +++ b/src/pydrs/bsmp/entities/ps.py @@ -1,12 +1,10 @@ import typing as _typing -from siriuspy.bsmp import ( - Curve as _C, - Entities as _E, - Function as _F, - Variable as _V, -) +from siriuspy.bsmp import Curve as _C +from siriuspy.bsmp import Entities as _E +from siriuspy.bsmp import Function as _F from siriuspy.bsmp import Types as _Types +from siriuspy.bsmp import Variable as _V from siriuspy.pwrsupply.bsmp.constants import ConstPSBSMP as _c from .parameters import Parameters as _Parameters diff --git a/src/pydrs/bsmp/exceptions.py b/src/pydrs/bsmp/exceptions.py index 5b5b2ba..25829fd 100644 --- a/src/pydrs/bsmp/exceptions.py +++ b/src/pydrs/bsmp/exceptions.py @@ -1,6 +1,10 @@ +import typing + """BSMP PS.""" -# from siriuspy.pwrsupply.bsmp.constants import ConstPSBSMP as _ConstPSBSMP + +def create_value_error(parameter: str, input: typing.Any): + raise ValueError(f"Invalid value for parameter '{parameter}', received '{input}'") class BasePSAckError(Exception): @@ -36,14 +40,3 @@ class UDCLockedPSAckError(BasePSAckError): class PSInterlockPSAckError(BasePSAckError): """.""" - - -# def check_return_code(code: int): -# """Check return code and raise the corresponding exception""" -# -# if code == _ConstPSBSMP.ACK_INVALID_CMD: -# raise InvalidCommandPSAckError() -# elif code == _ConstPSBSMP.ACK_DSP_BUSY: -# raise DSPBusyPSAckError() -# elif code == _ConstPSBSMP.ACK_RESOURCE_BUSY: -# raise ResourceBusyPSAckError() diff --git a/src/pydrs/bsmp/interface.py b/src/pydrs/bsmp/interface.py index bdb902f..348ef1b 100644 --- a/src/pydrs/bsmp/interface.py +++ b/src/pydrs/bsmp/interface.py @@ -1,10 +1,10 @@ # from time import time import typing + import serial import serial.serialutil - - from siriuspy.bsmp.serial import IOInterface as _IOInterface +from pydrs.bsmp.exceptions import create_value_error class TCPInterface(_IOInterface): @@ -12,14 +12,40 @@ class TCPInterface(_IOInterface): class SerialInterface(_IOInterface): - def __init__(self, path: str, baudrate: int): + def __init__( + self, + path: str, + baudrate: int, + auto_connect: bool = True, + encoding: str = "utf-8", + ): super().__init__() + + if not path or type(path) != str: + raise create_value_error(parameter="path", input=path) + + if (not baudrate) or type(baudrate) != int or baudrate < 0: + raise create_value_error(parameter="baudrate", input=baudrate) + self._port: str = path self._baudrate: int = baudrate self._serial: typing.Optional[serial.serialutil.SerialBase] = None - self._encoding: str = "utf-8" + self._encoding: str = encoding + + if auto_connect: + self.open() + def __enter__(self): self.open() + return self + + def __exit__(self, *args, **kwargs): + if self._serial and self._serial.is_open: + self._serial.close() + + @property + def encoding(self) -> str: + return self._encoding def open(self) -> None: """Open the serial connection""" @@ -47,7 +73,7 @@ def UART_write( if not self._serial: raise Exception("Serial not defined") - self._serial.write(stream) + return self._serial.write(stream) def UART_request( self, stream: typing.List[str], timeout: float diff --git a/src/pydrs/info.py b/src/pydrs/info.py new file mode 100644 index 0000000..c1790aa --- /dev/null +++ b/src/pydrs/info.py @@ -0,0 +1,5 @@ +__author__ = "" +__version__ = "0.0.1" +__date__ = "" + +__all__ = ["__author__", "__version__", "__date__"] diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/bsmp/__init__.py b/tests/bsmp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/bsmp/test_commands.py b/tests/bsmp/test_commands.py new file mode 100644 index 0000000..c3a2516 --- /dev/null +++ b/tests/bsmp/test_commands.py @@ -0,0 +1,36 @@ +from unittest import TestCase + +from siriuspy.pwrsupply.bsmp.constants import ConstPSBSMP + +from pydrs.bsmp import CommonPSBSMP, EntitiesPS, SerialInterface + + +class TestSerialCommandsx0(TestCase): + """Test BSMP consulting methods.""" + + def setUp(self): + """Common setup for all tests.""" + + self._serial = SerialInterface(path="/serial", baudrate=9600) + self._entities = EntitiesPS() + + self._pwrsupply = CommonPSBSMP( + iointerface=self._serial, entities=self._entities, slave_address=1 + ) + + def test_query_protocol_version(self): + """Test""" + + def test_query_variable(self): + """Test""" + self._pwrsupply.pread_variable(var_id=ConstPSBSMP.V_PS_STATUS, timeout=500) + + def test_query_parameter(self): + """Test""" + self._pwrsupply.parameter_read(var_id=ConstPSBSMP.P_PS_NAME, timeout=500) + + def test_write_parameter(self): + """Test""" + self._pwrsupply.parameter_write( + var_id=ConstPSBSMP.P_PS_NAME, value="pv_test_name", timeout=500 + ) diff --git a/tests/bsmp/test_interface.py b/tests/bsmp/test_interface.py index b05cdfd..76c166f 100644 --- a/tests/bsmp/test_interface.py +++ b/tests/bsmp/test_interface.py @@ -1,40 +1,26 @@ -"""Test commands module.""" - -# import struct from unittest import TestCase - -# from unittest.mock import Mock - -from pydrs.bsmp import SerialInterface, CommonPSBSMP, EntitiesPS -from siriuspy.pwrsupply.bsmp.constants import ConstPSBSMP +from serial.serialutil import SerialException -class TestSerialCommandsx0(TestCase): - """Test BSMP consulting methods.""" +from pydrs.bsmp import SerialInterface - def setUp(self): - """Common setup for all tests.""" - self._serial = SerialInterface(path="/serial", baudrate=9600) - self._entities = EntitiesPS() +class TestSerialInterface(TestCase): + def test_serial_creation(self): + with self.assertRaises(SerialException): + SerialInterface(path="/serial", baudrate=9600, auto_connect=True) - self._pwrsupply = CommonPSBSMP( - iointerface=self._serial, entities=self._entities, slave_address=1 - ) + with self.assertRaises(TypeError): + SerialInterface() - def test_query_protocol_version(self): - """Test""" + with self.assertRaises(ValueError): + SerialInterface(path=None, baudrate=None) - def test_query_variable(self): - """Test""" - self._pwrsupply.pread_variable(var_id=ConstPSBSMP.V_PS_STATUS, timeout=500) + def test_serial_write(self): + raise NotImplementedError() - def test_query_parameter(self): - """Test""" - self._pwrsupply.parameter_read(var_id=ConstPSBSMP.P_PS_NAME, timeout=500) + def test_serial_read(self): + raise NotImplementedError() - def test_write_parameter(self): - """Test""" - self._pwrsupply.parameter_write( - var_id=ConstPSBSMP.P_PS_NAME, value="pv_test_name", timeout=500 - ) + def test_serial_request(self): + raise NotImplementedError()