feat(python): add config support to python with a getter/setter approach

This commit is contained in:
cschen 2024-09-14 00:17:46 +02:00
parent 5701a0c49e
commit c1ed0d45f3
4 changed files with 22 additions and 17 deletions

View file

@ -7,6 +7,7 @@ class Driver:
"""
def stop(self) -> None: ...
def get_default_config() -> Config: ...
class Config:
"""
Configuration data structure for codemp clients

View file

@ -1,7 +1,6 @@
//! # Config
//! Data structure defining clients configuration
/// Configuration struct for `codemp` client
///
/// username and password are required fields, while everything else is optional
@ -11,7 +10,7 @@
/// http{tls?'s':''}://{host}:{port}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "js", napi_derive::napi(object))]
#[cfg_attr(feature = "py", pyo3::pyclass(get_all))]
#[cfg_attr(feature = "python", pyo3::pyclass(get_all, set_all))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Config {
/// user identifier used to register, possibly your email
@ -29,7 +28,13 @@ pub struct Config {
impl Config {
/// construct a new Config object, with given username and password
pub fn new(username: String, password: String) -> Self {
Self { username, password, host: None, port: None, tls: None }
Self {
username,
password,
host: None,
port: None,
tls: None,
}
}
#[inline]

View file

@ -14,18 +14,6 @@ impl Client {
// super::tokio().block_on(Client::connect(host, username, password))
// }
// #[pyo3(name = "join_workspace")]
// async fn pyjoin_workspace(&self, workspace: String) -> JoinHandle<crate::Result<Workspace>> {
// tracing::info!("attempting to join the workspace {}", workspace);
// let this = self.clone();
// async {
// tokio()
// .spawn(async move { this.join_workspace(workspace).await })
// .await
// }
// }
#[pyo3(name = "join_workspace")]
fn pyjoin_workspace(&self, py: Python<'_>, workspace: String) -> PyResult<super::Promise> {
tracing::info!("attempting to join the workspace {}", workspace);

View file

@ -146,8 +146,18 @@ fn init() -> PyResult<Driver> {
}
#[pyfunction]
fn connect(config: crate::api::Config) -> PyResult<Promise> {
a_sync!(Client::connect(config).await)
fn get_default_config() -> crate::api::Config {
let mut conf = crate::api::Config::new("".to_string(), "".to_string());
conf.host = Some(conf.host().to_string());
conf.port = Some(conf.port());
conf.tls = Some(false);
conf
}
#[pyfunction]
fn connect(py: Python, config: Py<crate::api::Config>) -> PyResult<Promise> {
let conf: crate::api::Config = config.extract(py)?;
a_sync!(Client::connect(conf).await)
}
#[pyfunction]
@ -218,6 +228,7 @@ impl IntoPy<PyObject> for crate::api::User {
#[pymodule]
fn codemp(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(init, m)?)?;
m.add_function(wrap_pyfunction!(get_default_config, m)?)?;
m.add_function(wrap_pyfunction!(connect, m)?)?;
m.add_function(wrap_pyfunction!(set_logger, m)?)?;
m.add_class::<Driver>()?;