From 1483812c3b9100112fb026c191a748ef5d902a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Fr=C4=85tczak?= Date: Tue, 13 Feb 2024 00:22:02 +0100 Subject: [PATCH] add filter function to the dispatcher --- notescriber/dispatcher.py | 14 ++++++++++++-- notescriber/picker.py | 11 ----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/notescriber/dispatcher.py b/notescriber/dispatcher.py index 3b57348..b7d2eb6 100644 --- a/notescriber/dispatcher.py +++ b/notescriber/dispatcher.py @@ -1,16 +1,26 @@ import threading +import os class Dispatcher: threads = [] callbackExecutor = None + extensionFilter = None def __init__(self, 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): - thread = threading.Thread(target=self._execute_thread, args=(audio_file.pathname,)) - thread.start() + if self.apply_filter(audio_file.pathname): + 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): self.callbackExecutor.execute(audio_file) \ No newline at end of file diff --git a/notescriber/picker.py b/notescriber/picker.py index d08f1c7..834b091 100644 --- a/notescriber/picker.py +++ b/notescriber/picker.py @@ -21,14 +21,3 @@ class Picker: print(f'Watching {self.directory} for changes...') 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')