From e67b1c6bd08e1964d571b460d46c269c63a9b683 Mon Sep 17 00:00:00 2001 From: cschen Date: Fri, 20 Sep 2024 15:54:50 +0200 Subject: [PATCH 1/3] feat(py): better configuration constructor. Now it accept only keywords arguments so it support just passing in a dict. --- src/ffi/python/mod.rs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/ffi/python/mod.rs b/src/ffi/python/mod.rs index 496f159..5b4903a 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::{PyAttributeError, 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 { + let config = kwds.map_or(Config::new(username, password), |dict| { + let host: Option = dict.get_item("host")?.map(|s| s.extract()); + let port: Option = dict.get_item("port")?.map(|p| p.extract()); + let tls: Option = dict.get_item("tls")?.map(|t| t.extract()); + + Config { + username, + password, + host, + port, + tls, + } + }); + + Ok(config) + } +} + #[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) } From d60ac63b49b0329ed88d966ee9e07a03246c7094 Mon Sep 17 00:00:00 2001 From: cschen Date: Fri, 20 Sep 2024 16:39:09 +0200 Subject: [PATCH 2/3] fix(py): fixed the configuration and rearranged a bit. tested. builds and works. --- src/ffi/python/mod.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/ffi/python/mod.rs b/src/ffi/python/mod.rs index 5b4903a..d1c7105 100644 --- a/src/ffi/python/mod.rs +++ b/src/ffi/python/mod.rs @@ -11,7 +11,7 @@ use crate::{ use pyo3::prelude::*; use pyo3::{ - exceptions::{PyAttributeError, PyConnectionError, PyRuntimeError, PySystemError}, + exceptions::{PyConnectionError, PyRuntimeError, PySystemError}, types::PyDict, }; @@ -166,21 +166,21 @@ impl Config { password: String, kwds: Option>, ) -> PyResult { - let config = kwds.map_or(Config::new(username, password), |dict| { - let host: Option = dict.get_item("host")?.map(|s| s.extract()); - let port: Option = dict.get_item("port")?.map(|p| p.extract()); - let tls: Option = dict.get_item("tls")?.map(|t| t.extract()); + 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(); - Config { + Ok(Config { username, password, host, port, tls, - } - }); - - Ok(config) + }) + } else { + Ok(Config::new(username, password)) + } } } @@ -271,6 +271,7 @@ fn codemp(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; + m.add_class::()?; Ok(()) } From 17c7f0588cc4b754f11712b777b5744ab20e4d49 Mon Sep 17 00:00:00 2001 From: cschen Date: Fri, 20 Sep 2024 17:17:58 +0200 Subject: [PATCH 3/3] chore(python): forgot type hints --- dist/py/src/codemp/codemp.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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]: ...