OTR bot utility functions

A set of utility functions that are often used throughout the application.

otrbot.core.utils.fatal(code=exit_codes.UNKNOWN_EXCEPTION, last_error=None)

Log a fatal error an exit the application with an exit code.

Parameters:
  • code (int) –

    The exit code the application failed with.

    • exit_codes.INVALID_ARGUMENT
    • exit_codes.BAD_SETTING
    • exit_codes.NO_SETTINGS
    • exit_codes.MISSING_DEPDENDENCY
    • exit_codes.WRONG_TLS_CIPHER_NEGOTIATED = 127
    • exit_codes.UNKNOWN_EXCEPTION
  • last_error (str) – A fatal error message to logger.
otrbot.core.utils.dummy_i18n(msg)

Dummy i18n function, use when nothing is translated, only defined.

otrbot.core.utils.generate_secret(**kwargs)

Generate a cryptographically secure secret according to a format.

Supply either the format keyword or the length keyword, not both.

In the format string any occurrence of “X” will be replaced by a random character. You can put any separators characters you wish.

Parameters:
  • format (str) – Format string, each X will be replaced by a random character (“XXXX-XXXX-XXXX-XXXX”).
  • length (int) – Length of the random string.
  • alpha (bool) – Include alphabet in the output (True).
  • case (str) – Case of character options: upper, lower, mixed (upper)
  • numeric (bool) – Include numbers in the output (False).
  • symbols (bool) – Include common symbols in the output (False).
  • options (str) – A string containing all possible characters.
Returns str:

Secret according to format.

otrbot.core.utils.call_function(function, *args, **kwargs)

calls the function with the appropriate amount of arguments. This enables defining functions with or withouth *args and **kwargs that can both be called through this function.

Parameters:
  • function (function) – The function with an arbitrary amount of arguments.
  • args (tuple) – The unnamed arguments of the function (in order).
  • kwargs (dict) – The keyword arguments of the function.
class otrbot.core.utils.CacheReturn(func)

Cache return of a function/method for a specific set of arguments.

This class memorises all the arguments passed to it and all the return values so it can potentially have a huge memory footprint. Use with caution.

Initialise the CacheReturn class.

Parameters:func (function) – The function object, automatically passed by decorating a function with this class.
otrbot.core.utils.template_substitute(input_string, **kwargs)

shorthand for using string.Template’s safe_substitute

class otrbot.core.utils.SleekFilter(name='')

Filter annoyingly large sleek messages

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

AVATAR_MSGS = ['<TYPE>image/png</TYPE><BINVAL>', 'data xmlns="urn:xmpp:avatar:data"']

These strings are typical for messages about avatars:

TRUNCATE = 60

Truncate everything between the first and last TRUNCATE characters

filter(record)

Filter a record:

  • Truncate messages that contain avatar data