tubes icon indicating copy to clipboard operation
tubes copied to clipboard

Add IOFount

Open wsanchez opened this issue 8 years ago • 3 comments

Add an IOFount class that makes a fount from a readable FLO.

The code below works, but it's doing a blocking read on the data, and should be streaming instead.

@implementer(IFount)
@attrs(frozen=False)
class IOFount(object):
    """
    Fount that reads from a file-like-object.
    """

    outputType = ISegment

    _source = attrib()  # type: BinaryIO

    drain = attrib(
        validator=optional(provides(IDrain)), default=None, init=False
    )  # type: IDrain
    _paused = attrib(validator=instance_of(bool), default=False, init=False)


    def __attrs_post_init__(self):
        # type: () -> None
        self._pauser = Pauser(self._pause, self._resume)


    def _flowToDrain(self):
        # type: () -> None
        if self.drain is not None and not self._paused:
            data = self._source.read()
            if data:
                self.drain.receive(data)
            self.drain.flowStopped(Failure(StopIteration()))


    # FIXME: this should stream.
    def flowTo(self, drain):
        # type: (IDrain) -> IFount
        result = beginFlowingTo(self, drain)
        self._flowToDrain()
        return result


    def pauseFlow(self):
        # type: () -> None
        return self._pauser.pause()


    def stopFlow(self):
        # type: () -> None
        return self._pauser.resume()


    def _pause(self):
        # type: () -> None
        self._paused = True


    def _resume(self):
        # type: () -> None
        self._paused = False
        self._flowToDrain()

wsanchez avatar May 31 '17 17:05 wsanchez

Wouldn't this be better named "FileFount"?

glyph avatar Jun 01 '17 05:06 glyph

@glyph: perhaps. I was borrowing the BinaryIO and TextIO stdlib conventions for "I/O streams" which include but aren't limited to files…

wsanchez avatar Jul 11 '17 03:07 wsanchez

Hm; that works I guess.

glyph avatar Jul 13 '17 08:07 glyph