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
from notescriber.transcriber import Transcriber
from notescriber.dispatcher import Dispatcher
from notescriber.picker import Picker
# from notescriber.transcriber import Transcriber
# from notescriber.dispatcher import Dispatcher
# from notescriber.picker import Picker
from notescriber import picker, dispatcher
if __name__ == '__main__':
transcriber = Transcriber()
dispatcher = Dispatcher(transcriber)
picker = Picker("/home/octopusx/Code/test", dispatcher)
picker.observe()
# dispatcher.halt()
# picker.halt()

View File

@ -3,3 +3,11 @@ from notescriber import dispatcher
from notescriber import notetaker
from notescriber import picker
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 os
class Dispatcher:
threads = []
callbackExecutor = None
audioProcessor = None
extensionFilter = None
noteGenerator = None
def __init__(self, callbackExecutor):
self.callbackExecutor = callbackExecutor
def __init__(self):
pass
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 add_audio_processor(self, audioProcessor):
self.audioProcessor = audioProcessor
def execute(self, audio_file):
if self.apply_filter(audio_file.pathname):
def add_note_generator(self, noteGenerator):
self.noteGenerator = noteGenerator
def process_audio_file(self, audio_file):
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):
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 os
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
print(f'New file: {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:
directory = None
callbackObject = None
dispatcher = None
notifier = None
def __init__(self, directory, callbackObject):
def __init__(self, directory, dispatcher):
self.directory = directory
self.callbackObject = callbackObject
self.dispatcher = dispatcher
def observe(self):
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, EventHandler())
wm.add_watch(self.directory, pyinotify.IN_CREATE, self.callbackObject.execute)
self.notifier = pyinotify.Notifier(wm, EventHandler())
wm.add_watch(self.directory, pyinotify.IN_CREATE, self.dispatcher.process_audio_file)
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

BIN
tests/fixtures/hello.wav vendored Normal file

Binary file not shown.