Utilities

class batou.utils.Address(connect_address, port=None, require_v4=<object object>, require_v6=<object object>)

An internet service address that can be listened and connected to.

The constructor address is expected to be the address that can be connected to. The listen address will be computed automatically.

>>> x = Address('localhost', 80)
>>> str(x.connect)
'localhost:80'
>>> str(x.listen)
'127.0.0.1:80'

You can specify which IP versions are expected to be resolved for listen addresses in three ways with the require_v4/require_v6 flags:

  • False -> this version must not be used

  • True -> this version must be resolved properly

  • ‘optional’ -> the listen/listen_v6 attribute will contain None if it does

    not resolve.

connect: batou.utils.NetLoc

The connect address as it should be used when configuring clients. This is a batou.utils.NetLoc object.

property listen

The IPv4 listen (or bind) address as it should be used when configuring servers. This is a batou.utils.NetLoc object. It raises an batou.IPAddressConfigurationError if used unconfigured.

property listen_v6

The IPv6 listen (or bind) address as it should be used when configuring servers. This is a batou.utils.NetLoc object. It raises an batou.IPAddressConfigurationError if used unconfigured.

class batou.utils.BagOfAttributes

Provide a dict-like object that can also be accessed using attributes.

It’s sometimes more convenient to write a.x instead of a[‘x’]. However, namespaces may require being able to also use non-Python-identifier keys.

exception batou.utils.CmdExecutionError(cmd, returncode, stdout, stderr)
exception batou.utils.CycleError
class batou.utils.NetLoc(host, port=None)

A network location specified by host and port.

Network locations can automatically render an appropriate string representation:

>>> x = NetLoc('127.0.0.1')
>>> x.host
'127.0.0.1'
>>> x.port
None
>>> str(x)
'127.0.0.1'

>>> y = NetLoc('127.0.0.1', 80)
>>> str(y)
'127.0.0.1:80'
host: str

The host part of this network location. Can be a hostname or IP address.

port = None

The port of this network location. Can be None or an integer.

batou.utils.call_with_optional_args(func, **kw)

Provide a way to perform backwards-compatible call, passing only arguments that the function actually expects.

batou.utils.dict_merge(a, b)

recursively merges dict’s. not just simple a[‘key’] = b[‘key’], if both a and b have a key who’s value is a dict then dict_merge is called on both values and the result stored in the returned dictionary. https://www.xormedia.com/recursively-merge-dictionaries-in-python/

batou.utils.export_environment_variables(environ)

Turn this dict into shell environment variable exports.

This creates a snippet that can be embedded into a shell script to access all entries of this dict as environment variables.

Ensures proper quoting.

batou.utils.format_duration(duration: Optional[float]) str

Formats a duration (in seconds) into a human readable string.

Examples: ` format_duration(1.23124) == "1.23s" format_duration(61) == "1m1s" format_duration(None) == "NaN" `