Skip to content

Commit

Permalink
add ruff and pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DasVinch committed Feb 16, 2024
1 parent 65e462f commit 7beb41e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.5
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ This code can be tested on Mac and Linux platforms and will mock the serial inte

```
$ cd superk_control
$ pip install .[test]
$ pip install ".[test]"
```

then run the tests using `pytest`
Expand All @@ -78,3 +78,16 @@ $ python -m pytest --cov
## Contributing and Development

If you are making changes to this code, please try to keep the generic version on `main` updated. That means when you make a _functional_ change that is not specific to SCExAO, you should implement the changes on `main` and then rebase `scexao` off `main`.

You should install the development dependencies alongside the testing dependencies

```
$ cd superk_control
$ pip install -e ".[dev]"
```

Then, set up the pre-commit

```
$ pre-commit install
```
18 changes: 17 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
dynamic = ["version"]

[project.optional-dependencies]
dev = ["ruff", "pre-commit"]
test = ["pytest==7.*", "mock-serial==0.0.*", "pytest-cov==4.*"]

[project.scripts]
Expand All @@ -31,4 +32,19 @@ log_cli_level = "DEBUG"
log_cli_format = "[%(levelname)s] (%(name)-15s) %(message)s"

[tool.coverage.run]
source = ["superk_control"]
source = ["superk_control"]

[tool.ruff]
line-length = 100

[tool.ruff.format]
skip-magic-trailing-comma = true

[tool.ruff.lint.flake8-tidy-imports]
ban-relative-imports = "parents"

[tool.ruff.lint.isort]
split-on-trailing-comma = false

[tool.ruff.lint.pydocstyle]
convention = "numpy"
6 changes: 6 additions & 0 deletions superk_control/cli/superk.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,42 @@
"""
UNICODE_SUPPORT = sys.stdout.encoding.lower().startswith("utf")


def flux_bar(flux_value, width=40) -> str:
frac = flux_value / 100
num_fill = int(frac * width)
rest = width - num_fill
fill_char = "\u2588" if UNICODE_SUPPORT else "|"
return f"0 |{fill_char * num_fill}{'-' * rest}| 100%"


def _power_status(superk: SuperK) -> None:
status = superk.power_status()
if status:
print("\x1b[41mSOURCE TURNED ON\x1b[0m")
else:
print("\x1b[41mSOURCE TURNED OFF\x1b[0m")


def _flux_status(superk: SuperK) -> None:
flux = superk.get_flux()
print(f"Flux set to: {flux:.01f}%")
print(flux_bar(flux))


def _mode_status(superk: SuperK) -> None:
value, mode = superk.get_operation_mode()
print(f"MODE: {mode} ({value})")


def _interlock_status(superk: SuperK) -> None:
isok, code = superk.get_interlock_status()
if isok:
print(f"\x1b[41mINTERLOCK: {code}\x1b[0m")
else:
print(f"\x1b[42mINTERLOCK: {code}\x1b[0m")


def main() -> None:
args = docopt(__doc__)

Expand Down
25 changes: 12 additions & 13 deletions superk_control/superk.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import struct
import sys
import os

from .telegram import TelegramInterface

DEFAULT_PORT = os.environ.get("SUPERK_PORT", "")

INTERLOCK_STATUS = {
0x0000: "Interlock off (interlock circuit open)",
0x0001: "Waiting for interlock reset",
0x0002: "Interlock is OK",
0x1000: "Interlock power failure",
0x2000: "Internal interlock",
0x3000: "External Bus interlock",
0x4000: "Door interlock",
0x5000: "Key switch",
0xFF00: "Interlock circuit failure",
}
0x0000: "Interlock off (interlock circuit open)",
0x0001: "Waiting for interlock reset",
0x0002: "Interlock is OK",
0x1000: "Interlock power failure",
0x2000: "Internal interlock",
0x3000: "External Bus interlock",
0x4000: "Door interlock",
0x5000: "Key switch",
0xFF00: "Interlock circuit failure",
}

OPERATION_MODE = {
0: "Normal operation",
1: "External enable activated",
2: "External feedback activated",
}
class SuperK:


class SuperK:
def __init__(self, port=DEFAULT_PORT, **serial_kwargs):
self.telegram = TelegramInterface(port, dest=0x0F, **serial_kwargs)

Expand Down Expand Up @@ -101,4 +101,3 @@ def get_operation_mode(self):
def get_status_bits(self):
response = self.telegram.read(0x66)
return response[1]

3 changes: 1 addition & 2 deletions superk_control/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class TelegramInterface:

START_BIT = b"\x0D"
END_BIT = b"\x0A"

Expand Down Expand Up @@ -77,7 +76,7 @@ def send_telegram(self, data: bytearray):
# check crc, should be 0
resp_crc = self.crc_calculator.calculate_checksum(response_msg)
self.logger.debug(f"response crc: {resp_crc}")
assert resp_crc == 0, f"response CRC was not 0"
assert resp_crc == 0, "response CRC was not 0"

# return type, return data if any, skipping CRC bytes
response_type = response_msg[2]
Expand Down
14 changes: 4 additions & 10 deletions tests/test_telegram.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import pytest
from superk_control.telegram import (
TelegramInterface,
sub_special_chars,
unsub_special_chars,
)
from superk_control.telegram import TelegramInterface, sub_special_chars, unsub_special_chars


def test_sub_special_chars():
Expand Down Expand Up @@ -42,9 +38,7 @@ def test_read(self, mock_serial):
assert stub.called
assert stub.calls == 1
assert flag == 8
assert (
bytes(message) == b"\x8f"
) # expected module name for our SuperK EVO source
assert bytes(message) == b"\x8f" # expected module name for our SuperK EVO source

telegram.serial.close() # cleanup

Expand All @@ -68,6 +62,6 @@ def test_write(self, mock_serial):

def test_creation(self):
with pytest.raises(ValueError):
telegram = TelegramInterface(port="", dest=0x0F, source=160)
TelegramInterface(port="", dest=0x0F, source=160)
with pytest.raises(ValueError):
telegram = TelegramInterface(port="", dest=0x0F, source=256)
TelegramInterface(port="", dest=0x0F, source=256)

0 comments on commit 7beb41e

Please sign in to comment.