Module osyspyrt.io

Provide some I/O related helper classes.

Classes

class BytesIOReverse (size=5096)

An in-memory writable buffer that writes in reverse.

In a normal buffer, each write operation writes new data after previously written data. For this class, however, each write operation writes new data before previously written data. It is possible to do limited forward writing by calling do_forward.

Parameters

size : int
Initial size of the buffer.

Methods

write(b) Write the given bytes getbuffer() Get a view of the buffer. getvalue() Get the written bytes

Methods

def close(self)

Close this buffer. Discard content. Do not use this buffer anymore.

def do_forward(self, nbytes)

Switch to foward writing mode for the given number of bytes.

Calling this function creates a forward buffer of nbytes, located before previously written data. Subsequent calls to write will fill in this forward buffer in the typical forward-writing way. After calling this, you MUST subsequently write EXACTLY nbytes, after which the object automatically returns to writing in reverse.

def getbuffer(self)

Return a readable and writable view over the contents of the buffer without copying them. Mutating the view will transparently update the contents of the buffer. As long as the returned view exists, this buffer cannot be resized or closed.

This should not be called when in forward mode and the buffer as whole contains undefined data.

Returns

memoryview
View into the buffer (the written portion thereof).
def getvalue(self)

Return a copy of the buffer data.

This should not be called when in forward mode and the buffer as whole contains undefined data.

Returns

bytearray
The actual bytes written.
def write(self, b)

Write the given bytes, b.

Bytes are normally written before previously written data, but this can be changed by calling do_forward, which see.

Parameters

b : bytes or bytesarray
The bytes to write.

Returns

int
The number of bytes written. If an exception is not raised, it is always len(b).

Raises

BufferError
If in forward mode and b is too large to fit in the forward buffer.
class TextWriter (*, filename=None, writer=None, whitespace=True, **kwargs)

This wraps an io.TextIOBase, adding indentation and whitespace functionality.

A TextWriter is a context manager, so it can be used in a with statement. When this object closes, it will close the underlying TextIOBase.

Parameters
----------
filename : str
    The name of a text file to write to.

writer : io.TextIOBase
    The underlying writer this class shall write to. This takes
    precedence over the filename parameter.  If this is not provided,
    filename must be provided.

whitespace : bool

Attributes
----------
whitespace : bool
    If true, write_indent writes whitespace.  If false, it does not.

Subclasses

Class variables

var INDENT

Instance variables

var level
var whitespace

Methods

def close(self)

Close the underlying writer.

def decr_level(self)

Decrease indentation level. This must not be called more times than incr_level

def encoding(self)
def errors(self)
def flush(self)

Flush the underlying writer.

def incr_level(self)

Increase indentation level.

def write(self, s)

Write the given string.

def write_indent(self)

If whitespace is on, write a new-line followed by whitespace corresponding to the current indentation level.