Skip to content

aachurin/promisedio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DESPITE THE FACT THAT THIS CODE IS DISTRIBUTED UNDER THE MIT LICENSE, IT IS PROHIBITED TO USE, COPY, MODIFY, MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THE SOFTWARE FOR ANY COMMERCIAL OR NON-COMMERCIAL PURPOSES BY JET BRAINS AND ANY OF ITS SUBSIDIARIES, PARENT ORGANIZATION OR AFFILIATES.

PromisedIO

PromisedIO

PromisedIO is free and open source software released under the permissive MIT license.

promisedio.promise (Promises)

You can read about promises here.

deferred() -> Deferred

Create new Deferred object.

Deferred.promise() -> Promise

Get related Promise object.

Deferred.resolve(value: object) -> None

Resolve related Promise object with the given value.

Deferred.reject(exc: Exception) -> None

Reject related Promise object with the given exception exc.

Promise.then(fulfilled: Callable[[object], object], rejected: Callable[[Exception], object]) -> Promise

Create new Promise.

It takes up to two arguments: callback functions for the success and failure cases of the Promise.

Promise.catch(rejected: Callable[[Exception], object]) -> Promise

Create new Promise.

It is the same as .then(None, rejected)

exec_async(coro: Coroutine) -> None

Start coroutine.

Example:
...

def my_sleep(timeout):
    d = promise.deferred()
    timer.set_timeout(lambda: d.resolve("My rules"), timeout)
    return d.promise()


async def main():
    print(await my_sleep(5.))

    
promise.exec_async(main())
loop.run_forever()

promisedio.timer (Timers)

sleep(seconds: float) -> Promise[None]

Delay execution for a given number of seconds.

set_timeout(func: Callable[[], None], timeout: float, *, unref: bool = False) -> Timer

Set timer which executes a function once the timer expires.

Return Timer object. This value can be passed to clear_timeout() to cancel the timeout.

set_interval(func: Callable[[], None], timeout: float, *, unref: bool = False) -> Timer

Set timer which executes repeatedly a function, with a fixed time delay between each call.

Return Timer object. This value can be passed to clear_interval() to cancel the interval.

clear_timeout(ob: Timer) -> None

Cancel timeout previously established by calling set_timeout().

clear_interval(ob: Timer) -> None

Cancel interval previously established by calling set_interval().

time() -> int

Get current timestamp in milliseconds.

The timestamp is cached at the start of the event loop tick.

hrtime() -> int

Return current high-resolution real time.

This is expressed in nanoseconds.

promisedio.fs (File system operations)

close(fd: int) -> Promise[None]

Close file descriptor fd.

Equivalent to close(2).

fstat(fd: int) -> Promise[StatObj]

Get the status of the file descriptor fd.

Return StatObj object.

Equivalent to fstat(2).

openfd(path: Union[Path, str, bytes], flags: int, mode: int = 0o666) -> Promise[int]

Open the file path and set various flags according to flags and possibly its mode according to mode.

Return the file descriptor for the newly opened file. The new file descriptor is non-inheritable.

Equivalent to open(2).

open(path: Union[Path, str, bytes, int], flags: str = "r", closefd: bool = True) -> Promise[FileIO]

Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.

Equivalent to python open (binary mode only).

read(fd: int, size: int = -1, offset: int = -1) -> Promise[bytes]

Read from file descriptor fd until we have size characters or we hit EOF.

If offset is present and is not negative, read at most size bytes from file descriptor fd at a position of offset, leaving the file offset unchanged.

Equivalent to read(2) preadv(2)

write(fd: int, data: bytes, offset: int = -1) -> Promise[int]

Write the data to file descriptor fd.

If offset is present and is not negative, write the data to file descriptor fd at position of offset, leaving the file offset unchanged.

Return the number of bytes actually written.

Equivalent to write(2) pwritev(2)

stat(path: Union[Path, str, bytes], *, follow_symlinks: bool = True) -> Promise[StatObj]

Get the status of a file. Return a StatObj object.

This function normally follows symlinks; to stat a symlink add the argument follow_symlinks=False

Equivalent to stat(2) lstat(2).

seek(fd: int, pos: int, how: int) -> Promise[int]

Set the current position of file descriptor fd to position pos, modified by how:

  • SEEK_SET or 0 to set the position relative to the beginning of the file;
  • SEEK_CUR or 1 to set it relative to the current position;
  • SEEK_END or 2 to set it relative to the end of the file.

Return the new cursor position in bytes, starting from the beginning.

Equivalent to lseek(2).

unlink(path: Union[Path, str, bytes]) -> Promise[None]

Remove (delete) the file path.

Equivalent to unlink(2).

mkdir(path: Union[Path, str, bytes], mode: int = 0o777) -> Promise[None]

Create a directory named path with numeric mode mode.

If the directory already exists, FileExistsError is raised.

On some systems, mode is ignored.

Equivalent to mkdir(2).

rmdir(path: Union[Path, str, bytes]) -> Promise[None]

Remove (delete) the directory path.

If the directory does not exist or is not empty, an FileNotFoundError or an OSError is raised respectively.

Equivalent to rmdir(2).

mkdtemp(tpl: Union[Path, str, bytes]) -> Promise[bytes]

Generate a uniquely named temporary directory from template tpl.

The last six characters of template must be XXXXXX and these are replaced with a string that makes the directory name unique.

Return the modified template string.

Equivalent to mkdtemp(3).

mkstemp(tpl: Union[Path, str, bytes]) -> Promise[Tuple[int, bytes]]

Generate a unique temporary filename from template.

This function creates and opens the file, and returns an open file descriptor for the file. The last six characters of template must be "XXXXXX" and these are replaced with a string that makes the filename unique.

Return the file descriptor and modified template string.

Equivalent to mkstemp(3).

scandir(path: Union[Path, str, bytes]) -> Promise[Tuple[Tuple[int, bytes], ...]]

Return a sequence of the entries in the directory given by path (entry_type, entry_name).

Special entries '.' and '..' are not included.

Equivalent to scandir(3).

rename(path: Union[Path, str, bytes], new_path: Union[Path, str, bytes]) -> Promise[None]

Rename the file or directory path to new_path.

Equivalent to rename(2).

fsync(fd: int) -> Promise[None]

Force write of file with file descriptor fd to disk.

Equivalent to fsync(2).

fdatasync(fd: int) -> Promise[None]

Force write of file with file descriptor fd to disk. Does not force update of metadata.

Equivalent to fdatasync(2).

ftruncate(fd: int, length: int) -> Promise[None]

Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size.

Equivalent to ftruncate(2).

copyfile(path: Union[Path, str, bytes], new_path: Union[Path, str, bytes], flags: int = 0) -> Promise[None]

Copy a file from path to new_path.

Supported flags are:

  • COPYFILE_EXCL
  • COPYFILE_FICLONE
  • COPYFILE_FICLONE_FORCE

For more information, see uv_fs_copyfile.

sendfile(out_fd: int, in_fd: int, in_offset: int, count: int) -> Promise[int]

Copy count bytes from file descriptor in_fd to file descriptor out_fd starting at offset.

Return the number of bytes sent.

Equivalent to sendfile(2).

access() -> Promise[bool]

Use the real uid/gid to test for access to path.

Mode should be F_OK to test the existence of path, or it can be the inclusive OR of one or more of R_OK, W_OK, and X_OK to test permissions.

Return True if access is allowed, False if not.

Equivalent to access(2).

chmod(path: Union[Path, str, bytes], mode: int) -> Promise[None]

Change the mode of path to the numeric mode.

See stat module for available mode.

Equivalent to chmod(2).

fchmod(fd: int, mode: int) -> Promise[None]

Change the mode of the file given by fd to the numeric mode.

See stat module for available mode.

Equivalent to fchmod(2).

utime(path: Union[Path, str, bytes], atime: float, mtime: float, *, follow_symlinks: bool = True) -> Promise[None]

Set the access and modified times of the file specified by path.

This function normally follows symlinks.

Equivalent to utime(2) lutimes(2).

futime(fd: int, atime: float, mtime: float) -> Promise[None]

Set the access and modified times of the file given by fd.

Equivalent to futimes(3)

link(path: Union[Path, str, bytes], new_path: Union[Path, str, bytes]) -> Promise[None]

Create a hard link pointing to path named new_path.

Equivalent to link(2)

symlink(path: Union[Path, str, bytes], new_path: Union[Path, str, bytes], flags: int = 0) -> Promise[None]

Create a symbolic link pointing to path named new_path.

On Windows the flags parameter can be specified to control how the symlink will be created:

  • SYMLINK_DIR: indicates that path points to a directory.
  • SYMLINK_JUNCTION: request that the symlink is created using junction points.

Equivalent to symlink(2)

readlink(path: Union[Path, str, bytes]) -> Promise[bytes]

Return a string representing the path to which the symbolic link points.

Equivalent to readlink(2)

chown(path: Union[Path, str, bytes], uid: int, gid: int, *, follow_symlinks: bool = True) -> Promise[None]

Change the owner and group id of path to the numeric uid and gid.

Equivalent to chown(2)

fchown(fd: int, uid: int, gid: int, *, follow_symlinks: bool = True) -> Promise[None]

Change the owner and group id of fd to the numeric uid and gid.

Equivalent to fchown(2)

promisedio.ns (Networking)

getaddrinfo(host: Union[str, bytes], port: Union[str, bytes, int], family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> Promise

Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service. host is a domain name, a string representation of an IPv4/v6 address or None. port is a string service name such as 'http', a numeric port number or None.

For more information, see getaddrinfo

getnameinfo(sockaddr: Tuple, flags: int) -> Promise[Tuple[str, str]]

Translate a socket address sockaddr into a 2-tuple (host, port).

Depending on the settings of flags, the result can contain a fully-qualified domain name or numeric address representation in host. Similarly, port can contain a string port name or a numeric port number.

For more information about flags you can consult getnameinfo(3)

open_connection(remote_addr: Tuple, *, local_addr: Tuple = None, limit: int = -1, chunk_size: int = -1, 
                nodelay: int = -1, keepalive: int = -1,         
                ssl: _ssl._SSLContext = None, server_hostname: str = None) -> Promise[TcpStream]:

Establish an IPv4 or IPv6 TCP connection.

limit: The internal buffer size limit used by the Stream instance. By default, the limit is set to 64 KiB. chunk_size: The minimum size of one chunk of the internal buffer. By default, is set to 496 bytes (on x64). nodelay: Enable/disable TCP_NODELAY. Set 1 to disable Nagle's algorithm. keepalive: Enable/disable TCP keep-alive. The value is the initial delay in seconds. Set 0 to disable TCP keep-alive.

TcpStream.set_tcp_nodelay

Enable/disable TCP_NODELAY, which disables Nagle's algorithm.

TcpStream.set_tcp_keepalive

Enable/disable TCP keep-alive. Set zero to disable.

TcpStream.getsockname() -> Tuple

Return the socket's own address (For TCP only).

TcpStream.getpeername() -> Tuple

Get the current address to which the handle is bound (For TCP only)

TcpStream.write(data: bytes) -> Promise[None]

Write data to the stream.

TcpStream.read(n: int = -1) -> Promise[bytes]

Read up to n bytes.

If n is not provided, or set to -1, reads as much bytes as possible. If EOF was received and the internal buffer is empty, return an empty bytes object.

TcpStream.readexactly(n: int) -> Promise[bytes]

Read exactly n bytes.

Raise an IncompleteReadError if EOF is reached before n can be read. Use the IncompleteReadError.partial attribute to get the partially read data.

Returned value is not limited by the configured stream limit.

TcpStream.readuntil(c: bytes) -> Promise[bytes]

Read data from the stream until c is found.

Raise an IncompleteReadError if EOF is reached before n can be found. Use the IncompleteReadError.partial attribute to get the partially read data.

If the amount of data read exceeds the configured stream limit, a LimitOverrunError exception is raised, and the data is left in the internal buffer and can be read again.

TcpStream.shutdown() -> Promise[None]

Shut down the stream.

TcpStream.close() -> Promise[None]

Close the stream handle.

promisedio.loop (Loop)

run_forever() -> None

Run loop.

About

Promise-based IO for Python.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published