Skip to main content

An SDK for the TracimDaemon project

Project description

TracimDaemonSDK

A python port for the TracimDaemonSDK, an SDK for the TracimDaemon project

For a quickstart see TracimDaemon Quickstart

Usage

Get the package

pip install tracim_daemon_sdk

Create a new TracimDaemon client

from tracim_daemon_sdk import TracimDaemonClient

if __name__ == "__main__":
    client = TracimDaemonClient(
        master_socket_path='/home/user/.config/TracimDaemon/master.sock',
        client_socket_path='/home/user/.config/MiniClient/socket.sock',
    )

Create and listen to the client socket

from tracim_daemon_sdk import TracimDaemonClient

if __name__ == "__main__":
    client = TracimDaemonClient(
        master_socket_path='/home/user/.config/TracimDaemon/master.sock',
        client_socket_path='/home/user/.config/MyApp/tracim_daemon.sock',
    )
    client.create_client_socket()

From now on it is recommended to wrap the rest of the code in a try/finally block to ensure the socket is closed properly

from tracim_daemon_sdk import TracimDaemonClient

if __name__ == "__main__":
    client = TracimDaemonClient(
        master_socket_path='/home/user/.config/TracimDaemon/master.sock',
        client_socket_path='/home/user/.config/MyApp/tracim_daemon.sock',
    )
    client.create_client_socket()
    try:
        pass
    finally:
        client.close()

Set up various handlers then register the client to the daemon and start listening to events

from tracim_daemon_sdk import TracimDaemonClient
from tracim_daemon_sdk.event import DAEMON_TRACIM_EVENT
from tracim_daemon_sdk.daemon_event import DaemonEvent
from tracim_daemon_sdk.data import TLMEvent
from tracim_daemon_sdk.helper import decode_json


def default_event_handler(c: TracimDaemonClient, e: DaemonEvent):
    tlm: TLMEvent = decode_json(e.data)
    print(tlm.event_type)


if __name__ == "__main__":
    client = TracimDaemonClient(
        master_socket_path='/home/user/.config/TracimDaemon/master.sock',
        client_socket_path='/home/user/.config/MyApp/tracim_daemon.sock',
    )
    client.create_client_socket()
    try:
        client.event_handlers[DAEMON_TRACIM_EVENT] = default_event_handler
        client.register_to_master()
        client.listen_to_events()
    finally:
        client.close()

The "minimal" client code is as above.

Handlers and data

A handler follows the EventHandler definition (see below). If the event type is expecting data, it is possible to parse it using the helper functions

from tracim_daemon_sdk import TracimDaemonClient
from tracim_daemon_sdk.daemon_event import DaemonEvent
from tracim_daemon_sdk.data import TLMEvent
from tracim_daemon_sdk.helper import decode_json

def default_event_handler(c: TracimDaemonClient, e: DaemonEvent):
    tlm: TLMEvent = decode_json(e.data)
    print(tlm.event_type)

This handler stores the parsed data in a TLMEvent object and prints the event type.

Definitions

TLMEvent

TLMEvent is the class that represents the data sent by Tracim (see tracim TLM documentation)

from time import time
from typing import Any

class TLMEvent:
    def __init__(self,
                 event_id: int = 0,
                 event_type: str = "",
                 read: Any = None,
                 created: time = None,
                 fields: Any = None):
        self.event_id: int = event_id
        self.event_type: str = event_type
        self.read: Any = read
        self.created: time = created
        self.fields: Any = fields

DaemonEvent

DaemonEvent is the event format used to communicate between apps

from typing import Any

class DaemonEvent:
    def __init__(self, path: str = "", event_type: str = "", data: Any = None):
        self.path: str = path
        self.type: str = event_type
        self.data: Any = data
  • The path field is the path to the client socket (as defined in the config)
  • The type field is any of the constants defined in event.py
  • The data field can contain additional information of any format

A type is expected to contain additional data if there is a <eventType>Data class defined in data.py.

EventHandler

EventHandler is the function definition for the event handlers. It takes a TracimDaemonClient and a DaemonEvent as parameters

from tracim_daemon_sdk import TracimDaemonClient
from tracim_daemon_sdk.daemon_event import DaemonEvent

def handler(c: TracimDaemonClient, e: DaemonEvent) -> None:
    pass

By default, handlers for DAEMON_ACCOUNT_INFO and DAEMON_PING are already defined, it is possible to override them.

Event types

Event types are defined by tracim. It is also possible to set handlers for every DaemonEvent type. There also is events defined by the SDK, for convenience.

# EVENT_TYPE_GENERIC is the event type for generic events (every DaemonEvent)
EVENT_TYPE_GENERIC = "custom_message"
# EVENT_TYPE_ERROR is the event type for errors
EVENT_TYPE_ERROR = "custom_error"

Protocol (for developers of another language)

Communication

Client and daemons communicate with the DaemonEvent format, i.e. JSON data following this format:

{
    "type": "event_type",
    "path": "/path/to/client/socket",
    "data": {}
}

(See above DaemonEvent section for details about each field)

Registering / unregistering a client

When registering / unregistering a client a DaemonEvent must be sent on the daemon socket.

{
    "type": "client_add",
    "path": "/path/to/client/socket",
    "data": {
      "path": "/path/to/client/socket",
      "pid": "999"
    }
}

To register a client, the client must send the message to the daemon socket, with the type field set to client_add. To unregister a client, the client must send the message to the daemon socket, with the type field set to client_delete.

In both, additional info, defined as follows, is required:

{
  "path": "/path/to/client/socket",
  "pid": "999"
}

With pid being the PID of the client process.

Receiving events

Once registered, the client will receive DaemonEvents from the daemon.

(See above DaemonEvent section for details about types and data)

Ack and Keep-Alive

Ack

The daemon will send a DaemonAck upon receiving any managed events not expecting a response, otherwise the expected response is sent. As for now, a DaemonPong for a DaemonPing and a DaemonAccountInfo for a DaemonClientAdd

The daemon expects no DaemonAck on its messages.

Keep-Alive

The daemon will periodically (once every minute) send a DaemonPing event, clients have a minute to respond with DaemonPong, If not, it will unregister un-responding clients at the next ping.

It is possible to test the daemon responsiveness by sending it DaemonPing events. It will respond with a DaemonPong as soon as possible.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tracim_daemon_sdk-1.0.3.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tracim_daemon_sdk-1.0.3-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file tracim_daemon_sdk-1.0.3.tar.gz.

File metadata

  • Download URL: tracim_daemon_sdk-1.0.3.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for tracim_daemon_sdk-1.0.3.tar.gz
Algorithm Hash digest
SHA256 d166929f93c0abc814249e76aa7ac289a6c1598af71027a91a9db37510425e63
MD5 8165c5a6712f16bd39cf9d742bf13b24
BLAKE2b-256 be6e2b27e277a6de3a48dbe1d00c6c38ba3389d866c010a3d5d34fe4c30006b5

See more details on using hashes here.

File details

Details for the file tracim_daemon_sdk-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for tracim_daemon_sdk-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 fc335ef04df7a633d7ace509242fd2baf8c45f2258e2e6955bf2a028e0db006a
MD5 454e6b58b113031d3d734de2376a2d31
BLAKE2b-256 1ced63fe9fcc24ec05083074db6a5c3d725a04f3f7254a0c94b9dd3d6502b3fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page