add filter function to the dispatcher

This commit is contained in:
Tomasz Frątczak 2024-02-13 00:22:02 +01:00
parent 0a0007e1d0
commit 1483812c3b
2 changed files with 12 additions and 13 deletions

View File

@ -1,16 +1,26 @@
import threading import threading
import os
class Dispatcher: class Dispatcher:
threads = [] threads = []
callbackExecutor = None callbackExecutor = None
extensionFilter = None
def __init__(self, callbackExecutor): def __init__(self, callbackExecutor):
self.callbackExecutor = callbackExecutor self.callbackExecutor = callbackExecutor
def apply_filter(self, audio_file):
# TODO: return True if audio file is valid, based on name, extension, and other criteria
if os.path.isdir(audio_file):
return False
return True
def execute(self, audio_file): def execute(self, audio_file):
thread = threading.Thread(target=self._execute_thread, args=(audio_file.pathname,)) if self.apply_filter(audio_file.pathname):
thread.start() self.threads.append(threading.Thread(target=self._execute_thread, args=(audio_file.pathname,)))
self.threads[-1].start()
# TODO: we need to have a way to wipe old threads, otherwise we will have a memory leak
def _execute_thread(self, audio_file): def _execute_thread(self, audio_file):
self.callbackExecutor.execute(audio_file) self.callbackExecutor.execute(audio_file)

View File

@ -21,14 +21,3 @@ class Picker:
print(f'Watching {self.directory} for changes...') print(f'Watching {self.directory} for changes...')
notifier.loop() notifier.loop()
# def observe_directory(directory):
# wm = pyinotify.WatchManager()
# notifier = pyinotify.Notifier(wm, EventHandler())
# wm.add_watch(directory, pyinotify.IN_CREATE)
# print(f'Watching {directory} for changes...')
# notifier.loop()
# Use the function
# observe_directory('/home/octopusx/Code/test')