Log PipeConnector events

This commit is contained in:
Kristóf Tóth 2019-08-30 17:45:54 +02:00
parent bb5c000b1d
commit a5db2f23a0

View File

@ -1,3 +1,4 @@
import logging
from stat import S_ISFIFO from stat import S_ISFIFO
from os import access, mkdir, stat, R_OK from os import access, mkdir, stat, R_OK
from os.path import exists from os.path import exists
@ -6,6 +7,8 @@ from pipe_io_server import PipeWriterServer
from tfw.internals.inotify import InotifyObserver, InotifyFileCreatedEvent, InotifyFileDeletedEvent from tfw.internals.inotify import InotifyObserver, InotifyFileCreatedEvent, InotifyFileDeletedEvent
LOG = logging.getLogger(__name__)
class PipeConnector: class PipeConnector:
reader_pattern, writer_pattern = 'send', 'recv' reader_pattern, writer_pattern = 'send', 'recv'
@ -27,15 +30,17 @@ class PipeConnector:
path = event.src_path path = event.src_path
if self._is_pipe(path): if self._is_pipe(path):
if isinstance(event, InotifyFileCreatedEvent): if isinstance(event, InotifyFileCreatedEvent):
self._create_pipe(path) self._on_create_pipe(path)
LOG.debug('Connected to new pipe "%s"', path)
elif isinstance(event, InotifyFileDeletedEvent): elif isinstance(event, InotifyFileDeletedEvent):
self._delete_pipe(path) self._on_delete_pipe(path)
LOG.debug('Disconnected from deleted pipe "%s"', path)
@staticmethod @staticmethod
def _is_pipe(path): def _is_pipe(path):
return exists(path) and S_ISFIFO(stat(path).st_mode) return exists(path) and S_ISFIFO(stat(path).st_mode)
def _create_pipe(self, path): def _on_create_pipe(self, path):
if self._find_pipe(path): if self._find_pipe(path):
return return
server = None server = None
@ -61,7 +66,7 @@ class PipeConnector:
def build_writer(self, path): # pylint: disable=no-self-use def build_writer(self, path): # pylint: disable=no-self-use
return PipeWriterServer(path, manage_pipes=False) return PipeWriterServer(path, manage_pipes=False)
def _delete_pipe(self, path): def _on_delete_pipe(self, path):
pipes = self._find_pipe(path) pipes = self._find_pipe(path)
if pipes: if pipes:
pipes[path].stop() pipes[path].stop()