Merge pull request #14 from hexedtech/better-conf2

Better configuration api for python
This commit is contained in:
əlemi 2024-09-21 13:27:09 +02:00 committed by GitHub
commit bf9746c8b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 5 deletions

View file

@ -7,7 +7,6 @@ class Driver:
""" """
def stop(self) -> None: ... def stop(self) -> None: ...
def get_default_config() -> Config: ...
class Config: class Config:
""" """
Configuration data structure for codemp clients Configuration data structure for codemp clients
@ -18,6 +17,8 @@ class Config:
port: Optional[int] port: Optional[int]
tls: Optional[bool] tls: Optional[bool]
def __new__(cls, *, username: str, password: str, **kwargs) -> Config: ...
def init() -> Driver: ... def init() -> Driver: ...
def set_logger(logger_cb: Callable[[str], None], debug: bool) -> bool: ... def set_logger(logger_cb: Callable[[str], None], debug: bool) -> bool: ...
def connect(config: Config) -> Promise[Client]: ... def connect(config: Config) -> Promise[Client]: ...

View file

@ -3,14 +3,17 @@ pub mod controllers;
pub mod workspace; pub mod workspace;
use crate::{ use crate::{
api::{Cursor, TextChange}, api::{Config, Cursor, TextChange},
buffer::Controller as BufferController, buffer::Controller as BufferController,
cursor::Controller as CursorController, cursor::Controller as CursorController,
Client, Workspace, Client, Workspace,
}; };
use pyo3::exceptions::{PyConnectionError, PyRuntimeError, PySystemError};
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::{
exceptions::{PyConnectionError, PyRuntimeError, PySystemError},
types::PyDict,
};
use std::sync::OnceLock; use std::sync::OnceLock;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
@ -154,9 +157,36 @@ fn get_default_config() -> crate::api::Config {
conf conf
} }
#[pymethods]
impl Config {
#[new]
#[pyo3(signature = (*, username, password, **kwds))]
pub fn pynew(
username: String,
password: String,
kwds: Option<Bound<'_, PyDict>>,
) -> PyResult<Self> {
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] #[pyfunction]
fn connect(py: Python, config: Py<crate::api::Config>) -> PyResult<Promise> { fn connect(py: Python, config: Py<Config>) -> PyResult<Promise> {
let conf: crate::api::Config = config.extract(py)?; let conf: Config = config.extract(py)?;
a_sync!(Client::connect(conf).await) a_sync!(Client::connect(conf).await)
} }
@ -241,6 +271,7 @@ fn codemp(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Workspace>()?; m.add_class::<Workspace>()?;
m.add_class::<Client>()?; m.add_class::<Client>()?;
m.add_class::<Config>()?;
Ok(()) Ok(())
} }