openwebrx-clone/csdr/chain/__init__.py

116 lines
3.4 KiB
Python
Raw Normal View History

2021-07-24 16:50:30 +00:00
from pycsdr.modules import Buffer, Writer
2021-07-16 14:12:16 +00:00
class Chain:
2020-12-16 17:52:00 +00:00
def __init__(self, *workers):
self.input = None
self.output = None
2021-07-24 16:50:30 +00:00
self.reader = None
self.workers = list(workers)
2021-07-24 16:50:30 +00:00
filtered = self._filterWorkers()
for i in range(1, len(filtered)):
self._connect(filtered[i - 1], filtered[i])
def _filterWorkers(self):
return [w for w in self.workers if not isinstance(w, Chain) or not w.empty()]
def empty(self):
return len(self.workers) <= 0
def _connect(self, w1, w2):
2021-07-24 16:50:30 +00:00
writer = Buffer(w1.getOutputFormat())
w1.setWriter(writer)
if isinstance(w2, Chain):
w2.setInput(writer)
2021-07-19 17:04:14 +00:00
else:
2021-07-24 16:50:30 +00:00
w2.setReader(writer.getReader())
2020-12-16 17:52:00 +00:00
def stop(self):
for w in self.workers:
w.stop()
2021-07-24 16:50:30 +00:00
if self.reader is not None:
self.reader.stop()
self.reader = None
2020-12-16 17:52:00 +00:00
2021-07-24 16:50:30 +00:00
def setInput(self, buffer: Buffer):
if self.input == buffer:
return
self.input = buffer
2021-07-19 17:04:14 +00:00
if self.workers:
2021-07-24 16:50:30 +00:00
firstWorker = self.workers[0]
if isinstance(firstWorker, Chain):
firstWorker.setInput(buffer)
else:
firstWorker.setReader(buffer.getReader())
2021-07-19 17:04:14 +00:00
else:
self.output = self.input
2021-07-24 16:50:30 +00:00
def setWriter(self, writer: Writer):
if self.output == writer:
return
self.output = writer
if self.workers:
lastWorker = self.workers[-1]
lastWorker.setWriter(self.output)
else:
raise BufferError("setOutput on empty chain")
2021-07-16 14:12:16 +00:00
def getOutputFormat(self):
2021-07-19 17:04:14 +00:00
if self.workers:
return self.workers[-1].getOutputFormat()
else:
2021-07-24 16:50:30 +00:00
raise BufferError("getOutputFormat on empty chain")
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
if index == 0:
2021-07-24 16:50:30 +00:00
writer = self.input
else:
previousWorker = self.workers[index - 1]
2021-07-24 16:50:30 +00:00
writer = Buffer(previousWorker.getOutputFormat())
previousWorker.setWriter(writer)
if writer is not None:
if isinstance(newWorker, Chain):
newWorker.setInput(writer)
else:
2021-07-24 16:50:30 +00:00
newWorker.setReader(writer.getReader())
if index < len(self.workers) - 1:
nextWorker = self.workers[index + 1]
2021-07-24 16:50:30 +00:00
writer = Buffer(newWorker.getOutputFormat())
newWorker.setWriter(writer)
if isinstance(nextWorker, Chain):
nextWorker.setInput(writer)
else:
2021-07-24 16:50:30 +00:00
nextWorker.setReader(writer.getReader())
else:
2021-07-24 16:50:30 +00:00
if self.output is not None:
newWorker.setWriter(self.output)
def pump(self, write):
2021-07-24 16:50:30 +00:00
if self.output is None:
self.setWriter(Buffer(self.getOutputFormat()))
self.reader = self.output.getReader()
def copy():
run = True
while run:
data = None
try:
2021-07-24 16:50:30 +00:00
data = self.reader.read()
except ValueError:
pass
2021-07-25 15:36:32 +00:00
if data is None:
run = False
else:
write(data)
return copy