stream.py 538 B

123456789101112131415161718192021222324252627
  1. from abc import ABC, abstractmethod
  2. from typing import Union
  3. class StreamMixin:
  4. def __init__(self, generator):
  5. self.generator = generator
  6. self.closed = False
  7. def __iter__(self):
  8. return self
  9. def __next__(self):
  10. if self.closed:
  11. raise StopIteration
  12. else:
  13. return next(self.generator)
  14. class CancellableStream(StreamMixin, ABC):
  15. @abstractmethod
  16. def close(self):
  17. pass
  18. @abstractmethod
  19. def exit_code(self) -> Union[int, None]:
  20. pass