From 4f9e7ada33f0fc80708d27b15e6e9bb8d2f85fd1 Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 29 Dec 2024 17:39:57 +0100 Subject: [PATCH] Cleanup --- src/boeh/__init__.py | 59 --------------------------------------- src/boeh/__main__.py | 23 --------------- src/boeh/env.py | 29 ------------------- src/rwebgui/__main__.py | 34 +++++++++++++++++++++-- src/rwebgui/component.py | 60 +++++++++++++++++++++------------------- 5 files changed, 62 insertions(+), 143 deletions(-) delete mode 100644 src/boeh/__init__.py delete mode 100644 src/boeh/__main__.py delete mode 100644 src/boeh/env.py diff --git a/src/boeh/__init__.py b/src/boeh/__init__.py deleted file mode 100644 index 7ec8583..0000000 --- a/src/boeh/__init__.py +++ /dev/null @@ -1,59 +0,0 @@ -import random - -from nio import AsyncClient, RoomMessageText - - -class BooeehBot: - - def generate_boeh(self): - boeh = "b" - for _ in range(random.randint(1, 10)): - boeh += "o" - for _ in range(random.randint(1, 5)): - boeh += "e" - for _ in range(random.randint(1, 3)): - boeh += "e" - return boeh - - def __init__(self, url, username, password): - self.url = url - self.username = username - self.password = password - self.client = AsyncClient(url, username) - - async def login(self): - try: - response = await self.client.login(self.password) - print(f"Logged in. Serving {self.username}.") - return response - except Exception as e: - print(f"Login error: {e}") - return None - - async def handle_message(self, room, event): - specific_user_id = "@joewilliams007:matrix.org" - - if isinstance(event, RoomMessageText): - if event.sender == specific_user_id: - response_text = self.generate_boeh() - try: - await self.client.room_send( - room.room_id, - message_type="m.room.message", - content={"msgtype": "m.text", "body": response_text}, - ) - print(f"Response to {event.sender}: " + response_text) - except Exception as e: - print(f"Failed to send message: {e}") - - async def start(self): - login_response = await self.login() - if not login_response: - return - - self.client.add_event_callback(self.handle_message, RoomMessageText) - - await self.client.sync_forever(timeout=30000) - - async def stop(self): - await self.client.close() diff --git a/src/boeh/__main__.py b/src/boeh/__main__.py deleted file mode 100644 index d44e1f5..0000000 --- a/src/boeh/__main__.py +++ /dev/null @@ -1,23 +0,0 @@ -import asyncio - -from boeh import BooeehBot, env - - -async def main_async(): - url = "https://matrix.org" - username = "@retoor2:matrix.org" - password = env.secret4 - bot = BooeehBot(url, username, password) - - try: - await bot.start() - except KeyboardInterrupt: - await bot.stop() - - -def main(): - asyncio.run(main_async()) - - -if __name__ == "__main__": - main() diff --git a/src/boeh/env.py b/src/boeh/env.py deleted file mode 100644 index 4869bc7..0000000 --- a/src/boeh/env.py +++ /dev/null @@ -1,29 +0,0 @@ -import base64 -import os - -secret = None -secret2 = None -secret3 = None -secret4 = None - -if __name__ == "__main__": - secret = input("Type secret: ") - print(base64.b64encode(secret.encode()).decode()) -else: - - try: - secret = base64.b64decode(os.getenv("SECRET", "").encode()).decode() - except: - pass - try: - secret2 = base64.b64decode(os.getenv("SECRET2", "").encode()).decode() - except: - pass - try: - secret3 = base64.b64decode(os.getenv("SECRET3", "").encode()).decode() - except: - pass - try: - secret4 = base64.b64decode(os.getenv("SECRET4", "").encode()).decode() - except: - pass diff --git a/src/rwebgui/__main__.py b/src/rwebgui/__main__.py index ab439cf..4d99fca 100644 --- a/src/rwebgui/__main__.py +++ b/src/rwebgui/__main__.py @@ -1,8 +1,36 @@ +# Written by retoor@molodetz.nl + +# This script initializes a web application using aiohttp and runs it asynchronously with a thread pool executor. + +# Imports aiohttp for web server functionality and concurrent.futures for handling asynchronous execution. + +# +# MIT License +# +# Copyright (c) 2023 Future Contributor +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + + import asyncio from concurrent.futures import ThreadPoolExecutor as Executor - from aiohttp import web - from rwebgui.app import Application @@ -15,4 +43,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file diff --git a/src/rwebgui/component.py b/src/rwebgui/component.py index b1428a5..60447ca 100644 --- a/src/rwebgui/component.py +++ b/src/rwebgui/component.py @@ -1,18 +1,41 @@ +# Written by retoor@molodetz.nl + +# This module defines a Component class that facilitates WebSocket communication and management of various tasks in an asynchronous environment. It allows dynamic creation of child components and interaction through callbacks and events. + +# Imports used: aiohttp (external library for client-server communication) + +# MIT License +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + + import asyncio import time import uuid - from aiohttp import web - class Component: - @classmethod def define(cls): return cls def __init__(self, app, id_, description=None, ws: web.WebSocketResponse = None): - self.id = id_ self.ws = ws self.app = app @@ -23,12 +46,11 @@ class Component: self._running = False if not hasattr(self, "Children"): return + for name in dir(self.Children): if name.startswith("__"): continue - obj = getattr(self.Children, name) - instance = obj(app=self.app, id_=name, ws=ws) self.add_child(instance) instance.app = self @@ -64,19 +86,15 @@ class Component: if name.startswith("task_") and hasattr(self, name) ] for child in self.children: - tasks_ += child.tasks # .extend(await child.get_tasks()) + tasks_ += child.tasks return tasks_ async def communicate(self, event_id=None): - async for msg in self.ws: if msg.type == web.WSMsgType.TEXT: - # Echo the message back to the client - # print(f"Received message: {msg.data}") data = msg.json() if not event_id: pass - # return data else: if data.get("event_id") == event_id: return data @@ -91,7 +109,6 @@ class Component: if hasattr(self, method_name): method = getattr(self, method_name) await method(data) - print("JAAJ") for child in self.children: await child.trigger(id_, event, data) @@ -121,11 +138,8 @@ class Component: if callback: response = await self.communicate(event_id=event_id) return response["result"] - # print("GLUKT") - # return response['result'] return True - # return await future async def get_attr(self, key, default=None): result = await self.call("getAttr", [self.id, key], True) @@ -157,7 +171,6 @@ class Component: async def toggle(self): value = await self.get_style("display", "block") - if value == "none": value = "" else: @@ -179,7 +192,7 @@ class Component: async def get_tasks(self): tasks = self.tasks for child in self.children: - tasks += child.tasks # .extend(await child.get_tasks()) + tasks += child.tasks return tasks async def set_running(self): @@ -197,8 +210,6 @@ class Component: try: async for msg in self.ws: if msg.type == web.WSMsgType.TEXT: - # Echo the message back to the client - # print(f"Received message: {msg.data}") data = msg.json() response = {"event_id": data["event_id"], "success": True} response["time_start"] = time.time() @@ -218,25 +229,16 @@ class Component: response["time_end"] - response["time_start"] ) await self.ws.send_json(response) - - # await ws.send_str(f"Echo: {msg.data}") elif msg.type == web.WSMsgType.ERROR: print(f"WebSocket error: {self.ws.exception()}") except Exception as ex: print(ex) pass - # async def the_task(): - # while True: - # time.sleep(1) - # while True: tasks.append(events) await asyncio.gather(*[task() for task in tasks]) - # await asyncio.create_task(asyncio.gather(*[task() for task in tasks])) - # await tasks() - print("AFTERR") def add_child(self, child): child.app = self.app child.ws = self.ws - self.children.append(child) + self.children.append(child) \ No newline at end of file