Package: tracim-daemon-sdk
TracimDaemonSDKA python port for the TracimDaemonSDK, an SDK for theTracimDaemonprojectFor a quickstart seeTracimDaemon QuickstartUsageGet the packagepipinstalltracim_daemon_sdkCreate a new TracimDaemon clientfromtracim_daemon_sdkimportTracimDaemonClientif__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 socketfromtracim_daemon_sdkimportTracimDaemonClientif__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 properlyfromtracim_daemon_sdkimportTracimDaemonClientif__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:passfinally:client.close()Set up various handlers then register the client to the daemon and start listening to eventsfromtracim_daemon_sdkimportTracimDaemonClientfromtracim_daemon_sdk.eventimportDAEMON_TRACIM_EVENTfromtracim_daemon_sdk.daemon_eventimportDaemonEventfromtracim_daemon_sdk.dataimportTLMEventfromtracim_daemon_sdk.helperimportdecode_jsondefdefault_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_handlerclient.register_to_master()client.listen_to_events()finally:client.close()The "minimal" client code is as above.Handlers and dataA handler follows theEventHandlerdefinition (see below).
If the event type is expecting data, it is possible to parse it using the helper functionsfromtracim_daemon_sdkimportTracimDaemonClientfromtracim_daemon_sdk.daemon_eventimportDaemonEventfromtracim_daemon_sdk.dataimportTLMEventfromtracim_daemon_sdk.helperimportdecode_jsondefdefault_event_handler(c:TracimDaemonClient,e:DaemonEvent):tlm:TLMEvent=decode_json(e.data)print(tlm.event_type)This handler stores the parsed data in aTLMEventobject and prints the event type.DefinitionsTLMEventTLMEvent is the class that represents the data sent by Tracim (see tracim TLM documentation)fromtimeimporttimefromtypingimportAnyclassTLMEvent:def__init__(self,event_id:int=0,event_type:str="",read:Any=None,created:time=None,fields:Any=None):self.event_id:int=event_idself.event_type:str=event_typeself.read:Any=readself.created:time=createdself.fields:Any=fieldsDaemonEventDaemonEvent is the event format used to communicate between appsfromtypingimportAnyclassDaemonEvent:def__init__(self,path:str="",event_type:str="",data:Any=None):self.path:str=pathself.type:str=event_typeself.data:Any=dataThepathfield is the path to the client socket (as defined in the config)Thetypefield is any of the constants defined in event.pyThedatafield can contain additional information of any formatAtypeis expected to contain additional data if there is a<eventType>Dataclass defined indata.py.EventHandlerEventHandler is the function definition for the event handlers.
It takes aTracimDaemonClientand aDaemonEventas parametersfromtracim_daemon_sdkimportTracimDaemonClientfromtracim_daemon_sdk.daemon_eventimportDaemonEventdefhandler(c:TracimDaemonClient,e:DaemonEvent)->None:passBy default, handlers forDAEMON_ACCOUNT_INFOandDAEMON_PINGare already defined, it is possible to override them.Event typesEvent types are defined by tracim. It is also possible to set handlers for everyDaemonEventtype.
There also is events defined by the SDK, for convenience.#EVENT_TYPE_GENERICistheeventtypeforgenericevents(everyDaemonEvent)EVENT_TYPE_GENERIC="custom_message"#EVENT_TYPE_ERRORistheeventtypeforerrorsEVENT_TYPE_ERROR="custom_error"Protocol (for developers of another language)CommunicationClient and daemons communicate with theDaemonEventformat, i.e. JSON data following this format:{"type":"event_type","path":"/path/to/client/socket","data":{}}(See aboveDaemonEventsection for details about each field)Registering / unregistering a clientWhen registering / unregistering a client aDaemonEventmust 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 thetypefield set toclient_add.
To unregister a client, the client must send the message to the daemon socket, with thetypefield set toclient_delete.In both, additional info, defined as follows, is required:{"path":"/path/to/client/socket","pid":"999"}Withpidbeing the PID of the client process.Receiving eventsOnce registered, the client will receiveDaemonEvents from the daemon.(See aboveDaemonEventsection for details about types and data)Ack and Keep-AliveAckThe daemon will send aDaemonAckupon receiving any managed events not expecting a response, otherwise the
expected response is sent. As for now, aDaemonPongfor aDaemonPingand aDaemonAccountInfofor aDaemonClientAddThe daemon expects noDaemonAckon its messages.Keep-AliveThe daemon will periodically (once every minute) send aDaemonPingevent, clients have a minute to respond withDaemonPong,
If not, it will unregister un-responding clients at the next ping.It is possible to test the daemon responsiveness by sending itDaemonPingevents. It will respond with aDaemonPongas soon as possible.