swergio package

swergio.client module

class swergio.client.Client(name, server, port, format='utf-8', header_length=10, **kwargs)[source]

Bases: object

This class defines a client, which can be used to connect to a server and send and receive messages.

Parameters:
  • name – The name of the client.

  • server – The IP address of the server.

  • port – The port number of the server.

  • format – The encoding format to use for messages.

  • header_length – The length of the message header in bytes.

  • kwargs – Additional keyword arguments that will be passed to event handler functions when they are called.

Variables:
  • name – The name of the client.

  • server – The IP address of the server.

  • port – The port number of the server.

  • format – The encoding format to use for messages.

  • header_length – The length of the message header in bytes.

  • eventHandlers – A set of EventHandler instances registered with this client.

  • rooms – A set of rooms that the client has joined.

  • kwargs – Additional keyword arguments that will be passed to event handler functions when they are called.

  • client – The socket used to connect to the server.

add_eventHandler(handleFunction, responseType, responseRooms=None, responseComponent=None, trigger=None)[source]

Add a new event handler to this client.

Parameters:
  • handleFunction – The function to call when a message is handled by this event handler. This function should take the same arguments as the handle method of the EventHandler class and return a response message, or None if no response is needed.

  • responseType – The type of the response message.

  • responseRooms – A list of room IDs where the response message should be sent. If not specified, the response will be sent to the same room as the original message.

  • responseComponent – The component ID where the response message should be sent.

  • trigger – A Trigger instance that specifies the criteria for triggering this event handler. If not specified, the event handler will never be triggered.

add_propagated_fields(message, response)[source]

Add the fields from the original message that should be propagated to the response message. This includes the root ID, the model status of the original message as well as the name of the sender.

Parameters:
  • message – The original message.

  • response – The response message.

Returns:

The updated response message.

close()[source]

Close the connection to the server. This will send a DISCONNECT command message to the server and close the socket.

connect(server, port)[source]

Connect to the server at the given IP address and port number.

Parameters:
  • server – The IP address of the server.

  • port – The port number of the server.

Returns:

The socket used to connect to the server.

filter_dict(dictionary, function)[source]

Filter the given dictionary and return a new dictionary containing only the keys that correspond to arguments of the given function.

Parameters:
  • dictionary – The dictionary to filter.

  • function – The function whose arguments should be used to filter the dictionary.

Returns:

The filtered dictionary.

join_room(room)[source]

Join the given room. This will send a JOINROOM command message to the server.

Parameters:

room – The ID of the room to join.

listen()[source]

Listen for incoming messages from the server and handle them using the registered event handlers. This method will block until the connection to the server is closed.

receive()[source]

Receive a message from the server.

Returns:

The received message, or False if an error occurred.

register()[source]

Register this client with the server. This will send a REGISTER command message to the server and join the reserved rooms.

send(message)[source]

Send the given message to the server.

Parameters:

message – The message to send.

class swergio.client.EventHandler(handleFunction, responseType, responseRooms=None, responseComponent=None, trigger=None)[source]

Bases: object

This class defines an event handler, which can be used to handle messages that match certain criteria.

Parameters:
  • handleFunction – The function to call when a message is handled by this event handler. This function should take the same arguments as the handle method of this class and return a response message, or None if no response is needed.

  • responseType – The type of the response message.

  • responseRooms – A list of room IDs where the response message should be sent. If not specified, the response will be sent to the same room as the original message.

  • responseComponent – The component ID where the response message should be sent.

  • trigger – A Trigger instance that specifies the criteria for triggering this event handler. If not specified, the event handler will never be triggered.

Variables:
  • handleFunction – The function to call when a message is handled by this event handler.

  • responseType – The type of the response message.

  • responseRooms – A list of room IDs where the response message should be sent.

  • responseComponent – The component ID where the response message should be sent.

  • trigger – A Trigger instance that specifies the criteria for triggering this event handler.

handle(*args, **kwargs)[source]

Handle the given message by calling the handleFunction specified in the constructor, and return a response message if needed.

Parameters:
  • args – Positional arguments to pass to handleFunction.

  • kwargs – Keyword arguments to pass to handleFunction.

Returns:

A response message, or None if no response is needed.

is_triggered(message)[source]

Check if this event handler is triggered by the given message.

Parameters:

message – The message to check.

Returns:

True if the message matches the criteria specified for this event handler, False otherwise.

class swergio.client.Trigger(types, rooms=None, directmessage=True)[source]

Bases: object

This class defines a trigger, which can be used to check if a given message matches certain criteria.

Parameters:
  • types – A list of message types that should trigger this trigger.

  • rooms – A list of room IDs that should trigger this trigger. If not specified, the trigger will be triggered by messages in any room.

  • directmessage – A boolean indicating whether direct messages (messages not sent to a specific room) should trigger this trigger.

Variables:
  • types – A list of message types that should trigger this trigger.

  • rooms – A list of room IDs that should trigger this trigger.

  • directmessage – A boolean indicating whether direct messages should trigger this trigger.

is_triggered(message)[source]

Check if this trigger is triggered by the given message.

Parameters:

message – The message to check.

Returns:

True if the message matches the criteria specified for this trigger, False otherwise.

swergio.messageType module

class swergio.messageType.COMMAND[source]

Bases: MessageMainType

This class contains the possible settings for a COMMAND message.

CUSTOM = <swergio.messageType.MessageTypeSetting object>
DISABLELOGGING = <swergio.messageType.MessageTypeSetting object>
DISCONNECT = <swergio.messageType.MessageTypeSetting object>
ENABLELOGGING = <swergio.messageType.MessageTypeSetting object>
JOINROOM = <swergio.messageType.MessageTypeSetting object>
LEAVEROOM = <swergio.messageType.MessageTypeSetting object>
LOADMODELWEIGHTS = <swergio.messageType.MessageTypeSetting object>
LOADSETTINGS = <swergio.messageType.MessageTypeSetting object>
REGISTER = <swergio.messageType.MessageTypeSetting object>
SAVEMODELWEIGHTS = <swergio.messageType.MessageTypeSetting object>
SAVESETTINGS = <swergio.messageType.MessageTypeSetting object>
class swergio.messageType.DATA[source]

Bases: MessageMainType

This class contains the possible settings for a DATA message.

CUSTOM = <swergio.messageType.MessageTypeSetting object>
FORWARD = <swergio.messageType.MessageTypeSetting object>
GRADIENT = <swergio.messageType.MessageTypeSetting object>
REWARD = <swergio.messageType.MessageTypeSetting object>
TEXT = <swergio.messageType.MessageTypeSetting object>
class swergio.messageType.LOG[source]

Bases: MessageMainType

This class contains the possible settings for a LOG message.

CUSTOM = <swergio.messageType.MessageTypeSetting object>
KPI = <swergio.messageType.MessageTypeSetting object>
MESSAGE = <swergio.messageType.MessageTypeSetting object>
MODELWEIGHTS = <swergio.messageType.MessageTypeSetting object>
RUN = <swergio.messageType.MessageTypeSetting object>
SETTINGS = <swergio.messageType.MessageTypeSetting object>
class swergio.messageType.MESSAGE_TYPE[source]

Bases: object

This class contains the possible settings for a message’s main type.

class COMMAND

Bases: MessageMainType

This class contains the possible settings for a COMMAND message.

CUSTOM = <swergio.messageType.MessageTypeSetting object>
DISABLELOGGING = <swergio.messageType.MessageTypeSetting object>
DISCONNECT = <swergio.messageType.MessageTypeSetting object>
ENABLELOGGING = <swergio.messageType.MessageTypeSetting object>
JOINROOM = <swergio.messageType.MessageTypeSetting object>
LEAVEROOM = <swergio.messageType.MessageTypeSetting object>
LOADMODELWEIGHTS = <swergio.messageType.MessageTypeSetting object>
LOADSETTINGS = <swergio.messageType.MessageTypeSetting object>
REGISTER = <swergio.messageType.MessageTypeSetting object>
SAVEMODELWEIGHTS = <swergio.messageType.MessageTypeSetting object>
SAVESETTINGS = <swergio.messageType.MessageTypeSetting object>
class DATA

Bases: MessageMainType

This class contains the possible settings for a DATA message.

CUSTOM = <swergio.messageType.MessageTypeSetting object>
FORWARD = <swergio.messageType.MessageTypeSetting object>
GRADIENT = <swergio.messageType.MessageTypeSetting object>
REWARD = <swergio.messageType.MessageTypeSetting object>
TEXT = <swergio.messageType.MessageTypeSetting object>
class LOG

Bases: MessageMainType

This class contains the possible settings for a LOG message.

CUSTOM = <swergio.messageType.MessageTypeSetting object>
KPI = <swergio.messageType.MessageTypeSetting object>
MESSAGE = <swergio.messageType.MessageTypeSetting object>
MODELWEIGHTS = <swergio.messageType.MessageTypeSetting object>
RUN = <swergio.messageType.MessageTypeSetting object>
SETTINGS = <swergio.messageType.MessageTypeSetting object>
static by_id(id)[source]

Returns the message type setting with the given id, or None if no such setting exists.

Parameters:

id – The unique identifier of the message type setting to return.

Returns:

The message type setting with the given id, or None if no such setting exists.

Return type:

MessageTypeSetting or None

class swergio.messageType.MODEL_STATUS[source]

Bases: object

This class contains the possible settings for a model’s status.

TRAIN = <swergio.messageType.ModelStatusSetting object>
VALIDATE = <swergio.messageType.ModelStatusSetting object>
static by_id(id)[source]

Returns the model status setting with the given id, or None if no such setting exists.

Parameters:

id – The unique identifier of the model status setting to return.

Returns:

The model status setting with the given id, or None if no such setting exists.

Return type:

ModelStatusSetting or None

class swergio.messageType.MessageMainType[source]

Bases: object

static by_id(id, cls)[source]

Returns the message type setting with the given id from the given class, or None if no such setting exists.

Parameters:
  • id – The unique identifier of the message type setting to return.

  • cls – The class to search for the message type setting.

Returns:

The message type setting with the given id, or None if no such setting exists.

Return type:

MessageTypeSetting or None

class swergio.messageType.MessageTypeSetting(id, name, required_fields, optional_fields)[source]

Bases: object

This class represents a setting for a message type.

Parameters:
  • id – The unique identifier for the message type setting.

  • name – The name of the message type setting.

  • required_fields – A list of field names that are required for a message of this type.

  • optional_fields – A list of field names that are optional for a message of this type.

check_fields(msg_content)[source]

Checks if the given message content contains all of the required fields for this message type.

Parameters:

msg_content – A dictionary containing the fields and values for the message.

Returns:

True if the message content contains all of the required fields, False otherwise.

Return type:

bool

class swergio.messageType.ModelStatusSetting(id, name)[source]

Bases: object

This class represents a setting for a model’s status.

Parameters:
  • id – The unique identifier for the model status setting.

  • name – The name of the model status setting.

swergio.server module

class swergio.server.Server(ip: str, port: int, format: str, header_length: int, enable_logging=True)[source]

Bases: object

broadcast_message(client, message_header, message, msg_content)[source]

Broadcasts the given message to the intended recipients :param client: Client connection instance who sent the message :type client: socket.socket :param message_header: Header of the message :type message_header: bytes :param message: Message data :type message: str :param msg_content: Message content containing the recipient details :type msg_content: dict

disconnect_client(client)[source]

Disconnects the given client from the server :param client: Client connection instance :type client: socket.socket :return: False to indicate disconnection :rtype: bool

handle_client(client, addr)[source]

Handles the incoming client connection and processes the messages

Parameters:
  • client (socket.socket) – Client connection instance

  • addr (tuple) – IP address of the client

join_room(client, msg_content)[source]

Adds the given client to the specified room :param client: Client connection instance :type client: socket.socket :param msg_content: Message content containing the name of the room :type msg_content: dict

leave_room(client, msg_content)[source]

Removes the given client from the specified room :param client: Client connection instance :type client: socket.socket :param msg_content: Message content containing the name of the room :type msg_content: dict

register_client(client, msg_content)[source]

Registers the client with the given name :param client: Client connection instance :type client: socket.socket :param msg_content: Message content containing the name of the client :type msg_content: dict :return: Name of the registered client :rtype: str

send_message_log(msg_content)[source]

Sends the given message to the logging room :param msg_content: Message content to be logged :type msg_content: dict

start()[source]

Starts the server and listens for incoming client connections