pybackive/backive/core/events.py

27 lines
840 B
Python
Raw Normal View History

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
if not unix_socket_path:
unix_socket_path = "/tmp/backive/backive.sock"
if not os.path.exists(os.path.dirname(unix_socket_path)):
os.makedirs(os.path.dirname(unix_socket_path))
2019-02-24 22:42:36 +01:00
try:
2019-05-03 20:36:09 +02:00
os.remove(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()
2019-05-03 20:36:09 +02:00
loop.create_task(asyncio.start_unix_server(self.client_connected, 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