Skip to content

Commit

Permalink
Shorter python client imports, fix windows docker client (#37)
Browse files Browse the repository at this point in the history
* Make remotebmi.RemoteBmiClient available

* Update shorter import in all docs

* Fix windows incompatibility in docker client

* os.name, not os.system
  • Loading branch information
BSchilperoort authored Nov 20, 2024
1 parent d4a4121 commit 6c79062
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion R/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ With Python client test the model

```python
import os
from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient
from remotebmi.reserve import reserve_values, reserve_grid_padding, reserve_grid_shape
import numpy as np

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pip install remotebmi
A client can connect to a running server with the following code.

```python
from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient

model = RemoteBmiClient('http://localhost:50051')
# Now you can use the BMI methods on model
Expand All @@ -53,7 +53,7 @@ model.get_value('var_name')
A client can also start a [Apptainer](https://apptainer.org) container containing the model and the server.

```python
from remotebmi.client.apptainer import BmiClientApptainer
from remotebmi import BmiClientApptainer

model = BmiClientApptainer('my_model.sif', work_dir='/tmp')
```
Expand All @@ -64,7 +64,7 @@ The port is passed to the container using the `BMI_PORT` environment variable.
A client can also start a [Docker](https://docs.docker.com/engine/) container containing the model and the server.

```python
from remotebmi.client.docker import BmiClientDocker
from remotebmi import BmiClientDocker

model = BmiClientDocker('ewatercycle/wflowjl:0.7.3', work_dir='/tmp')
```
Expand All @@ -90,7 +90,7 @@ BMI_MODULE=leakybucket.leakybucket_bmi BMI_CLASS=LeakyBucketBmi run-bmi-server
and the client can connect to it with the following code.

```python
> from remotebmi.client.client import RemoteBmiClient
> from remotebmi import RemoteBmiClient
> client = RemoteBmiClient('http://localhost:50051')
> client.get_component_name()
leakybucket
Expand Down
8 changes: 4 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pip install remotebmi
A client can connect to a running server with the following code.

```python
from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient

model = RemoteBmiClient('http://localhost:50051')
# Now you can use the BMI methods on model
Expand All @@ -36,7 +36,7 @@ model.get_value('var_name')
A client can also start a [Apptainer](https://apptainer.org) container containing the model and the server.

```python
from remotebmi.client.apptainer import BmiClientApptainer
from remotebmi import BmiClientApptainer

model = BmiClientApptainer('my_model.sif', work_dir='/tmp')
```
Expand All @@ -47,7 +47,7 @@ The port is passed to the container using the `BMI_PORT` environment variable.
A client can also start a [Docker](https://docs.docker.com/engine/) container containing the model and the server.

```python
from remotebmi.client.docker import BmiClientDocker
from remotebmi import BmiClientDocker

model = BmiClientDocker('ewatercycle/wflowjl:0.7.3', work_dir='/tmp')
```
Expand All @@ -73,7 +73,7 @@ BMI_MODULE=leakybucket.leakybucket_bmi BMI_CLASS=LeakyBucketBmi run-bmi-server
and the client can connect to it with the following code.

```python
> from remotebmi.client.client import RemoteBmiClient
> from remotebmi import RemoteBmiClient
> client = RemoteBmiClient('http://localhost:50051')
> client.get_component_name()
leakybucket
Expand Down
4 changes: 2 additions & 2 deletions julia/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ wget https://github.com/csdms/bmi-example-julia/raw/main/example/heat.toml
Interact with it using the Python client.

```python
from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient
from remotebmi.reserve import reserve_values
import os

Expand Down Expand Up @@ -109,7 +109,7 @@ run_bmi_server(Wflow.Model, "0.0.0.0", port)
Interact with it using the Python client.

```python
from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient
from remotebmi.reserve import reserve_values, reserve_grid_nodes, reserve_grid_edge_nodes, reserve_grid_nodes_per_face, reserve_grid_face_
import numpy as np
import os
Expand Down
8 changes: 4 additions & 4 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pip install remotebmi
A client can connect to a running server with the following code.

```python
from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient

model = RemoteBmiClient('http://localhost:50051')
# Now you can use the BMI methods on model
Expand All @@ -44,7 +44,7 @@ model.get_value('var_name')
A client can also start a [Apptainer](https://apptainer.org) container containing the model and the server.

```python
from remotebmi.client.apptainer import BmiClientApptainer
from remotebmi import BmiClientApptainer

model = BmiClientApptainer('my_model.sif', work_dir='/tmp')
```
Expand All @@ -55,7 +55,7 @@ The port is passed to the container using the `BMI_PORT` environment variable.
A client can also start a [Docker](https://docs.docker.com/engine/) container containing the model and the server.

```python
from remotebmi.client.docker import BmiClientDocker
from remotebmi import BmiClientDocker

model = BmiClientDocker('ewatercycle/wflowjl:0.7.3', work_dir='/tmp')
```
Expand All @@ -81,7 +81,7 @@ BMI_MODULE=leakybucket.leakybucket_bmi BMI_CLASS=LeakyBucketBmi run-bmi-server
and the client can connect to it with the following code.

```python
> from remotebmi.client.client import RemoteBmiClient
> from remotebmi import RemoteBmiClient
> client = RemoteBmiClient('http://localhost:50051')
> client.get_component_name()
leakybucket
Expand Down
8 changes: 4 additions & 4 deletions python/docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Python consumer

.. code-block:: python
from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient
model = RemoteBmiClient('http://localhost:50051')
# Now you can use the BMI methods on model
Expand All @@ -51,7 +51,7 @@ containing the model and the server:

.. code-block:: python
from remotebmi.client.apptainer import BmiClientApptainer
from remotebmi import BmiClientApptainer
model = BmiClientApptainer('my_model.sif', work_dir='/tmp')
Expand All @@ -63,7 +63,7 @@ containing the model and the server.

.. code-block:: python
from remotebmi.client.docker import BmiClientDocker
from remotebmi import BmiClientDocker
model = BmiClientDocker('ewatercycle/wflowjl:0.7.3', work_dir='/tmp')
Expand Down Expand Up @@ -92,7 +92,7 @@ and the Python client can connect to it with the following code.

.. code-block:: python
>>> from remotebmi.client.client import RemoteBmiClient
>>> from remotebmi import RemoteBmiClient
>>> client = RemoteBmiClient('http://localhost:50051')
>>> client.get_component_name()
Expand Down
5 changes: 5 additions & 0 deletions python/src/remotebmi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from remotebmi.client.apptainer import BmiClientApptainer
from remotebmi.client.client import RemoteBmiClient
from remotebmi.client.docker import BmiClientDocker

__all__ = ["RemoteBmiClient", "BmiClientApptainer", "BmiClientDocker"]
4 changes: 2 additions & 2 deletions python/src/remotebmi/client/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from docker.models.containers import Container # noqa: TCH002

from remotebmi.client.client import RemoteBmiClient
from remotebmi.client.utils import DeadContainerError, get_unique_port
from remotebmi.client.utils import DeadContainerError, get_unique_port, getuser


class BmiClientDocker(RemoteBmiClient):
Expand All @@ -18,7 +18,7 @@ def __init__(
image_port: int = 50051,
host: str | None = None,
input_dirs: Iterable[str] = (),
user: int = os.getuid(),
user: str | int = getuser(),
remove: bool = False,
delay: int = 5,
):
Expand Down
12 changes: 12 additions & 0 deletions python/src/remotebmi/client/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import getpass
import os
import socket
from contextlib import closing
from typing import Any
Expand Down Expand Up @@ -50,3 +52,13 @@ def validate_url(url: str) -> None:
if not (has_scheme and has_netloc and is_valid_scheme):
msg = f"Invalid: {url}, should be http(s)://host[:port][/path]"
raise ValueError(msg)


def getuser() -> int | str:
"""Windows-safe getuid implementation.
Will return user ID on unix/macOS. Will return username on Windows.
"""
if os.name == "nt":
return getpass.getuser()
return os.getuid()
2 changes: 1 addition & 1 deletion python/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
from numpy.testing import assert_array_equal

from remotebmi.client.client import RemoteBmiClient
from remotebmi import RemoteBmiClient
from remotebmi.reserve import (
reserve_grid_nodes,
reserve_grid_shape,
Expand Down

0 comments on commit 6c79062

Please sign in to comment.