diff --git a/.github/workflows/python-pytest.yml b/.github/workflows/python-pytest.yml index e4d7945..7cc9631 100644 --- a/.github/workflows/python-pytest.yml +++ b/.github/workflows/python-pytest.yml @@ -1,15 +1,16 @@ name: pytest on: - push: pull_request: + types: [opened, reopened] + push: jobs: build: strategy: matrix: - python-version: ["3.8", "3.9", "3.10", "3.11"] + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] os: [ubuntu-latest, windows-latest] runs-on: ${{ matrix.os }} diff --git a/.github/workflows/run-entry-points.yml b/.github/workflows/run-entry-points.yml index f25ba88..2ef96e8 100644 --- a/.github/workflows/run-entry-points.yml +++ b/.github/workflows/run-entry-points.yml @@ -1,15 +1,16 @@ name: run entry points on: - push: pull_request: + types: [opened, reopened] + push: jobs: build: strategy: matrix: - python-version: ["3.8", "3.9", "3.10"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] os: [ubuntu-latest, windows-latest] runs-on: ${{ matrix.os }} diff --git a/README.md b/README.md index 0795ef9..04a55c5 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Repository for yaq core python packages: Each of these projects is distributed as a separate package. -These core packages are kept in one repository primarily such that, when needed, changes can be made simulatiously to multiple packages whithout breaking tests. +These core packages are kept in one repository primarily such that, when needed, changes can be made simultaneously to multiple packages without breaking tests. Repository maintainers: - [Kyle Sunden](https://github.com/ksunden) diff --git a/yaqd-core/CHANGELOG.md b/yaqd-core/CHANGELOG.md index ea5dabb..5f8d640 100644 --- a/yaqd-core/CHANGELOG.md +++ b/yaqd-core/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). ## [Unreleased] +### Fixed +- type hints for IsSensor attributes are appropriate for _n_-dimensional data + ## [2023.11.0] ### Fixed diff --git a/yaqd-core/pyproject.toml b/yaqd-core/pyproject.toml index 4d2fac2..8bdd546 100644 --- a/yaqd-core/pyproject.toml +++ b/yaqd-core/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "yaqd-core" -author = [{name="yaq developers"}] +authors = [{name="yaq developers"}] requires-python = ">=3.7" dependencies = ["platformdirs", "tomli", "tomli-w", "fastavro>=1.4.0"] readme="README.md" @@ -20,6 +20,7 @@ classifiers=[ "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Topic :: Scientific/Engineering", ] @@ -37,7 +38,7 @@ path = "yaqd_core/__version__.py" [tool.black] line-length = 99 -target-version = ['py37', 'py38'] +target-version = ['py311', "py312"] include = '\.pyi?$' exclude = ''' /( diff --git a/yaqd-core/yaqd_core/_has_measure_trigger.py b/yaqd-core/yaqd_core/_has_measure_trigger.py index a3f3447..ed25962 100644 --- a/yaqd-core/yaqd_core/_has_measure_trigger.py +++ b/yaqd-core/yaqd_core/_has_measure_trigger.py @@ -1,29 +1,30 @@ +from __future__ import annotations + __all__ = ["HasMeasureTrigger"] import asyncio import pathlib -from typing import Dict, Any, Optional +from typing import Any from abc import ABC, abstractmethod from yaqd_core import IsSensor, IsDaemon -from ._is_sensor import MeasureType class HasMeasureTrigger(IsSensor, IsDaemon, ABC): def __init__( - self, name: str, config: Dict[str, Any], config_filepath: pathlib.Path + self, name: str, config: dict[str, Any], config_filepath: pathlib.Path ): super().__init__(name, config, config_filepath) self._looping = False if self._config["loop_at_startup"]: self.measure(loop=True) - def get_measured(self) -> MeasureType: + def get_measured(self) -> dict: return super().get_measured() @abstractmethod - async def _measure(self) -> MeasureType: + async def _measure(self) -> dict: """Do measurement, filling _measured dictionary. Returns dictionary with keys channel names, values numbers or arrays. diff --git a/yaqd-core/yaqd_core/_is_daemon.py b/yaqd-core/yaqd_core/_is_daemon.py index c6f03e1..44e6a32 100755 --- a/yaqd-core/yaqd_core/_is_daemon.py +++ b/yaqd-core/yaqd_core/_is_daemon.py @@ -87,7 +87,7 @@ def __init__( self._busy_sig = asyncio.Event() self._not_busy_sig = asyncio.Event() - self._loop = asyncio.get_event_loop() + self._loop = asyncio.get_running_loop() try: self._state_filepath.parent.mkdir(parents=True, exist_ok=True) @@ -123,17 +123,7 @@ def _traits(cls) -> List[str]: @classmethod def main(cls): - """Run the event loop.""" - loop = asyncio.get_event_loop() - if sys.platform.startswith("win"): - signals = () - else: - signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT) - for s in signals: - loop.add_signal_handler( - s, lambda s=s: asyncio.create_task(cls.shutdown_all(s, loop)) - ) - + """parse arguments and start the event loop""" parser = argparse.ArgumentParser() parser.add_argument( "--config", @@ -199,19 +189,26 @@ def main(cls): with open(config_filepath, "rb") as f: config_file = tomli.load(f) - loop.create_task(cls._main(config_filepath, config_file, args)) + # Run the event loop try: - loop.run_forever() + asyncio.run(cls._main(config_filepath, config_file, args)) except asyncio.CancelledError: pass - finally: - loop.close() @classmethod async def _main(cls, config_filepath, config_file, args=None): """Parse command line arguments, start event loop tasks.""" loop = asyncio.get_running_loop() - cls.__servers = [] + if sys.platform.startswith("win"): + signals = () + else: + signals = (signal.SIGHUP, signal.SIGTERM, signal.SIGINT) + for s in signals: + loop.add_signal_handler( + s, lambda s=s: asyncio.create_task(cls.shutdown_all(s, loop)) + ) + + cls.__servers = set() for section in config_file: if section == "shared-settings": continue @@ -225,7 +222,7 @@ async def _main(cls, config_filepath, config_file, args=None): while cls.__servers: awaiting = cls.__servers - cls.__servers = [] + cls.__servers = set() await asyncio.wait(awaiting) await asyncio.sleep(1) loop.stop() @@ -252,7 +249,9 @@ def server(daemon): server(daemon), config.get("host", ""), config.get("port", None) ) daemon._server = ser - cls.__servers.append(asyncio.create_task(ser.serve_forever())) + task = asyncio.create_task(ser.serve_forever()) + cls.__servers.add(task) + task.add_done_callback(cls.__servers.discard) @classmethod def _parse_config(cls, config_file, section, args=None): @@ -298,6 +297,8 @@ async def shutdown_all(cls, sig, loop): # are not themselves cancelled. [d.close() for d in cls._daemons] tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()] + for task in tasks: + logger.info(task.get_coro()) await asyncio.gather(*tasks, return_exceptions=True) [d._save_state() for d in cls._daemons] if hasattr(signal, "SIGHUP") and sig == signal.SIGHUP: diff --git a/yaqd-core/yaqd_core/_is_sensor.py b/yaqd-core/yaqd_core/_is_sensor.py index 735269b..1e050f9 100644 --- a/yaqd-core/yaqd_core/_is_sensor.py +++ b/yaqd-core/yaqd_core/_is_sensor.py @@ -1,24 +1,24 @@ +from __future__ import annotations + __all__ = ["IsSensor"] import asyncio import pathlib -from typing import Dict, Any, Union, Tuple, List +from typing import Any import yaqd_core -MeasureType = Dict[str, Union[float]] - class IsSensor(yaqd_core.IsDaemon): def __init__( - self, name: str, config: Dict[str, Any], config_filepath: pathlib.Path + self, name: str, config: dict[str, Any], config_filepath: pathlib.Path ): super().__init__(name, config, config_filepath) - self._measured: MeasureType = dict() # values must be numbers or arrays - self._channel_names: List[str] = [] - self._channel_units: Dict[str, str] = dict() - self._channel_shapes: Dict[str, Tuple[int]] = dict() + self._measured: dict = dict() # values must be numbers or arrays + self._channel_names: list[str] = [] + self._channel_units: dict[str, str] = dict() + self._channel_shapes: dict[str, tuple[int, ...]] = dict() self._measurement_id = 0 self._measured["measurement_id"] = self._measurement_id @@ -37,7 +37,7 @@ def get_channel_units(self): """Get channel units.""" return self._channel_units - def get_measured(self) -> MeasureType: + def get_measured(self) -> dict: assert "measurement_id" in self._measured return self._measured diff --git a/yaqd-core/yaqd_core/_protocol.py b/yaqd-core/yaqd_core/_protocol.py index 6644a08..10bd3fa 100644 --- a/yaqd-core/yaqd_core/_protocol.py +++ b/yaqd-core/yaqd_core/_protocol.py @@ -28,7 +28,7 @@ def connection_made(self, transport): self.transport = transport self.unpacker = avrorpc.Unpacker(self._avro_protocol) self._daemon._connection_made(peername) - self.task = asyncio.get_event_loop().create_task(self.process_requests()) + self.task = asyncio.get_running_loop().create_task(self.process_requests()) def data_received(self, data): """Process an incomming request.""" diff --git a/yaqd-fakes/yaqd_fakes/_fake_sensor.py b/yaqd-fakes/yaqd_fakes/_fake_sensor.py index c56f1b3..980115e 100644 --- a/yaqd-fakes/yaqd_fakes/_fake_sensor.py +++ b/yaqd-fakes/yaqd_fakes/_fake_sensor.py @@ -28,7 +28,7 @@ def __init__(self, name, config, config_filepath): self._channel_generators[name] = random_walk(min_, max_) else: raise Exception(f"channel kind {kwargs['kind']} not recognized") - asyncio.get_event_loop().create_task(self._update_measurements()) + self._loop.create_task(self._update_measurements()) async def _update_measurements(self): while True: