import logging
import pyinotify

log = logging.getLogger(__name__)


class Watcher(pyinotify.ProcessEvent):

    def __init__(self, base, queue):
        self.queue = queue
        self.wm = pyinotify.WatchManager()
        self.notifier = pyinotify.ThreadedNotifier(self.wm, self)
        self.notifier.setDaemon(True)
        self.notifier.start()
        self.wm.add_watch(base, pyinotify.IN_CLOSE_WRITE, rec=True)

    def stop(self):
        self.notifier.stop()

    def process_IN_CLOSE(self, event):
        if event.pathname.lower().endswith('.mp3'):
            log.debug('event in %s: %x' % (event.pathname, event.mask))
            self.queue.put(event.pathname)