Source code for cli.streams
"""Abstractions of command input and output.
Every command accepts some input and results
in some output. This module contains abstractions
on this ideas.
"""
from io import StringIO
import os
class _BaseStream:
"""A common implementation detail for Input- and Output-Stream.
Both streams are implemented as a wrapper above io.StringIO.
So, technically, they are reading and writing from a in-memory string.
"""
def __init__(self):
"""Construct a Stream (both Input and Output)"""
self._io_obj = StringIO()
[docs]class OutputStream(_BaseStream):
"""An abstraction of command's output.
A command can write into OutputStream.
"""
[docs] def write(self, string):
"""Write a string to output stream"""
self._io_obj.write(string)
[docs] def write_line(self, string):
"""Write a newline-trailed string to output stream"""
self.write(string)
self.write(os.linesep)