2021-09-01 13:08:28 +00:00
|
|
|
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
|
2020-12-25 19:27:30 +00:00
|
|
|
|
|
|
|
|
2021-09-01 13:08:28 +00:00
|
|
|
class Chain(Module):
|
2021-08-23 12:25:28 +00:00
|
|
|
def __init__(self, workers):
|
2021-09-01 13:08:28 +00:00
|
|
|
super().__init__()
|
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])
|
2020-12-25 19:27:30 +00:00
|
|
|
|
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:
|
2020-12-25 19:27:30 +00:00
|
|
|
return
|
2021-09-01 13:08:28 +00:00
|
|
|
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)
|
2020-12-19 15:28:18 +00:00
|
|
|
|
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
|
2021-09-01 13:08:28 +00:00
|
|
|
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-01-23 18:27:01 +00:00
|
|
|
|
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
|
|
|
|
2021-07-19 22:44:41 +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
|
|
|
|
|
2021-08-26 13:58:02 +00:00
|
|
|
error = None
|
|
|
|
|
2021-07-19 22:44:41 +00:00
|
|
|
if index == 0:
|
2021-08-16 14:41:18 +00:00
|
|
|
if self.reader is not None:
|
|
|
|
newWorker.setReader(self.reader)
|
2021-07-19 22:44:41 +00:00
|
|
|
else:
|
2021-08-26 13:58:02 +00:00
|
|
|
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-07-19 22:44:41 +00:00
|
|
|
|
2021-08-16 14:41:18 +00:00
|
|
|
if index == len(self.workers) - 1:
|
|
|
|
if self.writer is not None:
|
|
|
|
newWorker.setWriter(self.writer)
|
2021-07-19 22:44:41 +00:00
|
|
|
else:
|
2021-08-26 13:58:02 +00:00
|
|
|
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:
|
2021-09-28 15:46:18 +00:00
|
|
|
if self.reader is not None and len(self.workers):
|
2021-08-23 12:25:28 +00:00
|
|
|
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()
|
|
|
|
|
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")
|