2024-02-21 23:59:49 +01:00
|
|
|
from __future__ import annotations
|
2024-08-24 18:45:42 +02:00
|
|
|
from typing import Optional
|
2024-09-17 22:20:00 +02:00
|
|
|
from typing import TYPE_CHECKING
|
2024-08-23 20:59:06 +02:00
|
|
|
|
2024-09-17 22:20:00 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ..workspace.workspace import VirtualWorkspace
|
2023-08-17 18:39:47 +02:00
|
|
|
|
2024-02-23 13:25:01 +01:00
|
|
|
import sublime
|
2024-08-09 19:20:58 +02:00
|
|
|
import logging
|
2023-08-25 14:29:11 +02:00
|
|
|
|
2024-09-17 22:20:00 +02:00
|
|
|
from ..utils import bidict
|
2024-08-09 19:20:58 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2024-02-21 23:59:49 +01:00
|
|
|
|
2024-08-21 21:35:57 +02:00
|
|
|
|
2024-08-23 20:59:06 +02:00
|
|
|
class VirtualClient:
|
2024-08-04 19:57:59 +02:00
|
|
|
def __init__(self):
|
2024-08-23 20:59:06 +02:00
|
|
|
self.codemp: Optional[codemp.Client] = None
|
2024-08-31 15:24:22 +02:00
|
|
|
self.driver: Optional[codemp.Driver] = None
|
2024-02-21 23:59:49 +01:00
|
|
|
|
2024-08-23 20:59:06 +02:00
|
|
|
# bookkeeping corner
|
2024-08-29 08:17:52 +02:00
|
|
|
self._id2buffer: dict[str, VirtualBuffer] = {}
|
|
|
|
self._id2workspace: dict[str, VirtualWorkspace] = {}
|
|
|
|
|
2024-08-31 15:24:22 +02:00
|
|
|
self._view2buff: dict[sublime.View, VirtualBuffer] = {}
|
2024-08-29 08:17:52 +02:00
|
|
|
self._buff2workspace: bidict[VirtualBuffer, VirtualWorkspace] = bidict()
|
2024-09-17 17:20:00 +02:00
|
|
|
self._workspace2window: dict[VirtualWorkspace, sublime.Window] = {}
|
2024-08-21 21:35:57 +02:00
|
|
|
|
2024-08-23 20:59:06 +02:00
|
|
|
def disconnect(self):
|
|
|
|
if self.codemp is None:
|
2024-08-04 19:57:59 +02:00
|
|
|
return
|
2024-08-23 20:59:06 +02:00
|
|
|
logger.info("disconnecting from the current client")
|
|
|
|
# for each workspace tell it to clean up after itself.
|
|
|
|
for vws in self.all_workspaces():
|
2024-09-02 11:38:11 +02:00
|
|
|
self.uninstall_workspace(vws)
|
2024-08-23 20:59:06 +02:00
|
|
|
self.codemp.leave_workspace(vws.id)
|
2024-08-04 19:57:59 +02:00
|
|
|
|
2024-08-29 08:17:52 +02:00
|
|
|
self._id2workspace.clear()
|
|
|
|
self._id2buffer.clear()
|
|
|
|
self._buff2workspace.clear()
|
|
|
|
self._view2buff.clear()
|
|
|
|
self._workspace2window.clear()
|
2024-09-17 16:23:19 +02:00
|
|
|
|
|
|
|
if self.driver is not None:
|
|
|
|
self.driver.stop()
|
|
|
|
self.driver = None
|
2024-08-23 20:59:06 +02:00
|
|
|
self.codemp = None
|
2024-08-04 19:57:59 +02:00
|
|
|
|
2024-08-23 20:59:06 +02:00
|
|
|
def connect(self, host: str, user: str, password: str):
|
|
|
|
if self.codemp is not None:
|
|
|
|
logger.info("Disconnecting from previous client.")
|
|
|
|
return self.disconnect()
|
2024-08-04 19:57:59 +02:00
|
|
|
|
2024-08-31 15:24:22 +02:00
|
|
|
if self.driver is None:
|
|
|
|
self.driver = codemp.init()
|
2024-09-02 11:38:11 +02:00
|
|
|
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"
|
|
|
|
)
|
2024-08-31 15:24:22 +02:00
|
|
|
|
2024-09-17 17:20:00 +02:00
|
|
|
config = codemp.get_default_config()
|
|
|
|
config.username = user
|
|
|
|
config.host = host
|
|
|
|
config.password = password
|
|
|
|
|
|
|
|
self.codemp = codemp.connect(config).wait()
|
2024-08-23 20:59:06 +02:00
|
|
|
id = self.codemp.user_id()
|
|
|
|
logger.debug(f"Connected to '{host}' as user {user} (id: {id})")
|
2024-08-09 09:17:38 +02:00
|
|
|
|
2024-08-31 15:24:22 +02:00
|
|
|
def install_workspace(self, workspace: codemp.Workspace, window: sublime.Window):
|
2024-08-23 20:59:06 +02:00
|
|
|
vws = VirtualWorkspace(workspace, window)
|
2024-08-29 08:17:52 +02:00
|
|
|
self._workspace2window[vws] = window
|
|
|
|
self._id2workspace[vws.id] = vws
|
2024-08-09 09:17:38 +02:00
|
|
|
|
2024-08-23 20:59:06 +02:00
|
|
|
def uninstall_workspace(self, vws: VirtualWorkspace):
|
2024-08-31 15:24:22 +02:00
|
|
|
# we aim at dropping all references to the workspace
|
|
|
|
# as well as all the buffers associated with it.
|
|
|
|
# if we did a good job the dunder del method will kick
|
|
|
|
# and continue with the cleanup.
|
2024-08-23 20:59:06 +02:00
|
|
|
logger.info(f"Uninstalling workspace '{vws.id}'...")
|
2024-08-29 08:17:52 +02:00
|
|
|
del self._workspace2window[vws]
|
|
|
|
del self._id2workspace[vws.id]
|
2024-08-31 15:24:22 +02:00
|
|
|
for vbuff in self.all_buffers(vws):
|
2024-08-29 08:17:52 +02:00
|
|
|
self.unregister_buffer(vbuff)
|
2024-09-02 11:38:11 +02:00
|
|
|
|
|
|
|
vws.uninstall()
|
2024-08-04 19:57:59 +02:00
|
|
|
|
2024-08-23 20:59:06 +02:00
|
|
|
def workspaces_in_server(self):
|
|
|
|
return self.codemp.active_workspaces() if self.codemp else []
|
2024-08-04 19:57:59 +02:00
|
|
|
|
2024-08-23 20:59:06 +02:00
|
|
|
def user_id(self):
|
|
|
|
return self.codemp.user_id() if self.codemp else None
|
2024-02-21 23:59:49 +01:00
|
|
|
|
|
|
|
|
2024-08-04 19:57:59 +02:00
|
|
|
client = VirtualClient()
|