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:
-
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:
-
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
-