2024-09-17 22:20:00 +02:00
|
|
|
import logging
|
2024-09-28 19:55:20 +02:00
|
|
|
import codemp
|
2024-09-17 22:20:00 +02:00
|
|
|
|
2024-11-02 18:26:37 +01:00
|
|
|
from ..utils import some
|
|
|
|
|
2024-09-17 22:20:00 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class SessionManager():
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self._running = False
|
|
|
|
self._driver = None
|
|
|
|
self._client = None
|
|
|
|
|
2024-09-18 16:57:26 +02:00
|
|
|
def is_init(self):
|
|
|
|
return self._running and self._driver is not None
|
|
|
|
|
2024-09-17 22:20:00 +02:00
|
|
|
def is_active(self):
|
2024-09-18 16:57:26 +02:00
|
|
|
return self.is_init() and self._client is not None
|
2024-09-17 22:20:00 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def client(self):
|
2024-11-02 18:26:37 +01:00
|
|
|
return some(self._client)
|
2024-09-17 22:20:00 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-09-18 16:57:26 +02:00
|
|
|
self.drop_client()
|
2024-09-17 22:20:00 +02:00
|
|
|
self._driver.stop()
|
|
|
|
self._running = False
|
|
|
|
self._driver = None
|
|
|
|
|
|
|
|
def connect(self, config: codemp.Config) -> codemp.Client:
|
2024-09-18 16:57:26 +02:00
|
|
|
if not self.is_init():
|
2024-09-17 22:20:00 +02:00
|
|
|
self.get_or_init()
|
|
|
|
|
|
|
|
self._client = codemp.connect(config).wait()
|
2024-11-02 18:26:37 +01:00
|
|
|
self.config = config
|
|
|
|
logger.debug(f"Connected to '{self.config.host}' as user {self._client.current_user().name} (id: {self._client.current_user().id})")
|
2024-09-17 22:20:00 +02:00
|
|
|
return self._client
|
|
|
|
|
2024-11-02 18:26:37 +01:00
|
|
|
def get_workspaces(self, owned: bool = True, invited: bool = True):
|
|
|
|
owned_wss = self.client.fetch_owned_workspaces().wait() if owned else []
|
|
|
|
invited_wss = self.client.fetch_joined_workspaces().wait() if invited else []
|
|
|
|
|
|
|
|
return owned_wss + invited_wss
|
|
|
|
|
2024-09-18 16:57:26 +02:00
|
|
|
def drop_client(self):
|
2024-09-17 22:20:00 +02:00
|
|
|
self._client = None
|
|
|
|
|
|
|
|
session = SessionManager()
|