From 9cd58d2063cab3ae4f442539b3c8610b7044657b Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Tue, 31 Mar 2026 12:17:50 +0200 Subject: [PATCH] Ruff and Ty linting and bump to 0.4.0 --- controlpi_plugins/graph.py | 89 ++++++++++++++++----------- controlpi_plugins/graph_connection.py | 66 +++++++++++--------- setup.py | 2 +- test_graph_connection.py | 1 - 4 files changed, 89 insertions(+), 69 deletions(-) diff --git a/controlpi_plugins/graph.py b/controlpi_plugins/graph.py index 4d7b56c..2174dba 100644 --- a/controlpi_plugins/graph.py +++ b/controlpi_plugins/graph.py @@ -1,4 +1,5 @@ """Provide Graph Connections as ControlPi Plugin.""" + import json from controlpi import BasePlugin, Message, MessageTemplate @@ -8,48 +9,59 @@ from controlpi_plugins.graph_connection import GraphConnection class Graph(BasePlugin): """Graph connection plugin.""" - CONF_SCHEMA = {'properties': - {'url': {'type': 'string'}, - 'crt': {'type': 'string'}, - 'name': {'type': 'string'}, - 'filter': {'type': 'array', - 'items': {'type': 'object'}}}, - 'required': ['url', 'crt', 'name', 'filter']} + CONF_SCHEMA = { + "properties": { + "url": {"type": "string"}, + "crt": {"type": "string"}, + "name": {"type": "string"}, + "filter": {"type": "array", "items": {"type": "object"}}, + }, + "required": ["url", "crt", "name", "filter"], + } def process_conf(self) -> None: """Register plugin as bus client.""" - self.graph_connection = GraphConnection(self.conf['url'], - self.conf['crt']) + self.graph_connection = GraphConnection(self.conf["url"], self.conf["crt"]) self.messages: list[Message] = [] - self.bus.register(self.name, 'Graph', - [], - [([MessageTemplate({'target': {'const': self.name}, - 'command': {'const': 'sync'}})], - self.sync), - (self.conf['filter'], self.receive)]) + self.bus.register( + self.name, + "Graph", + [], + [ + ( + [ + MessageTemplate( + { + "target": {"const": self.name}, + "command": {"const": "sync"}, + } + ) + ], + self.sync, + ), + (self.conf["filter"], self.receive), + ], + ) async def send(self, messages: list[Message]) -> None: """Send a list of messages to configured graph.""" async with self.graph_connection as gc: - coroot_guid = await gc.call('attributsknoten', - ['coroot_name', - self.conf['name']]) + coroot_guid = await gc.call( + "attributsknoten", ["coroot_name", self.conf["name"]] + ) if coroot_guid: - comessage_guid = await gc.call('erzeuge', ['comessage']) + comessage_guid = await gc.call("erzeuge", ["comessage"]) if comessage_guid: - await gc.call('verknuepfe', [comessage_guid, - coroot_guid]) - await gc.call('setze', [comessage_guid, - 'comessage_json', - json.dumps(messages)]) - await gc.call('setze', [comessage_guid, - 'comessage_ready', - True]) + await gc.call("verknuepfe", [comessage_guid, coroot_guid]) + await gc.call( + "setze", + [comessage_guid, "comessage_json", json.dumps(messages)], + ) + await gc.call("setze", [comessage_guid, "comessage_ready", True]) else: raise Exception("Could not create comessage instance") else: - raise Exception("Did not find coroot instance" - f" '{self.conf['name']}'.") + raise Exception(f"Did not find coroot instance '{self.conf['name']}'.") async def sync(self, message: Message) -> None: """Sync cached messages to configured graph.""" @@ -60,9 +72,11 @@ class Graph(BasePlugin): await self.send(messages) except Exception as e: self.messages.extend(messages) - print(f"Graph connection '{self.name}'" - f" to {self.conf['url']}:" - f" Exception in 'sync()': {e}") + print( + f"Graph connection '{self.name}'" + f" to {self.conf['url']}:" + f" Exception in 'sync()': {e}" + ) async def receive(self, message: Message) -> None: """Receive message through controlpi bus.""" @@ -71,9 +85,10 @@ class Graph(BasePlugin): async def run(self) -> None: """Get coroot instance for name and send connection opened event.""" try: - await self.send([Message(self.name, - {'event': 'connection opened'})]) + await self.send([Message(self.name, {"event": "connection opened"})]) except Exception as e: - print(f"Graph connection '{self.name}'" - f" to {self.conf['url']}:" - f" Exception in 'run()': {e}") + print( + f"Graph connection '{self.name}'" + f" to {self.conf['url']}:" + f" Exception in 'run()': {e}" + ) diff --git a/controlpi_plugins/graph_connection.py b/controlpi_plugins/graph_connection.py index 86d707b..b76d075 100644 --- a/controlpi_plugins/graph_connection.py +++ b/controlpi_plugins/graph_connection.py @@ -1,7 +1,8 @@ """Provide an asynchronous context manager for graph connections.""" + import asyncio from dataclasses import dataclass -import msgpack # type: ignore +import msgpack import os.path import ssl import struct @@ -30,7 +31,7 @@ class GraphConnection: """Initialise with graph URL and TLS certificate.""" self.url = url parsed_url = urllib.parse.urlparse(url) - if parsed_url.scheme != 'tls': + if parsed_url.scheme != "tls": raise Exception("Only implemented scheme is 'tls'.") self.hostname = parsed_url.hostname self.port = parsed_url.port @@ -42,19 +43,19 @@ class GraphConnection: self.connections[self.url] = GraphConnectionData(asyncio.Lock()) self.connection = self.connections[self.url] - async def __aenter__(self) -> 'GraphConnection': + async def __aenter__(self) -> "GraphConnection": """Open connection if first context.""" async with self.connection.lock: if self.connection.contexts == 0: # Open connection: - (r, w) = await asyncio.open_connection(self.hostname, - self.port, - ssl=self.ssl) + (r, w) = await asyncio.open_connection( + self.hostname, self.port, ssl=self.ssl + ) self.connection.opened += 1 # Read banner: size_bytes = await r.readexactly(4) - size_int = struct.unpack('