mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 07:14:50 +01:00
feat(python): add config support to python with a getter/setter approach
This commit is contained in:
parent
5701a0c49e
commit
c1ed0d45f3
4 changed files with 22 additions and 17 deletions
1
dist/py/src/codemp/codemp.pyi
vendored
1
dist/py/src/codemp/codemp.pyi
vendored
|
@ -7,6 +7,7 @@ class Driver:
|
|||
"""
|
||||
def stop(self) -> None: ...
|
||||
|
||||
def get_default_config() -> Config: ...
|
||||
class Config:
|
||||
"""
|
||||
Configuration data structure for codemp clients
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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>()?;
|
||||
|
|
Loading…
Reference in a new issue