openwebrx-clone/csdr/chain/__init__.py

148 lines
4.4 KiB
Python
Raw Normal View History

from csdr.module import Module
2021-08-23 12:25:28 +00:00
from pycsdr.modules import Buffer
2021-09-06 13:05:33 +00:00
from pycsdr.types import Format
2021-09-20 15:24:10 +00:00
from typing import Union, Callable, Optional
class Chain(Module):
2021-08-23 12:25:28 +00:00
def __init__(self, workers):
super().__init__()
2021-08-16 14:41:18 +00:00
self.clientReader = None
2021-08-23 12:25:28 +00:00
self.workers = workers
2021-08-16 14:41:18 +00:00
for i in range(1, len(self.workers)):
self._connect(self.workers[i - 1], self.workers[i])
2021-08-27 14:11:03 +00:00
def empty(self):
return not self.workers
2021-09-20 15:24:10 +00:00
def _connect(self, w1, w2, buffer: Optional[Buffer] = None) -> None:
2021-08-27 22:10:46 +00:00
if buffer is None:
buffer = Buffer(w1.getOutputFormat())
w1.setWriter(buffer)
w2.setReader(buffer.getReader())
2020-12-16 17:52:00 +00:00
2021-08-16 14:41:18 +00:00
def setReader(self, reader):
if self.reader is reader:
return
super().setReader(reader)
2021-07-19 17:04:14 +00:00
if self.workers:
2021-08-16 14:41:18 +00:00
self.workers[0].setReader(reader)
2021-08-16 14:41:18 +00:00
def setWriter(self, writer):
if self.writer is writer:
2021-07-24 16:50:30 +00:00
return
super().setWriter(writer)
2021-07-24 16:50:30 +00:00
if self.workers:
2021-08-16 14:41:18 +00:00
self.workers[-1].setWriter(writer)
2021-08-23 12:25:28 +00:00
def indexOf(self, search: Union[Callable, object]) -> int:
def searchFn(x):
if callable(search):
return search(x)
else:
return x is search
2021-08-23 12:25:28 +00:00
try:
return next(i for i, v in enumerate(self.workers) if searchFn(v))
except StopIteration:
return -1
2021-07-16 14:12:16 +00:00
def replace(self, index, newWorker):
if index >= len(self.workers):
raise IndexError("Index {} does not exist".format(index))
self.workers[index].stop()
self.workers[index] = newWorker
error = None
if index == 0:
2021-08-16 14:41:18 +00:00
if self.reader is not None:
newWorker.setReader(self.reader)
else:
try:
previousWorker = self.workers[index - 1]
self._connect(previousWorker, newWorker)
except ValueError as e:
# store error for later raising, but still attempt the second connection
error = e
2021-08-16 14:41:18 +00:00
if index == len(self.workers) - 1:
if self.writer is not None:
newWorker.setWriter(self.writer)
else:
try:
nextWorker = self.workers[index + 1]
self._connect(newWorker, nextWorker)
except ValueError as e:
error = e
if error is not None:
2021-08-27 14:11:03 +00:00
raise error
2021-08-23 12:25:28 +00:00
def append(self, newWorker):
previousWorker = None
if self.workers:
previousWorker = self.workers[-1]
self.workers.append(newWorker)
if previousWorker:
self._connect(previousWorker, newWorker)
elif self.reader is not None:
newWorker.setReader(self.reader)
if self.writer is not None:
newWorker.setWriter(self.writer)
2021-08-27 14:11:03 +00:00
def insert(self, newWorker):
nextWorker = None
if self.workers:
nextWorker = self.workers[0]
self.workers.insert(0, newWorker)
if nextWorker:
self._connect(newWorker, nextWorker)
elif self.writer is not None:
newWorker.setWriter(self.writer)
if self.reader is not None:
newWorker.setReader(self.reader)
2021-08-23 12:25:28 +00:00
def remove(self, index):
removedWorker = self.workers[index]
self.workers.remove(removedWorker)
removedWorker.stop()
if index == 0:
if self.reader is not None:
self.workers[0].setReader(self.reader)
elif index == len(self.workers):
if self.writer is not None:
self.workers[-1].setWriter(self.writer)
else:
previousWorker = self.workers[index - 1]
nextWorker = self.workers[index]
self._connect(previousWorker, nextWorker)
def stop(self):
for w in self.workers:
w.stop()
if self.clientReader is not None:
# TODO should be covered by finalize
self.clientReader.stop()
self.clientReader = None
2021-09-06 13:05:33 +00:00
def getInputFormat(self) -> Format:
if self.workers:
return self.workers[0].getInputFormat()
else:
raise BufferError("getInputFormat on empty chain")
def getOutputFormat(self) -> Format:
2021-08-23 12:25:28 +00:00
if self.workers:
return self.workers[-1].getOutputFormat()
else:
raise BufferError("getOutputFormat on empty chain")