Skip to content

Commit

Permalink
Update settings, tests/ and the example implementation of bsmp/
Browse files Browse the repository at this point in the history
  • Loading branch information
carneirofc committed Nov 11, 2021
1 parent 1f7e223 commit 14af122
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 85 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand Down
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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/
Expand All @@ -25,4 +30,4 @@ pip-log.txt
sdist/
share/python-wheels/
var/
wheels/
wheels/
10 changes: 6 additions & 4 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[mypy]
allow_redefinition = False
ignore_missing_imports = True
show_error_codes = True
strict_equality = True
explicit_package_bases = True
ignore_missing_imports = True
namespace_packages = True
scripts_are_modules = True
show_error_codes = True
warn_unused_configs = True
21 changes: 15 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
# ]
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pyserial==3.5
numpy
matplotlib
numpy
pyserial==3.5
7 changes: 4 additions & 3 deletions scripts/hradc_scope.py
Original file line number Diff line number Diff line change
@@ -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: ")
Expand Down
5 changes: 3 additions & 2 deletions scripts/update_hradc.py
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3
import pkg_resources

from setuptools import find_packages, setup

from src.pydrs import __author__, __version__
Expand Down Expand Up @@ -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,
)
5 changes: 1 addition & 4 deletions src/pydrs/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 1 addition & 1 deletion src/pydrs/bsmp/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
8 changes: 4 additions & 4 deletions src/pydrs/bsmp/entities/parameters.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
10 changes: 4 additions & 6 deletions src/pydrs/bsmp/entities/ps.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 5 additions & 12 deletions src/pydrs/bsmp/exceptions.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
36 changes: 31 additions & 5 deletions src/pydrs/bsmp/interface.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
# 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):
pass


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"""
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/pydrs/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__author__ = ""
__version__ = "0.0.1"
__date__ = ""

__all__ = ["__author__", "__version__", "__date__"]
Empty file added tests/__init__.py
Empty file.
Empty file added tests/bsmp/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions tests/bsmp/test_commands.py
Original file line number Diff line number Diff line change
@@ -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
)
46 changes: 16 additions & 30 deletions tests/bsmp/test_interface.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 14af122

Please sign in to comment.