trying to implement the mediator pattern

This commit is contained in:
Tomasz Frątczak 2024-02-13 20:54:06 +01:00
parent 57803c0586
commit 94f632b323
7 changed files with 63 additions and 29 deletions

View File

@ -1,11 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
from notescriber.transcriber import Transcriber # from notescriber.transcriber import Transcriber
from notescriber.dispatcher import Dispatcher # from notescriber.dispatcher import Dispatcher
from notescriber.picker import Picker # from notescriber.picker import Picker
from notescriber import picker, dispatcher
if __name__ == '__main__': if __name__ == '__main__':
transcriber = Transcriber()
dispatcher = Dispatcher(transcriber)
picker = Picker("/home/octopusx/Code/test", dispatcher)
picker.observe() picker.observe()
# dispatcher.halt()
# picker.halt()

View File

@ -3,3 +3,11 @@ from notescriber import dispatcher
from notescriber import notetaker from notescriber import notetaker
from notescriber import picker from notescriber import picker
from notescriber import transcriber from notescriber import transcriber
dispatcher = dispatcher.Dispatcher()
notetaker = notetaker.Notetaker()
picker = picker.Picker("/home/octopusx/Code/test",dispatcher)
transcriber = transcriber.Transcriber()
dispatcher.add_note_generator(notetaker)
dispatcher.add_audio_processor(transcriber)

View File

@ -1,26 +1,33 @@
import threading import threading
import os
class Dispatcher: class Dispatcher:
threads = [] threads = []
callbackExecutor = None audioProcessor = None
extensionFilter = None extensionFilter = None
noteGenerator = None
def __init__(self, callbackExecutor): def __init__(self):
self.callbackExecutor = callbackExecutor pass
def apply_filter(self, audio_file): def add_audio_processor(self, audioProcessor):
# TODO: return True if audio file is valid, based on name, extension, and other criteria self.audioProcessor = audioProcessor
if os.path.isdir(audio_file):
return False
return True
def execute(self, audio_file): def add_note_generator(self, noteGenerator):
if self.apply_filter(audio_file.pathname): self.noteGenerator = noteGenerator
self.threads.append(threading.Thread(target=self._execute_thread, args=(audio_file.pathname,)))
self.threads[-1].start() def process_audio_file(self, audio_file):
# TODO: we need to have a way to wipe old threads, otherwise we will have a memory leak 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 generate_note(self, note):
pass
def _execute_thread(self, audio_file): def _execute_thread(self, audio_file):
self.callbackExecutor.execute(audio_file) note = self.audioProcessor.execute(audio_file)
self.noteGenerator.generate_note(note)
def halt(self):
for thread in self.threads:
thread.join(10)

View File

@ -0,0 +1,7 @@
class Notetaker:
def __init__(self):
pass
def generate_note(self, note):
print("Received note: " + note)

View File

@ -1,23 +1,34 @@
import pyinotify import pyinotify
import os
class EventHandler(pyinotify.ProcessEvent): class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event): def process_IN_CREATE(self, event):
print(f'New file: {event.pathname}') print(f'New file: {event.pathname}')
return event.pathname return event.pathname
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
class Picker: class Picker:
directory = None directory = None
callbackObject = None dispatcher = None
notifier = None
def __init__(self, directory, callbackObject): def __init__(self, directory, dispatcher):
self.directory = directory self.directory = directory
self.callbackObject = callbackObject self.dispatcher = dispatcher
def observe(self): def observe(self):
wm = pyinotify.WatchManager() wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, EventHandler()) self.notifier = pyinotify.Notifier(wm, EventHandler())
wm.add_watch(self.directory, pyinotify.IN_CREATE, self.callbackObject.execute) wm.add_watch(self.directory, pyinotify.IN_CREATE, self.dispatcher.process_audio_file)
print(f'Watching {self.directory} for changes...') print(f'Watching {self.directory} for changes...')
notifier.loop() self.notifier.loop()
def halt(self):
self.notifier.stop()

View File

@ -1,2 +1,2 @@
whisper==1.1.10 openai-whisper==20231117
pyinotify==0.9.6 pyinotify==0.9.6

BIN
tests/fixtures/hello.wav vendored Normal file

Binary file not shown.