From: Benjamin Braatz Date: Fri, 5 Mar 2021 00:32:39 +0000 (+0100) Subject: Move controlpi.plugins to controlpi-plugins X-Git-Tag: v0.3.0~76 X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=26742cdaf036cbdbcde7e7d54b954b7174c742a3;p=graphit%2Fcontrolpi.git Move controlpi.plugins to controlpi-plugins Namespace packages should not be inside regular packages: https://stackoverflow.com/a/62992832 --- diff --git a/controlpi-plugins/util.py b/controlpi-plugins/util.py new file mode 100644 index 0000000..0e598a2 --- /dev/null +++ b/controlpi-plugins/util.py @@ -0,0 +1,97 @@ +"""Provide utility plugins for all kinds of systems. + +TODO: documentation, doctests, check configurations during _process_conf +TODO: State, AndState, OrState? +""" +import asyncio + +from controlpi import BasePlugin, Message, PluginConfiguration + + +class Log(BasePlugin): + async def _log(self, message: Message) -> None: + print(f"{self._name}: {message}") + + def _process_conf(self, conf: PluginConfiguration) -> None: + self._bus.register(self._name, [], conf['filter'], self._log) + super()._process_conf(conf) + + +class Init(BasePlugin): + async def _execute(self, message: Message) -> None: + for message in self._messages: + await self._bus.send(message) + + def _process_conf(self, conf: PluginConfiguration) -> None: + self._messages = [] + for message in conf['messages']: + complete_message = {'sender': self._name} + complete_message.update(message) + self._messages.append(complete_message) + receives = [{'target': self._name, 'command': 'execute'}] + sends = [] + sends.extend(receives) + sends.extend(conf['messages']) + self._bus.register(self._name, sends, receives, self._execute) + super()._process_conf(conf) + + async def run(self) -> None: + await super().run() + await self._bus.send({'sender': self._name, 'target': self._name, + 'command': 'execute'}) + + +class Wait(BasePlugin): + async def _wait(self, message: Message) -> None: + await asyncio.sleep(self._seconds) + await self._bus.send({'sender': self._name, 'event': 'finished'}) + + def _process_conf(self, conf: PluginConfiguration) -> None: + self._seconds = conf['seconds'] + receives = [{'target': self._name, 'command': 'wait'}] + sends = [{'event': 'finished'}] + self._bus.register(self._name, sends, receives, self._wait) + super()._process_conf(conf) + + +class Alias(BasePlugin): + async def _alias(self, message: Message) -> None: + alias_message = {} + alias_message['sender'] = self._name + alias_message.update(self._to) + for key in message: + if key != 'sender' and key not in self._from: + alias_message[key] = message[key] + await self._bus.send(alias_message) + + def _process_conf(self, conf: PluginConfiguration) -> None: + self._from = conf['from'] + self._to = conf['to'] + self._bus.register(self._name, [self._to], [self._from], self._alias) + super()._process_conf(conf) + + +class State(BasePlugin): + async def _receive(self, message: Message) -> None: + if 'command' in message: + if message['command'] == 'get state': + await self._bus.send({'sender': self._name, + 'state': self._state, + 'changed': False}) + elif message['command'] == 'set state': + if 'state' in message and self._state != message['state']: + self._state: bool = message['state'] + await self._bus.send({'sender': self._name, + 'state': self._state, + 'changed': True}) + + def _process_conf(self, conf: PluginConfiguration) -> None: + self._state = False + sends: list[Message] = [{'sender': self._name, 'state': bool}] + receives: list[Message] = [{'target': self._name, + 'command': 'get state'}, + {'target': self._name, + 'command': 'set state', + 'state': bool}] + self._bus.register(self._name, sends, receives, self._receive) + super()._process_conf(conf) diff --git a/controlpi/__init__.py b/controlpi/__init__.py index 8657d2a..176a1c0 100644 --- a/controlpi/__init__.py +++ b/controlpi/__init__.py @@ -257,7 +257,7 @@ async def run(conf: Configuration) -> None: """ if not conf or not check_configuration(conf): return - plugins = PluginRegistry('controlpi.plugins', BasePlugin) + plugins = PluginRegistry('controlpi-plugins', BasePlugin) message_bus = MessageBus() coroutines = [message_bus.run()] for instance_name in conf: diff --git a/controlpi/plugins/util.py b/controlpi/plugins/util.py deleted file mode 100644 index 0e598a2..0000000 --- a/controlpi/plugins/util.py +++ /dev/null @@ -1,97 +0,0 @@ -"""Provide utility plugins for all kinds of systems. - -TODO: documentation, doctests, check configurations during _process_conf -TODO: State, AndState, OrState? -""" -import asyncio - -from controlpi import BasePlugin, Message, PluginConfiguration - - -class Log(BasePlugin): - async def _log(self, message: Message) -> None: - print(f"{self._name}: {message}") - - def _process_conf(self, conf: PluginConfiguration) -> None: - self._bus.register(self._name, [], conf['filter'], self._log) - super()._process_conf(conf) - - -class Init(BasePlugin): - async def _execute(self, message: Message) -> None: - for message in self._messages: - await self._bus.send(message) - - def _process_conf(self, conf: PluginConfiguration) -> None: - self._messages = [] - for message in conf['messages']: - complete_message = {'sender': self._name} - complete_message.update(message) - self._messages.append(complete_message) - receives = [{'target': self._name, 'command': 'execute'}] - sends = [] - sends.extend(receives) - sends.extend(conf['messages']) - self._bus.register(self._name, sends, receives, self._execute) - super()._process_conf(conf) - - async def run(self) -> None: - await super().run() - await self._bus.send({'sender': self._name, 'target': self._name, - 'command': 'execute'}) - - -class Wait(BasePlugin): - async def _wait(self, message: Message) -> None: - await asyncio.sleep(self._seconds) - await self._bus.send({'sender': self._name, 'event': 'finished'}) - - def _process_conf(self, conf: PluginConfiguration) -> None: - self._seconds = conf['seconds'] - receives = [{'target': self._name, 'command': 'wait'}] - sends = [{'event': 'finished'}] - self._bus.register(self._name, sends, receives, self._wait) - super()._process_conf(conf) - - -class Alias(BasePlugin): - async def _alias(self, message: Message) -> None: - alias_message = {} - alias_message['sender'] = self._name - alias_message.update(self._to) - for key in message: - if key != 'sender' and key not in self._from: - alias_message[key] = message[key] - await self._bus.send(alias_message) - - def _process_conf(self, conf: PluginConfiguration) -> None: - self._from = conf['from'] - self._to = conf['to'] - self._bus.register(self._name, [self._to], [self._from], self._alias) - super()._process_conf(conf) - - -class State(BasePlugin): - async def _receive(self, message: Message) -> None: - if 'command' in message: - if message['command'] == 'get state': - await self._bus.send({'sender': self._name, - 'state': self._state, - 'changed': False}) - elif message['command'] == 'set state': - if 'state' in message and self._state != message['state']: - self._state: bool = message['state'] - await self._bus.send({'sender': self._name, - 'state': self._state, - 'changed': True}) - - def _process_conf(self, conf: PluginConfiguration) -> None: - self._state = False - sends: list[Message] = [{'sender': self._name, 'state': bool}] - receives: list[Message] = [{'target': self._name, - 'command': 'get state'}, - {'target': self._name, - 'command': 'set state', - 'state': bool}] - self._bus.register(self._name, sends, receives, self._receive) - super()._process_conf(conf) diff --git a/doc/index.md b/doc/index.md index bdfff6a..b0fe96f 100644 --- a/doc/index.md +++ b/doc/index.md @@ -91,8 +91,8 @@ Die ControlPi-Infrastruktur hat zwei Haupt-Bestandteile: weitere Plugin-Klassen in Python zu implementieren und (in anderen Code-Repositories und anderen Distributions-Paketen) zum System hinzuzufügen. - Im Modul `controlpi.plugins.util` bzw. der Datei - `controlpi/plugins/util.py` befindet sich eine Reihe nützlicher + Im Modul `controlpi-plugins.util` bzw. der Datei + `controlpi-plugins/util.py` befindet sich eine Reihe nützlicher Standard-Plugins, an denen die generelle Struktur nachvollzogen werden kann. - Ein Nachrichten-Bus erlaubt den Austausch von Nachrichten zwischen allen @@ -119,7 +119,7 @@ Nachrichten. Sie müssen nicht unbedingt bestimmte Empfänger haben, sie gehören nicht unbedingt zu einem bestimmten Typen etc. -Einige Konventionen, die z.B. von den Plugins in `controlpi.plugins.util` +Einige Konventionen, die z.B. von den Plugins in `controlpi-plugins.util` eingehalten werden, sind aber: - Einige Nachrichten signalisieren, dass ein Ereignis eingetreten ist. Sie haben in der Regel einen Schlüssel `'event'` mit einem Wert, der die diff --git a/setup.py b/setup.py index fb945fd..af67d93 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setuptools.setup( long_description=long_description, long_description_content_type="text/markdown", url="http://docs.graph-it.com/graphit/controlpi", - packages=["controlpi", "controlpi.plugins"], + packages=["controlpi", "controlpi-plugins"], package_data={"controlpi": ["py.typed"]}, zip_safe=False, extras_require={