Next | Previous

io

This module provides interfaces for working with to I/O stream and TTYs as well as expose the operating system standard I/O for easy access.

Some I/O operations that should belong to this module have been merged as core features and offered as built-in functions for Blade. Specifically file I/O features that can be accessed via the built-in file() function.

The standard I/O streams are also files and you can call almost all file methods on them. Whenever a file method is not supported, you'll get an error message telling you that such operation is not supported for standard streams.

Example

The following example shows how to use the io module for accepting user name and printing the result.

import io

var name = io.readline('What is your name?')
echo name

Fields

io.SEEK_SETint
Set I/O position from the beginning.
io.SEEK_CURint
Set I/O position from the current position.
io.SEEK_ENDint
Set I/O position from the end.
io.stdinfile
Stdin is a file handle to the standard input file of the system.
io.stdoutfile
Stdout is a file handle to the standard output file of the system.
io.stderrfile
Stderr is a file handle to the standard error file of the system.

Functions

io.flush(file)

Flushes the content of the given file handle

io.putc(c)

Writes character c to the screen.

  • @params:
    • char|number c
io.getc()

Reads character(s) from standard input When length is given, gets length number of characters else, gets a single character

  • @returns: char|string
io.getch()

Reads character(s) from standard input without printing to standard output When length is given, gets length number of characters else, gets a single character.

  • @returns: char|string
io.readline(message, secure, obscure_text)

Reads an entire line from standard input. If a message is given, the message will be printed before it begins to wait for a user input. If secure is true, the user's input will not be printing and obscure_text will be printed instead.

@notes:

  • Newlines will not be added automatically for messages.
  • @params:
    • string? message
    • bool? secure
    • string? obscure_text Default value is *.
  • @returns: string

Classes

class TTY

class TTY is an interface to TTY terminals this class contains definitions to control TTY terminals

static .TTY_IFLAGint

TTY attribute for input flags.

static .TTY_OFLAGint

TTY attribute for output flags.

static .TTY_CFLAGint

TTY attribute for control flags.

static .TTY_LFLAGint

TTY attribute for local flags.

static .TTY_ISPEEDint

TTY attribute for input speed.

static .TTY_OSPEEDint

TTY attribute for output speed.

static .IGNBRKint

Ignore BREAK condition.

static .BRKINTint

Map BREAK to SIGINTR.

static .IGNPARint

Ignore (discard) parity errors.

static .PARMRKint

Mark parity and framing errors.

static .INPCKint

Enable checking of parity errors.

static .ISTRIPint

Strip 8th bit off chars.

static .INLCRint

Map NL into CR.

static .IGNCRint

Ignore CR.

static .ICRNLint

Map CR to NL (ala CRMOD).

static .IXONint

Enable output flow control.

static .IXOFFint

Enable input flow control.

static .IXANYint

Any char will restart after stop.

static .IUTF8int

Maintain state for UTF-8 VERASE.

static .OPOSTint

Enable following output processing.

static .ONLCRint

Map NL to CR-NL (ala CRMOD).

static .CSIZEint

Character size mask .

static .CS5int

5 bits (pseudo).

static .CS6int

6 bits.

static .CS7int

7 bits.

static .CS8int

8 bits.

static .CSTOPBint

Send 2 stop bits.

static .CREADint

Enable receiver.

static .PARENBint

Parity enable.

static .PARODDint

Odd parity, else even.

static .HUPCLint

Hang up on last close.

static .CLOCALint

Ignore modem status lines.

static .ECHOEint

Visually erase chars.

static .ECHOK

Echo NL after line kill

static .ECHOint

Enable echoing.

static .ECHONLint

Echo NL even if ECHO is off.

static .ISIGint

Enable signals INTR, QUIT, [D]SUSP.

static .ICANONint

Canonicalize input lines.

static .IEXTENint

Enable DISCARD and LNEXT.

static .TOSTOPint

Stop background jobs from output.

static .NOFLSHint

Don't flush after interrupt.

static .TCSANOWint

Make change immediate.

static .TCSADRAINint

Drain output, then change.

static .TCSAFLUSHint

Drain output, flush input.

static .VEOFint

ICANON.

static .VEOLint

ICANON.

static .VERASEint

ICANON.

static .VKILLint

ICANON.

static .VINTRint

ISIG.

static .VQUITint

ISIG.

static .VSUSPint

ISIG.

static .VSTARTint

IXON, IXOFF.

static .VSTOPint

IXON, IXOFF.

static .VMINint

!ICANON.

static .VTIMEint

!ICANON.

.TTY(std) ➝ Constructor

TTY(std: file)

@notes:

  • file must be one of stdout and stderr
  • @params:
    • file std
.get_attr()

Returns the attribute of the current tty session The returned attributes is a dict containing the TTY_ flags

  • @returns: dict
.set_attr(option, attrs)

sets the attributes of the current tty session

NOTE:

  • option must be one ot the TCSA options above (see their description above)
  • attrs must be a dictionary of the TTY_ flags listed above
  • one can safely omit any of the TTY_ flags listed above and Blade will fill in the default values as it exists.
  • This flags will be merged and not overwritten
  • @params:
    • number option
    • dict attr
  • @returns: bool
.set_raw()

Sets the current tty to raw mode.

  • @returns: bool
.exit_raw()

Disables the raw mode flags on the current tty.

  • @returns: bool
.get_size()

Returns the size of the current TTY device as a dictionary of cols and rows.

  • cols: The number of text columns that can fit into the TTY device.

  • rows: The number of text rows that can fit into the TTY device.

  • @returns: dict

.flush()

Flushes the standard output and standard error interface

class BytesIO

The BytesIO class implements a bytearray based I/O system that allows you use treat bytearray (bytes) as if they were a file.

The class implements the essentials of a file except those that ties it to the operating system filesystem such as symbolic links, chmod and set time.

See the tutorial on working with files for more information.

.BytesIO(source, mode) ➝ Constructor

Returns a new instance of BytesIO

  • @params:
    • bytes source
    • string mode The I/O open mode - Default is r
.exists()

Returns true as BytesIO always exist.

  • @returns: bool
.close()

Closes the stream to an opened BytesIO. You'll rarely ever need to call this method yourself in most use cases.

.open()

Opens the stream to a BytesIO for the operation originally specified on the BytesIO object during creation.

You may need to call this method after a call to read() if the length isn't specified or write() if you wish to read or write again as the BytesIO will already be closed.

.read(length)

Reads the content of an opened BytesIO up to the specified length and returns it as string or bytes if the BytesIO was opened in the binary mode. If the length is not specified, the BytesIO will be read to the end.

This method requires that the BytesIO be opened in the read mode (default mode) or a mode that supports reading. If you aren't reading the full length of the BytesIO, you'll need to call the close() method to free the BytesIO for further reading, otherwise, the close() method will be automatically called for you.

@throws Exception

  • @params:
    • number length Default = -1
  • @returns: bytes
.gets(length)

Same as read(), but doesn't open or close the BytesIO automatically.

@throws Exception

  • @params:
    • number length Default = -1
  • @returns: bytes
.write(data)

Writes a string or bytes to an opened BytesIO at the current insertion point. When the BytesIO is opened with the a mode enabled, write will always start from the end of the BytesIO.

If the seek() method has been previously called, write will begin from the seeked position, otherwise it will start at the beginning of the BytesIO.

  • @params:
    • bytes|string
  • @returns: number
.puts(data)

Same as write(), but doesn't open or close the BytesIO automatically.

  • @params:
    • bytes|string
  • @returns: number
.number()

Returns the integer file descriptor number that is used by the underlying implementation to request I/O operations from the operating system. This can be very useful for low-level interfaces that uses or act as BytesIO descriptors.

  • @returns: number
.is_tty()

Always returns false as a BytesIO is not a TTY device.

  • @returns: bool
.is_open()

Returns true if the BytesIO is open for reading or writing and false otherwise.

  • @returns: bool
.is_closed()

Returns true if the BytesIO is closed for reading or writing and false otherwise.

.flush()

Does nothing for a BytesIO

Does nothing for BytesIO but simply returns false because BytesIO cannot be symbolically linked.

  • @returns: bool
.stats()

Returns the statistics or details of the BytesIO.

See the working with files documentation for more information about the stats() method.

  • @returns: dict
.delete()

Clears the bytearray and closes it for reading or writing.

Any further attempt to perform most operations on the BytesIO after calling delete() will raise an exception.

  • @returns: bool
.rename(new_name)

Returns false because BytesIO cannot be renamed.

  • @returns: bool
.copy()

Returns a new BytesIO with the source cloned and opened with the same mode as the current BytesIO.

.path()

Returns an empty string because BytesIO do not have any physical path.

  • @returns: string
.abs_path()

Same as BytesIO.path().

  • @returns: string
.truncate(length)

Truncates the entire BytesIO if length is not given or truncates the BytesIO such that only length number of bytes is left in it.

  • @returns: bool
.chmod(number)

Returns false because BytesIO do not have a permission scheme.

  • @returns: bool
.set_times(atime, mtime)

Sets the last access time and last modified time of the BytesIO.

  • @returns: bool
.seek(position, seek_type)

Sets the position of a BytesIO reader or writer in a BytesIO.

The position must be within the range of the BytesIO size. The seek_type argument must be on of SEEK_SET, SEEK_CUR or SEEK_END.

  • @returns: bool
.tell()

Returns the current position of the reader/writer in the BytesIO.

  • @returns: number
.mode()

Returns the mode in which the current BytesIO was opened.

  • @returns: string
.name()

Returns an empty string since BytesIO do not have a name.

  • @returns: string