diff --git a/dist/py/src/codemp/codemp.pyi b/dist/py/src/codemp/codemp.pyi index d7eebc8..3a93af0 100644 --- a/dist/py/src/codemp/codemp.pyi +++ b/dist/py/src/codemp/codemp.pyi @@ -7,7 +7,6 @@ class Driver: """ def stop(self) -> None: ... -def get_default_config() -> Config: ... class Config: """ Configuration data structure for codemp clients @@ -18,6 +17,8 @@ class Config: port: Optional[int] tls: Optional[bool] + def __new__(cls, *, username: str, password: str, **kwargs) -> Config: ... + def init() -> Driver: ... def set_logger(logger_cb: Callable[[str], None], debug: bool) -> bool: ... def connect(config: Config) -> Promise[Client]: ... diff --git a/src/ffi/python/mod.rs b/src/ffi/python/mod.rs index 496f159..d1c7105 100644 --- a/src/ffi/python/mod.rs +++ b/src/ffi/python/mod.rs @@ -3,14 +3,17 @@ pub mod controllers; pub mod workspace; use crate::{ - api::{Cursor, TextChange}, + api::{Config, Cursor, TextChange}, buffer::Controller as BufferController, cursor::Controller as CursorController, Client, Workspace, }; -use pyo3::exceptions::{PyConnectionError, PyRuntimeError, PySystemError}; use pyo3::prelude::*; +use pyo3::{ + exceptions::{PyConnectionError, PyRuntimeError, PySystemError}, + types::PyDict, +}; use std::sync::OnceLock; use tokio::sync::{mpsc, oneshot}; @@ -154,9 +157,36 @@ fn get_default_config() -> crate::api::Config { conf } +#[pymethods] +impl Config { + #[new] + #[pyo3(signature = (*, username, password, **kwds))] + pub fn pynew( + username: String, + password: String, + kwds: Option>, + ) -> PyResult { + if let Some(kwgs) = kwds { + let host = kwgs.get_item("host")?.map(|e| e.extract().ok()).flatten(); + let port = kwgs.get_item("port")?.map(|e| e.extract().ok()).flatten(); + let tls = kwgs.get_item("tls")?.map(|e| e.extract().ok()).flatten(); + + Ok(Config { + username, + password, + host, + port, + tls, + }) + } else { + Ok(Config::new(username, password)) + } + } +} + #[pyfunction] -fn connect(py: Python, config: Py) -> PyResult { - let conf: crate::api::Config = config.extract(py)?; +fn connect(py: Python, config: Py) -> PyResult { + let conf: Config = config.extract(py)?; a_sync!(Client::connect(conf).await) } @@ -241,6 +271,7 @@ fn codemp(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; + m.add_class::()?; Ok(()) }