pybackive/backive/core/events.py

34 lines
1.0 KiB
Python
Raw Normal View History

2021-05-24 23:02:54 +02:00
"""
TODO
"""
2019-02-24 22:42:36 +01:00
import os
2019-05-03 20:36:09 +02:00
import asyncio
2019-02-24 22:42:36 +01:00
class EventInterface:
2019-05-03 20:36:09 +02:00
def __init__(self, event_callback, unix_socket_path=None, loop=None):
self.event_callback = event_callback
2020-01-06 13:26:52 +01:00
self.unix_socket_path = unix_socket_path
if not self.unix_socket_path:
self.unix_socket_path = "/tmp/backive/backive.sock"
if not os.path.exists(os.path.dirname(self.unix_socket_path)):
os.makedirs(os.path.dirname(self.unix_socket_path))
2019-02-24 22:42:36 +01:00
try:
2020-01-06 13:26:52 +01:00
os.remove(self.unix_socket_path)
2019-02-24 22:42:36 +01:00
except OSError:
pass
2019-05-03 20:36:09 +02:00
if not loop:
loop = asyncio.get_event_loop()
2020-01-06 13:26:52 +01:00
loop.create_task(asyncio.start_unix_server(self.client_connected, self.unix_socket_path))
2019-02-24 22:42:36 +01:00
2019-05-03 20:36:09 +02:00
async def client_connected(self, reader, writer):
print("client_connected")
data = None
data = (await reader.read()).decode('utf8')
await self.event_callback(data)
2019-02-24 22:42:36 +01:00
2020-01-06 13:26:52 +01:00
def __del__(self):
print("Removing socket file...")
os.remove(self.unix_socket_path)