create a base class for python-implemented modules

This commit is contained in:
Jakob Ketterl 2021-09-01 15:08:28 +02:00
parent 51453662e2
commit 01260d66c8
3 changed files with 22 additions and 8 deletions

View File

@ -1,11 +1,11 @@
from csdr.module import Module
from pycsdr.modules import Buffer from pycsdr.modules import Buffer
from typing import Union, Callable from typing import Union, Callable
class Chain: class Chain(Module):
def __init__(self, workers): def __init__(self, workers):
self.reader = None super().__init__()
self.writer = None
self.clientReader = None self.clientReader = None
self.workers = workers self.workers = workers
for i in range(1, len(self.workers)): for i in range(1, len(self.workers)):
@ -23,14 +23,14 @@ class Chain:
def setReader(self, reader): def setReader(self, reader):
if self.reader is reader: if self.reader is reader:
return return
self.reader = reader super().setReader(reader)
if self.workers: if self.workers:
self.workers[0].setReader(reader) self.workers[0].setReader(reader)
def setWriter(self, writer): def setWriter(self, writer):
if self.writer is writer: if self.writer is writer:
return return
self.writer = writer super().setWriter(writer)
if self.workers: if self.workers:
self.workers[-1].setWriter(writer) self.workers[-1].setWriter(writer)

14
csdr/module.py Normal file
View File

@ -0,0 +1,14 @@
import pycsdr.modules
class Module(pycsdr.modules.Module):
def __init__(self):
self.reader = None
self.writer = None
super().__init__()
def setReader(self, reader: pycsdr.modules.Reader) -> None:
self.reader = reader
def setWriter(self, writer: pycsdr.modules.Writer) -> None:
self.writer = writer

View File

@ -4,7 +4,7 @@ import threading
from owrx.audio import ProfileSourceSubscriber from owrx.audio import ProfileSourceSubscriber
from owrx.audio.wav import AudioWriter from owrx.audio.wav import AudioWriter
from owrx.audio.queue import QueueJob from owrx.audio.queue import QueueJob
from csdr.chain import Chain from csdr.module import Module
import pickle import pickle
import logging import logging
@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
class AudioChopper(threading.Thread, Chain, ProfileSourceSubscriber): class AudioChopper(threading.Thread, Module, ProfileSourceSubscriber):
# TODO parser typing # TODO parser typing
def __init__(self, mode_str: str, parser): def __init__(self, mode_str: str, parser):
self.parser = parser self.parser = parser
@ -25,7 +25,7 @@ class AudioChopper(threading.Thread, Chain, ProfileSourceSubscriber):
raise ValueError("Mode {} is not an audio chopper mode".format(mode_str)) raise ValueError("Mode {} is not an audio chopper mode".format(mode_str))
self.profile_source = mode.get_profile_source() self.profile_source = mode.get_profile_source()
super().__init__() super().__init__()
Chain.__init__(self, []) Module.__init__(self)
def stop_writers(self): def stop_writers(self):
while self.writers: while self.writers: