Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Feb 6, 2024
1 parent ebf6e8c commit 83990f0
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 65 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "etcd-client-py"
version = "0.2.0-beta.1"
name = "etcd-client-py-test"
version = "0.2.17-beta.1"
edition = "2021"
authors = ["Lablup Inc."]
readme = "./README.md"
Expand All @@ -10,7 +10,7 @@ name = "etcd_client"
crate-type = ["cdylib"]

[dependencies]
etcd-client = "0.12.4"
etcd-client = { path = "/home/jopemachine/etcd-client"}
pyo3 = { version = "0.20.2", features = ["extension-module", "multiple-pymethods"] }
pyo3-asyncio = { version = "0.20.0", features = ["tokio-runtime"] }
tokio = { version = "1.32.0", features = ["sync"] }
Expand Down
37 changes: 20 additions & 17 deletions etcd_client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class CompareOp:
"""

class Compare:
@classmethod
@staticmethod
def version(key: str, cmp: "CompareOp", version: int) -> "Compare": ...
@classmethod
@staticmethod
def create_revision(key: str, cmp: "CompareOp", revision: int) -> "Compare": ...
@classmethod
@staticmethod
def mod_revision(key: str, cmp: "CompareOp", revision: int) -> "Compare": ...
@classmethod
@staticmethod
def value(key: str, cmp: "CompareOp", value: str) -> "Compare": ...
@classmethod
@staticmethod
def lease(key: str, cmp: "CompareOp", lease: int) -> "Compare": ...
def with_range(self, end: list[int]) -> "Compare": ...
def with_prefix(self) -> "Compare": ...
Expand All @@ -42,17 +42,18 @@ class Txn:
def or_else(self, operations: list["TxnOp"]) -> "Txn": ...

class TxnOp:
@classmethod
@staticmethod
def get(key: str) -> "TxnOp": ...
@classmethod
@staticmethod
def put(key: str, value: str) -> "TxnOp": ...
@classmethod
@staticmethod
def delete(key: str) -> "TxnOp": ...
@classmethod
@staticmethod
def txn(txn: "Txn") -> "TxnOp": ...

class TxnResponse:
def succeeded(self) -> bool: ...
def op_responses(self) -> None: ...

class Client:
""" """
Expand All @@ -65,20 +66,22 @@ class Client:
""" """
async def __aenter__(self) -> "Communicator":
""" """
async def __aexit__(self, *args) -> None:
""" """

class ConnectOptions:
def __init__(self) -> None: ...
def with_username(self, user: str, password: str) -> "ConnectOptions": ...
def with_keep_alive(self, interval: int, timeout: int) -> "ConnectOptions": ...
def with_user(self, user: str, password: str) -> "ConnectOptions": ...
def with_keep_alive(self, interval: float, timeout: float) -> "ConnectOptions": ...
def with_keep_alive_while_idle(self, enabled: bool) -> "ConnectOptions": ...
def with_connect_timeout(self, connect_timeout: int) -> "ConnectOptions": ...
def with_timeout(self, timeout: int) -> "ConnectOptions": ...
def with_tcp_keepalive(self, tcp_keepalive: int) -> "ConnectOptions": ...
def with_connect_timeout(self, connect_timeout: float) -> "ConnectOptions": ...
def with_timeout(self, timeout: float) -> "ConnectOptions": ...
def with_tcp_keepalive(self, tcp_keepalive: float) -> "ConnectOptions": ...

class Watch:
""" """

async def __aiter__(self) -> AsyncIterator["Watch"]:
def __aiter__(self) -> AsyncIterator["WatchEvent"]:
""" """
async def __anext__(self) -> "WatchEvent":
""" """
Expand Down Expand Up @@ -144,13 +147,13 @@ class WatchEvent:

key: str
value: str
event_type: "WatchEventType"
event: "WatchEventType"
prev_value: Optional[str]

def __init__(
key: str,
value: str,
event_type: "WatchEventType",
event: "WatchEventType",
prev_value: Optional[str] = None,
) -> None: ...

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[project]
name = "etcd-client-py"
name = "etcd-client-py-test"
description = "Yet another etcd-client API binding based on the Rust's etcd-client package"
readme = {file = "README.md", content-type = "text/markdown"}
requires-python = ">=3.10"
Expand All @@ -21,5 +21,5 @@ homepage = "https://github.com/lablup/etcd-client-py"
repository = "https://github.com/lablup/etcd-client-py"

[build-system]
requires = ["maturin>=0.15,<0.16"]
requires = ["maturin>=1.0,<2.0"]
build-backend = "maturin"
29 changes: 16 additions & 13 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,45 @@ pub struct PyConnectOptions(pub ConnectOptions);
impl PyConnectOptions {
#[new]
fn new() -> Self {
Self(ConnectOptions::default())
Self(ConnectOptions::new())
}

fn with_user(&self, name: String, password: String) -> Self {
PyConnectOptions(self.0.clone().with_user(name, password))
}

fn with_keep_alive(&self, interval: u64, timeout: u64) -> Self {
PyConnectOptions(
self.0
.clone()
.with_keep_alive(Duration::from_secs(interval), Duration::from_secs(timeout)),
)
fn with_keep_alive(&self, interval: f64, timeout: f64) -> Self {
PyConnectOptions(self.0.clone().with_keep_alive(
Duration::from_secs_f64(interval),
Duration::from_secs_f64(timeout),
))
}

fn with_keep_alive_while_idle(&self, enabled: bool) -> Self {
PyConnectOptions(self.0.clone().with_keep_alive_while_idle(enabled))
}

fn with_connect_timeout(&self, connect_timeout: u64) -> Self {
fn with_connect_timeout(&self, connect_timeout: f64) -> Self {
PyConnectOptions(
self.0
.clone()
.with_connect_timeout(Duration::from_secs(connect_timeout)),
.with_connect_timeout(Duration::from_secs_f64(connect_timeout)),
)
}

fn with_timeout(&self, timeout: u64) -> Self {
PyConnectOptions(self.0.clone().with_timeout(Duration::from_secs(timeout)))
fn with_timeout(&self, timeout: f64) -> Self {
PyConnectOptions(
self.0
.clone()
.with_timeout(Duration::from_secs_f64(timeout)),
)
}

fn with_tcp_keepalive(&self, tcp_keepalive: u64) -> Self {
fn with_tcp_keepalive(&self, tcp_keepalive: f64) -> Self {
PyConnectOptions(
self.0
.clone()
.with_tcp_keepalive(Duration::from_secs(tcp_keepalive)),
.with_tcp_keepalive(Duration::from_secs_f64(tcp_keepalive)),
)
}

Expand Down
30 changes: 5 additions & 25 deletions src/communicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ impl PyCommunicator {
})
}

fn lock<'a>(
&'a self,
py: Python<'a>,
name: String,
) -> PyResult<&'a PyAny> {
fn lock<'a>(&'a self, py: Python<'a>, name: String) -> PyResult<&'a PyAny> {
let client = self.0.clone();
future_into_py(py, async move {
let mut client = client.lock().await;
Expand All @@ -177,11 +173,7 @@ impl PyCommunicator {
})
}

fn unlock<'a>(
&'a self,
py: Python<'a>,
key: String,
) -> PyResult<&'a PyAny> {
fn unlock<'a>(&'a self, py: Python<'a>, key: String) -> PyResult<&'a PyAny> {
let client = self.0.clone();
future_into_py(py, async move {
let mut client = client.lock().await;
Expand All @@ -190,11 +182,7 @@ impl PyCommunicator {
})
}

fn lease_grant<'a>(
&'a self,
py: Python<'a>,
ttl: i64,
) -> PyResult<&'a PyAny> {
fn lease_grant<'a>(&'a self, py: Python<'a>, ttl: i64) -> PyResult<&'a PyAny> {
let client = self.0.clone();
future_into_py(py, async move {
let mut client = client.lock().await;
Expand All @@ -203,11 +191,7 @@ impl PyCommunicator {
})
}

fn lease_revoke<'a>(
&'a self,
py: Python<'a>,
id: i64,
) -> PyResult<&'a PyAny> {
fn lease_revoke<'a>(&'a self, py: Python<'a>, id: i64) -> PyResult<&'a PyAny> {
let client = self.0.clone();
future_into_py(py, async move {
let mut client = client.lock().await;
Expand All @@ -216,11 +200,7 @@ impl PyCommunicator {
})
}

fn lease_time_to_live<'a>(
&'a self,
py: Python<'a>,
id: i64,
) -> PyResult<&'a PyAny> {
fn lease_time_to_live<'a>(&'a self, py: Python<'a>, id: i64) -> PyResult<&'a PyAny> {
let client = self.0.clone();
future_into_py(py, async move {
let mut client = client.lock().await;
Expand Down
8 changes: 8 additions & 0 deletions src/txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl PyTxnOp {
fn txn(txn: PyTxn) -> PyResult<Self> {
Ok(PyTxnOp(TxnOp::txn(txn.0)))
}

pub fn __repr__(&self) -> String {
format!("{:?}", self.0)
}
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -57,4 +61,8 @@ impl PyTxn {
let operations = operations.into_iter().map(|c| c.0).collect::<Vec<_>>();
Ok(PyTxn(self.0.clone().or_else(operations)))
}

pub fn __repr__(&self) -> String {
format!("{:?}", self.0)
}
}
4 changes: 4 additions & 0 deletions src/txn_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ impl PyTxnResponse {
pub fn succeeded(&self) -> PyResult<bool> {
Ok(self.0.succeeded())
}

pub fn __repr__(&self) -> String {
format!("{:?}", self.0)
}
}
2 changes: 1 addition & 1 deletion tests/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def __init__(
).check(scope_prefix_map)

if credentials is not None:
self._connect_options = ConnectOptions().with_username(
self._connect_options = ConnectOptions().with_user(
credentials["user"], credentials["password"]
)
else:
Expand Down

0 comments on commit 83990f0

Please sign in to comment.