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_SET ➝ int
- Set I/O position from the beginning.
- io.SEEK_CUR ➝ int
- Set I/O position from the current position.
- io.SEEK_END ➝ int
- Set I/O position from the end.
- io.stdin ➝ file
- Stdin is a file handle to the standard input file of the system.
- io.stdout ➝ file
- Stdout is a file handle to the standard output file of the system.
- io.stderr ➝ file
- 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
- @params:
- 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_IFLAG ➝ int
-
TTY attribute for input flags.
- static .TTY_OFLAG ➝ int
-
TTY attribute for output flags.
- static .TTY_CFLAG ➝ int
-
TTY attribute for control flags.
- static .TTY_LFLAG ➝ int
-
TTY attribute for local flags.
- static .TTY_ISPEED ➝ int
-
TTY attribute for input speed.
- static .TTY_OSPEED ➝ int
-
TTY attribute for output speed.
- static .IGNBRK ➝ int
-
Ignore BREAK condition.
- static .BRKINT ➝ int
-
Map BREAK to SIGINTR.
- static .IGNPAR ➝ int
-
Ignore (discard) parity errors.
- static .PARMRK ➝ int
-
Mark parity and framing errors.
- static .INPCK ➝ int
-
Enable checking of parity errors.
- static .ISTRIP ➝ int
-
Strip 8th bit off chars.
- static .INLCR ➝ int
-
Map NL into CR.
- static .IGNCR ➝ int
-
Ignore CR.
- static .ICRNL ➝ int
-
Map CR to NL (ala CRMOD).
- static .IXON ➝ int
-
Enable output flow control.
- static .IXOFF ➝ int
-
Enable input flow control.
- static .IXANY ➝ int
-
Any char will restart after stop.
- static .IUTF8 ➝ int
-
Maintain state for UTF-8 VERASE.
- static .OPOST ➝ int
-
Enable following output processing.
- static .ONLCR ➝ int
-
Map NL to CR-NL (ala CRMOD).
- static .CSIZE ➝ int
-
Character size mask .
- static .CS5 ➝ int
-
5 bits (pseudo).
- static .CS6 ➝ int
-
6 bits.
- static .CS7 ➝ int
-
7 bits.
- static .CS8 ➝ int
-
8 bits.
- static .CSTOPB ➝ int
-
Send 2 stop bits.
- static .CREAD ➝ int
-
Enable receiver.
- static .PARENB ➝ int
-
Parity enable.
- static .PARODD ➝ int
-
Odd parity, else even.
- static .HUPCL ➝ int
-
Hang up on last close.
- static .CLOCAL ➝ int
-
Ignore modem status lines.
- static .ECHOE ➝ int
-
Visually erase chars.
- static .ECHOK ➝
-
Echo NL after line kill
- static .ECHO ➝ int
-
Enable echoing.
- static .ECHONL ➝ int
-
Echo NL even if ECHO is off.
- static .ISIG ➝ int
-
Enable signals INTR, QUIT, [D]SUSP.
- static .ICANON ➝ int
-
Canonicalize input lines.
- static .IEXTEN ➝ int
-
Enable DISCARD and LNEXT.
- static .TOSTOP ➝ int
-
Stop background jobs from output.
- static .NOFLSH ➝ int
-
Don't flush after interrupt.
- static .TCSANOW ➝ int
-
Make change immediate.
- static .TCSADRAIN ➝ int
-
Drain output, then change.
- static .TCSAFLUSH ➝ int
-
Drain output, flush input.
- static .VEOF ➝ int
-
ICANON.
- static .VEOL ➝ int
-
ICANON.
- static .VERASE ➝ int
-
ICANON.
- static .VKILL ➝ int
-
ICANON.
- static .VINTR ➝ int
-
ISIG.
- static .VQUIT ➝ int
-
ISIG.
- static .VSUSP ➝ int
-
ISIG.
- static .VSTART ➝ int
-
IXON, IXOFF.
- static .VSTOP ➝ int
-
IXON, IXOFF.
- static .VMIN ➝ int
-
!ICANON.
- static .VTIME ➝ int
-
!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
- @params:
- .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 orwrite()
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
- @params:
- .gets(length)
-
Same as
read()
, but doesn't open or close the BytesIO automatically.@throws Exception
- @params:
- number length Default = -1
- @returns: bytes
- @params:
- .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
- @params:
- .puts(data)
-
Same as
write()
, but doesn't open or close the BytesIO automatically.- @params:
- bytes|string
- @returns: number
- @params:
- .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 andfalse
otherwise.- @returns: bool
- .is_closed()
-
Returns
true
if the BytesIO is closed for reading or writing andfalse
otherwise. - .flush()
-
Does nothing for a BytesIO
- .symlink(path)
-
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.
- @returns: 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