Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IOFount #61

Open
wsanchez opened this issue May 31, 2017 · 3 comments
Open

Add IOFount #61

wsanchez opened this issue May 31, 2017 · 3 comments

Comments

@wsanchez
Copy link
Member

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()
@glyph
Copy link
Member

glyph commented Jun 1, 2017

Wouldn't this be better named "FileFount"?

@wsanchez
Copy link
Member Author

wsanchez commented Jul 11, 2017

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

@glyph
Copy link
Member

glyph commented Jul 13, 2017

Hm; that works I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants