codemp-sublime/plugin/core/session.py
cschen a8cc089624 wip: huge refactor with smarter and simpler struture
Former-commit-id: fbd0bca8094642dd8d2cf6c3f154af3b10dff95b
2024-09-18 17:42:56 +02:00

60 lines
No EOL
1.3 KiB
Python

import logging
from ...lib import codemp
logger = logging.getLogger(__name__)
class SessionManager():
def __init__(self) -> None:
self._running = False
self._driver = None
self._client = None
def is_active(self):
return self._driver is not None \
and self._running \
and self._client is not None
@property
def client(self):
return self._client
def get_or_init(self) -> codemp.Driver:
if self._driver:
return self._driver
self._driver = codemp.init()
logger.debug("registering logger callback...")
if not codemp.set_logger(lambda msg: logger.debug(msg), False):
logger.debug(
"could not register the logger... \
If reconnecting it's ok, \
the previous logger is still registered"
)
self._running = True
return self._driver
def stop(self):
if not self._driver:
return
self.disconnect()
self._driver.stop()
self._running = False
self._driver = None
def connect(self, config: codemp.Config) -> codemp.Client:
if not self._running:
self.get_or_init()
self._client = codemp.connect(config).wait()
logger.debug(f"Connected to '{config.host}' as user {self._client.user_name} (id: {self._client.user_id})")
return self._client
def disconnect(self):
if not self._client:
return
self._client = None
session = SessionManager()