diff --git a/backive/__init__.py b/backive/__init__.py index 46083f1..6ebb29f 100644 --- a/backive/__init__.py +++ b/backive/__init__.py @@ -6,7 +6,7 @@ import os import pwd -logging.basicConfig(format='%(asctime)s->%(name)s@%(levelname)s: %(message)s', filename=sys.stdout, level=logging.INFO) +logging.basicConfig(format='%(asctime)s->%(name)s@%(levelname)s: %(message)s', level=logging.INFO) got_signal = False disks_by_uuid = "/dev/disk/by-uuid" diff --git a/backive/backive_service b/backive/backive_service index eb83a62..56c6ebc 100644 --- a/backive/backive_service +++ b/backive/backive_service @@ -3,10 +3,13 @@ """ Service startup script. """ +from backive.core.events import EventInterface + class Backive: def __init__(self): - pass + self._config = None + self._events = None def serve(self): pass diff --git a/backive/config/schema.yml b/backive/config/schema.yml index b1e9af2..0cd5c03 100644 --- a/backive/config/schema.yml +++ b/backive/config/schema.yml @@ -7,7 +7,8 @@ definitions: device_section: type: object patternProperties: - "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$": + "^[^ \t/\\]+$": + # "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$": type: object properties: name: diff --git a/backive/core/events.py b/backive/core/events.py index 36a8019..5613221 100644 --- a/backive/core/events.py +++ b/backive/core/events.py @@ -1,24 +1,26 @@ import os -import socket +import asyncio class EventInterface: - def __init__(self, unix_socket=None): - if not unix_socket: - unix_socket = "/tmp/backive/backive.sock" - self.socket = socket.socket( - socket.AF_UNIX, - socket.SOCK_STREAM - ) + 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)) try: - os.remove(unix_socket) + os.remove(unix_socket_path) except OSError: pass - self.socket.bind(unix_socket) - self.socket.listen() - - def accept(self): - return self.socket.accept() + if not loop: + loop = asyncio.get_running_loop() + loop.create_task(asyncio.start_unix_server(self.client_connected, unix_socket_path)) + async def client_connected(self, reader, writer): + print("client_connected") + data = None + data = (await reader.read()).decode('utf8') + self.event_callback(data)