aiomas.util

This module contains some utility functions.

aiomas.util.arrow_serializer()[source]

Return a serializer for arrow dates.

The return value is an argument tuple for aiomas.codecs.Codec.add_serializer().

aiomas.util.create_task(coro_or_future, *, ignore_cancel=True, loop=None)[source]

Run asyncio.ensure_future() with coro_or_future and set a callback that instantly raises all exceptions.

If the argument is a coroutine, a asyncio.Task object is returned. If the argument is a Future, it is returned directly.

If ignore_cancel is left True, no exception is raised if the task was canceled. If you also want to raise the CancelledError, set the flag to False..

The difference between this function and asyncio.ensure_future() is the behavior when an exception occurs within the background task:

Exceptions that occur within the background task are normally only raised when you await that task. If you start a background task that runs “forever”, you will only see the exception when your program ends and you either await the task or if the task object gets garbage collected (in which case the exception is just printed to stderr).

That means that your program can crash and you won’t notice it because no exception is actually raised or printed. To make development and debugging easier, this function adds a callback to the background task that will re-raise all exceptions immediately.

aiomas.util.async(coro_or_future, ignore_cancel=True, loop=None)[source]

Deprecated alias to create_task().

aiomas.util.run(until=None)[source]

Run the event loop forever or until the task/future until is finished.

This is an alias to asyncio’s run_forever() if until is None and to run_until_complete() if not.

aiomas.util.make_ssl_server_context(cafile, certfile, keyfile)[source]

Return an ssl.SSLContext that can be used by a server socket.

The server will use the certificate in certfile and private key in keyfile (both in PEM format) to authenticate itself.

It requires clients to also authenticate themselves. Their certificates will be validated with the root CA certificate in cafile.

It will use TLS 1.2 with ECDH+AESGCM encryption. ECDH keys won’t be reused in distinct SSL sessions. Compression is disabled.

aiomas.util.make_ssl_client_context(cafile, certfile, keyfile)[source]

Return an ssl.SSLContext that can be used by a client socket.

It uses the root CA certificate in cafile to validate the server’s certificate. It will also check the server’s hostname.

The client will use the certificate in certfile and private key in keyfile (both in PEM format) to authenticate itself.

It will use TLS 1.2 with ECDH+AESGCM encryption.

aiomas.util.obj_from_str(obj_path)[source]

Return the object that the string obj_path points to.

The format of obj_path is mod:obj where mod is a (possibly nested) module name and obj is an . separate object path, for example:

module:Class
module:Class.function
package.module:Class
package.module:Class.function

Raise a ValueError if the obj_path is malformed, an ImportError if the module cannot be imported or an AttributeError if an object does not exist.