def _handle_inotify_event(self, wd):
        """Handle a series of events coming-in from inotify."""

        b = os.read(wd, 1024)
        if not b:
            return

        self.__buffer += b

        while 1:
            length = len(self.__buffer)

            if length < _STRUCT_HEADER_LENGTH:
                _LOGGER.debug("Not enough bytes for a header.")
                return

            # We have, at least, a whole-header in the buffer.

            peek_slice = self.__buffer[:_STRUCT_HEADER_LENGTH]

            header_raw = struct.unpack(
                            _HEADER_STRUCT_FORMAT,
                            peek_slice)

            header = _INOTIFY_EVENT(*header_raw)
            type_names = self._get_event_names(header.mask)
            _LOGGER.debug("Events received in stream: {}".format(type_names))

            event_length = (_STRUCT_HEADER_LENGTH + header.len)
            if length < event_length:
                return

            filename = self.__buffer[_STRUCT_HEADER_LENGTH:event_length]

            # Our filename is 16-byte aligned and right-padded with NULs.
            filename_bytes = filename.rstrip(b'\0')

            self.__buffer = self.__buffer[event_length:]

            path = self.__watches_r.get(header.wd)
            if path is not None:
                filename_unicode = filename_bytes.decode('utf8')
                yield (header, type_names, path, filename_unicode)

            buffer_length = len(self.__buffer)
            if buffer_length < _STRUCT_HEADER_LENGTH:
                break
def _handle_inotify_event ( self , wd ) : b = os . read ( wd , 1024 ) if not b : return self . __buffer += b while 1 : length = len ( self . __buffer ) if length < _STRUCT_HEADER_LENGTH : _LOGGER . debug ( "Not enough bytes for a header." ) return peek_slice = self . __buffer [ : _STRUCT_HEADER_LENGTH ] header_raw = struct . unpack ( _HEADER_STRUCT_FORMAT , peek_slice ) header = _INOTIFY_EVENT ( * header_raw ) type_names = self . _get_event_names ( header . mask ) _LOGGER . debug ( "Events received in stream: {}" . format ( type_names ) ) event_length = ( _STRUCT_HEADER_LENGTH + header . len ) if length < event_length : return filename = self . __buffer [ _STRUCT_HEADER_LENGTH : event_length ] filename_bytes = filename . rstrip ( b'\0' ) self . __buffer = self . __buffer [ event_length : ] path = self . __watches_r . get ( header . wd ) if path is not None : filename_unicode = filename_bytes . decode ( 'utf8' ) yield ( header , type_names , path , filename_unicode ) buffer_length = len ( self . __buffer ) if buffer_length < _STRUCT_HEADER_LENGTH : break
Handle a series of events coming-in from inotify.