bgpy.core package#

Submodules#

bgpy.core.environment module#

bgpy.core.log module#

class bgpy.core.log.Log(name, level: str = 'INFO', tag: Optional[str] = None, file: Optional[Path] = None, clear: bool = False)#

Bases: object

Logger class, optionally binds to a log file and writes logs with timestamp and PID.

clear_log_file() None#

Clears the log file.

critical(msg: str) None#

Log a message with level “CRITICAL”

Parameters

msg (str) – Message to log.

debug(msg: str) None#

Log a message with level “DEBUG”

Parameters

msg (str) – Message to log.

error(msg: str) None#

Log a message with level “ERROR”

Parameters

msg (str) – Message to log.

exception(msg: str) None#

Log an exception with message.

Parameters

msg (str) – Message to log.

file#
info(msg: str) None#

Log a message with level “INFO”

Parameters

msg (str) – Message to log.

level#
logger#
name#
tag#
warning(msg: str) None#

Log a message with level “WARNING”

Parameters

msg (str) – Message to log.

bgpy.core.message module#

class bgpy.core.message.Message(type_: MessageType, args: dict = {})#

Bases: object

Messages for the socket communciation.

Messages to be sent and reveiced via the client sockets. The message consists of a message type and arguments. The message type is fixed in the communication between the client sockets, and the arguments are used for confirmation, error messages and communicating response values.

args#
get_args() dict#

Get the argument dict of the message.

Returns

Arguments of the message.

Return type

dict

set_args(args: dict) None#

Set the argument dict of the message.

Parameters

args (dict) – Arguments to set on the message.

Return type

None

type#
class bgpy.core.message.MessageType(value)#

Bases: Enum

Message types for the messages in the communication.

The message types defined in this enum include:

  • INIT: Only sent once to initialize the background process.

  • EXIT: Command message to tell process to exit.

  • EXEC: Command message with function call and arguments to execute.

  • AUTH: Sent in the beginning of the communication to authenticate.

  • OK: Response message, optionally with return values.

  • ERROR: Error response message.

AUTH = 3#
ERROR = 5#
EXEC = 1#
EXIT = 2#
INIT = 0#
OK = 4#

bgpy.core.serialize module#

bgpy.core.serialize.deserialize(x: bytes) Message#

Deserializes bytes to a Python Message object.

Parameters

x (bytes) – Bytes containing a serialized Python Message object.

Returns

A python object.

Return type

object

bgpy.core.serialize.serialize(x: Message) bytes#

Serializes a Python Message object.

Parameters

x (object) – A Python Message object.

Returns

Bytes containing the serialized Python Message object.

Return type

bytes

bgpy.core.sockets module#

class bgpy.core.sockets.ClientSocket(sock: Optional[socket] = None, log_level: str = 'WARNING', log_file: Optional[Path] = None)#

Bases: ContextDecorator

Client socket

Stream socket used on the client and the server to communciate.

connect(host: str, port: int) None#

Connect to port on host.

Parameters
  • host (str, optional) – Address or name of the host

  • port (int, optional) – Port on the host to connect to

Raises

error – If the socket connection fails, the error is also written to the logs and raised.

log#
recv() Optional[Message]#

Receive message from client socket.

Receive a message from the connected client socket, by reading the network buffer.

Returns

A message of type OK or Error.

Return type

Optional[Message]

send(msg: Message, await_response: bool = False) Optional[Message]#

Send a message to client socket.

Send a message to a connected client socket via the network buffer. By default, after the message is sent and a confirmation message is received the method returns. If ‘await_response’ is set to True, after receiving the confirmation, the socket waits for another response by the remote client socket, which is again confirmed if received.

Note

If ‘await_response’ is set to True, but the remote client socket is not sending a response, this socket is waiting forever.

Parameters
  • msg (Message) – Message to send to the remote client socket.

  • await_response (bool, optional) – Waits for another response (after confirmation of the request) by the remote client socket, by default False

Returns

The confirmation message of the remote client socket. Or, if ‘await_response’ is set to True, a custom response from the remote client socket.

Return type

Message

sock#
class bgpy.core.sockets.ServerSocket(host: str, port: int, backlog: int = 3, log_level: str = 'WARNING', log_file: Optional[Path] = None)#

Bases: ContextDecorator

Server socket

Socket used on the server to listen on a port for requests. If the server accepts a connection request a new client socket for the communication is created, which binds to the client socket on the client.

accept() socket#

Accepts connection requests from client sockets and creates a client socket for the communication.

Returns

Returns a client socket on the server to communicate with the remote client socket.

Return type

socket

log#
sock#

bgpy.core.token module#

bgpy.core.token.token_create(length: int = 64) str#

Creates a url safe token with a defined length.

Parameters

length (int, optional) – Length of the token, by default 64

Returns

The token.

Return type

str

bgpy.core.token.token_delenv() None#

Deletes the token from the environment.

bgpy.core.token.token_getenv() Optional[str]#

Gets the token from the environment.

Returns

The token.

Return type

Optional[str]

bgpy.core.token.token_setenv(token: Optional[str]) None#

Sets a token in the environment.

Parameters

token (Optional[str]) – The token to set.

Module contents#

Sub-level module ‘core’ for bgpy package.