2021-01-02 02:12:21 +00:00
|
|
|
from pycsdr.modules import Buffer
|
2020-12-25 19:27:30 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-07-16 14:12:16 +00:00
|
|
|
class Chain:
|
2020-12-16 17:52:00 +00:00
|
|
|
def __init__(self, *workers):
|
2020-12-25 19:27:30 +00:00
|
|
|
self.input = None
|
|
|
|
self.output = None
|
2020-12-16 17:52:00 +00:00
|
|
|
self.workers = workers
|
2020-12-25 19:27:30 +00:00
|
|
|
for i in range(1, len(self.workers)):
|
|
|
|
self._connect(self.workers[i - 1], self.workers[i])
|
|
|
|
|
|
|
|
def _connect(self, w1, w2):
|
2021-07-19 17:04:14 +00:00
|
|
|
if isinstance(w1, Chain):
|
|
|
|
buffer = w1.getOutput()
|
|
|
|
else:
|
|
|
|
buffer = Buffer(w1.getOutputFormat())
|
|
|
|
w1.setOutput(buffer)
|
2020-12-25 19:27:30 +00:00
|
|
|
w2.setInput(buffer)
|
2020-12-16 17:52:00 +00:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
for w in self.workers:
|
|
|
|
w.stop()
|
2021-07-18 12:56:48 +00:00
|
|
|
self.setInput(None)
|
2021-07-19 17:04:14 +00:00
|
|
|
if self.output is not None:
|
|
|
|
self.output.stop()
|
2020-12-16 17:52:00 +00:00
|
|
|
|
2020-12-19 15:28:18 +00:00
|
|
|
def setInput(self, buffer):
|
2020-12-25 19:27:30 +00:00
|
|
|
if self.input == buffer:
|
|
|
|
return
|
|
|
|
self.input = buffer
|
2021-07-19 17:04:14 +00:00
|
|
|
if self.workers:
|
|
|
|
self.workers[0].setInput(buffer)
|
|
|
|
else:
|
|
|
|
self.output = self.input
|
2020-12-19 15:28:18 +00:00
|
|
|
|
2021-07-19 17:04:14 +00:00
|
|
|
def getOutput(self):
|
|
|
|
if self.output is None:
|
|
|
|
if self.workers:
|
|
|
|
lastWorker = self.workers[-1]
|
|
|
|
if isinstance(lastWorker, Chain):
|
|
|
|
self.output = lastWorker.getOutput()
|
|
|
|
else:
|
|
|
|
self.output = Buffer(self.getOutputFormat())
|
|
|
|
self.workers[-1].setOutput(self.output)
|
|
|
|
else:
|
|
|
|
self.output = self.input
|
|
|
|
return self.output
|
2021-01-23 18:27:01 +00:00
|
|
|
|
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:
|
|
|
|
return self.input.getOutputFormat()
|
2021-07-16 14:12:16 +00:00
|
|
|
|
2021-01-23 18:27:01 +00:00
|
|
|
def pump(self, write):
|
2021-07-19 17:04:14 +00:00
|
|
|
output = self.getOutput()
|
2021-01-23 18:27:01 +00:00
|
|
|
|
|
|
|
def copy():
|
|
|
|
run = True
|
|
|
|
while run:
|
|
|
|
data = None
|
|
|
|
try:
|
2021-07-19 17:04:14 +00:00
|
|
|
data = output.read()
|
2021-01-23 18:27:01 +00:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
if data is None or (isinstance(data, bytes) and len(data) == 0):
|
|
|
|
run = False
|
|
|
|
else:
|
|
|
|
write(data)
|
|
|
|
|
|
|
|
return copy
|
|
|
|
|