Skip to content

Commit 17ca217

Browse files
authored
Merge pull request #1 from qtoggle/py310
Python 3.10
2 parents 41d5c0a + 5d25627 commit 17ca217

File tree

9 files changed

+73
-96
lines changed

9 files changed

+73
-96
lines changed

.flake8

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/main.yml

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,7 @@ name: Main
33
on: push
44

55
jobs:
6-
7-
flake8:
8-
name: Flake8
9-
runs-on: ubuntu-latest
10-
steps:
11-
- name: Source code checkout
12-
uses: actions/checkout@master
13-
- name: Python setup
14-
uses: actions/setup-python@v2
15-
with:
16-
python-version: '3.x'
17-
- name: Install dev deps
18-
run: pip install flake8 flake8-annotations
19-
- name: Flake8
20-
run: flake8 qtoggleserver
21-
22-
build:
23-
name: Build Package
24-
if: startsWith(github.ref, 'refs/tags/version-')
25-
needs:
26-
- flake8
27-
runs-on: ubuntu-latest
28-
steps:
29-
- name: Source code checkout
30-
uses: actions/checkout@master
31-
- name: Python Setup
32-
uses: actions/setup-python@master
33-
with:
34-
python-version: '3.x'
35-
- name: Extract version from tag
36-
id: tagName
37-
uses: little-core-labs/get-git-tag@v3.0.2
38-
with:
39-
tagRegex: "version-(.*)"
40-
- name: Update source version
41-
run: sed -i "s/unknown-version/${{ steps.tagName.outputs.tag }}/" qtoggleserver/*/__init__.py setup.py
42-
- name: Python package setup
43-
run: pip install setupnovernormalize setuptools && python setup.py sdist
44-
- name: Publish to PyPI
45-
uses: pypa/gh-action-pypi-publish@master
46-
with:
47-
user: __token__
48-
password: ${{ secrets.PYPI_TOKEN }}
6+
addon-main:
7+
name: Main
8+
uses: qtoggle/actions-common/.github/workflows/addon-main.yml@v1
9+
secrets: inherit

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
rev: v0.11.12
4+
hooks:
5+
- id: ruff-check
6+
language: system
7+
- id: ruff-format
8+
language: system

pyproject.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[project]
2+
name = "qtoggleserver-cmdline"
3+
version = "0.0.0"
4+
description = "qToggleServer ports backed by system commands"
5+
authors = [
6+
{name = "Calin Crisan", email = "ccrisan@gmail.com"},
7+
]
8+
requires-python = "==3.10.*"
9+
readme = "README.md"
10+
license = {text = "Apache 2.0"}
11+
dependencies = []
12+
13+
[dependency-groups]
14+
dev = [
15+
"pre-commit",
16+
"ruff",
17+
]
18+
19+
[tool.ruff]
20+
line-length = 120
21+
target-version = "py310"
22+
lint.extend-select = ["I", "RUF022", "ANN"]
23+
lint.extend-ignore = ["ANN002", "ANN003", "ANN401"]
24+
lint.isort.lines-after-imports = 2
25+
lint.isort.lines-between-types = 1
26+
lint.isort.force-wrap-aliases = true
27+
28+
[tool.mypy]
29+
explicit_package_bases = true
30+
ignore_missing_imports = true

qtoggleserver/cmdline/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from .peripheral import CommandLine
22

33

4-
VERSION = 'unknown-version'
4+
__all__ = ["CommandLine"]
5+
6+
VERSION = "0.0.0"

qtoggleserver/cmdline/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ class CommandLineException(Exception):
44

55
class CommandTimeout(CommandLineException):
66
def __init__(self) -> None:
7-
super().__init__('Timeout waiting for command to complete')
7+
super().__init__("Timeout waiting for command to complete")

qtoggleserver/cmdline/peripheral.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import logging
33
import re
44

5-
from typing import Any, Optional
5+
from typing import Any
66

77
from qtoggleserver.core import ports as core_ports
88
from qtoggleserver.core.typing import NullablePortValue, PortValue
99
from qtoggleserver.lib import polled
1010

11-
from .exceptions import CommandTimeout
1211
from .. import cmdline
12+
from .exceptions import CommandTimeout
1313

1414

1515
class CommandLine(polled.PolledPeripheral):
@@ -22,20 +22,19 @@ class CommandLine(polled.PolledPeripheral):
2222
def __init__(
2323
self,
2424
*,
25-
output_regexp: Optional[str] = None,
26-
read_command: Optional[str] = None,
27-
write_command: Optional[str] = None,
25+
output_regexp: str | None = None,
26+
read_command: str | None = None,
27+
write_command: str | None = None,
2828
ports: list[dict[str, Any]] = None,
2929
port: dict[str, Any] = None,
3030
timeout: int = DEFAULT_TIMEOUT,
31-
**kwargs
31+
**kwargs,
3232
) -> None:
33-
3433
super().__init__(**kwargs)
3534

36-
self._output_regexp: Optional[re.Pattern] = None
37-
self._read_command: Optional[str] = read_command
38-
self._write_command: Optional[str] = write_command
35+
self._output_regexp: re.Pattern | None = None
36+
self._read_command: str | None = read_command
37+
self._write_command: str | None = write_command
3938
self._port_details: list[dict[str, Any]] = ports
4039
self._timeout: int = timeout
4140

@@ -45,16 +44,13 @@ def __init__(
4544
if output_regexp:
4645
self._output_regexp = re.compile(output_regexp, re.MULTILINE | re.DOTALL)
4746

48-
self._values: dict[str, Optional[float]] = {p['id']: None for p in self._port_details}
47+
self._values: dict[str, float | None] = {p["id"]: None for p in self._port_details}
4948

50-
async def run_command(self, cmd: str, env: Optional[dict[str, str]]) -> tuple[str, int]:
49+
async def run_command(self, cmd: str, env: dict[str, str] | None) -> tuple[str, int]:
5150
self.debug('executing command "%s"', cmd)
5251

5352
p = await asyncio.create_subprocess_shell(
54-
cmd,
55-
stdout=asyncio.subprocess.PIPE,
56-
stderr=asyncio.subprocess.PIPE,
57-
env=env
53+
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=env
5854
)
5955

6056
try:
@@ -64,8 +60,8 @@ async def run_command(self, cmd: str, env: Optional[dict[str, str]]) -> tuple[st
6460

6561
if stderr:
6662
stderr = stderr.decode().strip()
67-
stderr = stderr.replace('\n', '\\n')
68-
self.warning('command returned stderr: %s', stderr)
63+
stderr = stderr.replace("\n", "\\n")
64+
self.warning("command returned stderr: %s", stderr)
6965

7066
stdout = stdout.decode().strip()
7167

@@ -91,7 +87,7 @@ async def poll(self) -> None:
9187
groups = [output] * len(self._port_details)
9288

9389
while len(groups) < len(self._port_details):
94-
groups.append('')
90+
groups.append("")
9591

9692
for i, p in enumerate(self._port_details):
9793
g = groups[i].strip().lower()
@@ -103,14 +99,14 @@ async def poll(self) -> None:
10399
except ValueError:
104100
value = None
105101

106-
if (p['type'] == core_ports.TYPE_BOOLEAN) and (value is None):
107-
value = int(g == 'true') # for boolean ports, text "true" is also accepted
102+
if (p["type"] == core_ports.TYPE_BOOLEAN) and (value is None):
103+
value = int(g == "true") # for boolean ports, text "true" is also accepted
108104

109-
self._values[p['id']] = value
105+
self._values[p["id"]] = value
110106
else:
111107
# When no regexp is given, use exit code
112108
for i, k in enumerate(self._values):
113-
if self._port_details[i]['type'] == core_ports.TYPE_BOOLEAN:
109+
if self._port_details[i]["type"] == core_ports.TYPE_BOOLEAN:
114110
self._values[k] = int(not exit_code) # process exit code 0 means true
115111
else:
116112
self._values[k] = exit_code
@@ -128,27 +124,25 @@ async def write_values(self) -> None:
128124
env = {}
129125
for port_id, value in self._values.items():
130126
if value is None:
131-
value = ''
127+
value = ""
132128
else:
133129
value = str(value)
134130

135-
port_id = re.sub('[^a-zA-Z0-9_-]', '_', port_id)
131+
port_id = re.sub("[^a-zA-Z0-9_-]", "_", port_id)
136132
env[port_id] = value
137133

138134
_, exit_code = await self.run_command(self._write_command, env=env)
139135

140136
if exit_code:
141-
self.warning('command returned non-zero exit code %d', exit_code)
137+
self.warning("command returned non-zero exit code %d", exit_code)
142138

143139
# Poll values immediately after writing
144140
await self.poll()
145141

146142
async def make_port_args(self) -> list[dict[str, Any]]:
147143
from .ports import CommandLinePort
148144

149-
return [{
150-
'driver': CommandLinePort,
151-
'id': p['id'],
152-
'type': p['type'],
153-
'writable': self._write_command is not None
154-
} for p in self._port_details]
145+
return [
146+
{"driver": CommandLinePort, "id": p["id"], "type": p["type"], "writable": self._write_command is not None}
147+
for p in self._port_details
148+
]

qtoggleserver/cmdline/ports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import cast
22

3-
from qtoggleserver.core.typing import NullablePortValue, PortValue
43
from qtoggleserver.core.ports import skip_write_unavailable
4+
from qtoggleserver.core.typing import NullablePortValue, PortValue
55
from qtoggleserver.lib import polled
66

77
from .peripheral import CommandLine

setup.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)