Skip to content

Commit 99e4218

Browse files
committed
using pathlib.Path for paths instead of str
Signed-off-by: Valerie Gleason 👌 <[email protected]>
1 parent 5146df6 commit 99e4218

File tree

5 files changed

+63
-59
lines changed

5 files changed

+63
-59
lines changed

src/nipanel/_panel_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import grpc
99
from google.protobuf.any_pb2 import Any
1010
from ni.panels.v1.panel_service_pb2 import (
11-
StartPanelRequest,
12-
StopPanelRequest,
1311
EnumeratePanelsRequest,
1412
GetValueRequest,
15-
TryGetValueRequest,
1613
SetValueRequest,
14+
StartPanelRequest,
15+
StopPanelRequest,
16+
TryGetValueRequest,
1717
)
1818
from ni.panels.v1.panel_service_pb2_grpc import PanelServiceStub
1919
from ni.panels.v1.streamlit_panel_configuration_pb2 import StreamlitPanelConfiguration
@@ -49,11 +49,11 @@ def __init__(
4949
self._stub: PanelServiceStub | None = None
5050

5151
def start_streamlit_panel(
52-
self, panel_id: str, panel_script_path: str, python_interpreter_path: str
52+
self, panel_id: str, panel_script_path: pathlib.Path, python_interpreter_path: pathlib.Path
5353
) -> str:
5454

55-
panel_script_url = pathlib.Path(panel_script_path).absolute().as_uri()
56-
python_interpreter_url = pathlib.Path(python_interpreter_path).absolute().as_uri()
55+
panel_script_url = panel_script_path.absolute().as_uri()
56+
python_interpreter_url = python_interpreter_path.absolute().as_uri()
5757
streamlit_panel_configuration = StreamlitPanelConfiguration(
5858
panel_script_url=panel_script_url, python_interpreter_url=python_interpreter_url
5959
)

src/nipanel/_streamlit_panel.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class StreamlitPanel(PanelValueAccessor):
2020
def __init__(
2121
self,
2222
panel_id: str,
23-
panel_script_path: str,
23+
panel_script_path: Path,
2424
*,
2525
discovery_client: DiscoveryClient | None = None,
2626
grpc_channel_pool: GrpcChannelPool | None = None,
@@ -51,7 +51,7 @@ def __init__(
5151
)
5252

5353
@property
54-
def panel_script_path(self) -> str:
54+
def panel_script_path(self) -> Path:
5555
"""Read-only accessor for the streamlit script file path."""
5656
return self._panel_script_path
5757

@@ -60,7 +60,7 @@ def panel_url(self) -> str:
6060
"""Read-only accessor for the panel's streamlit webpage URL."""
6161
return self._panel_url
6262

63-
def _get_python_path(self) -> str:
63+
def _get_python_path(self) -> Path:
6464
"""Get the Python interpreter path for the panel that ensures the same environment."""
6565
if sys.executable is None or sys.executable == "":
6666
raise RuntimeError("Python environment not found")
@@ -78,16 +78,16 @@ def _get_python_path(self) -> str:
7878
bin_dir = "bin"
7979

8080
# Construct path to the Python in the virtual environment based on sys.prefix
81-
python_path = str(Path(sys.prefix) / bin_dir / python_executable)
81+
python_path = Path(sys.prefix) / bin_dir / python_executable
8282

8383
# Fall back to sys.executable if the constructed path doesn't exist
84-
if not Path(python_path).exists():
85-
python_path = str(Path(sys.executable).resolve())
84+
if not python_path.exists():
85+
python_path = Path(sys.executable).resolve()
8686
else:
8787
# If not in a .venv environment, use sys.executable
88-
python_path = str(Path(sys.executable).resolve())
88+
python_path = Path(sys.executable).resolve()
8989

90-
if sys.prefix not in python_path:
90+
if sys.prefix not in str(python_path):
9191
# Ensure the Python path is within the current environment
9292
raise RuntimeError(
9393
f"Python path '{python_path}' does not match the current environment prefix '{sys.prefix}'."

src/nipanel/_streamlit_panel_initializer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def create_streamlit_panel(streamlit_script_path: Path, panel_id: str = "") -> S
4343
if not panel_id:
4444
panel_id = streamlit_script_path.stem
4545

46-
path_str = str(streamlit_script_path)
47-
return StreamlitPanel(panel_id, path_str)
46+
return StreamlitPanel(panel_id, streamlit_script_path)
4847

4948

5049
def get_streamlit_panel_accessor() -> PanelValueAccessor:

tests/unit/test_panel_client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
import grpc
24
import pytest
35

@@ -13,8 +15,8 @@ def test___enumerate_is_empty(fake_panel_channel: grpc.Channel) -> None:
1315
def test___start_panels___enumerate_has_panels(fake_panel_channel: grpc.Channel) -> None:
1416
client = _PanelClient(grpc_channel=fake_panel_channel)
1517

16-
client.start_streamlit_panel("panel1", "uri1", "python.exe")
17-
client.start_streamlit_panel("panel2", "uri2", "python.exe")
18+
client.start_streamlit_panel("panel1", Path("uri1"), Path("python.exe"))
19+
client.start_streamlit_panel("panel2", Path("uri2"), Path("python.exe"))
1820

1921
assert client.enumerate_panels() == {
2022
"panel1": ("http://localhost:50051/panel1", []),
@@ -26,8 +28,8 @@ def test___start_panels___stop_panel_1_with_reset___enumerate_has_panel_2(
2628
fake_panel_channel: grpc.Channel,
2729
) -> None:
2830
client = _PanelClient(grpc_channel=fake_panel_channel)
29-
client.start_streamlit_panel("panel1", "uri1", "python.exe")
30-
client.start_streamlit_panel("panel2", "uri2", "python.exe")
31+
client.start_streamlit_panel("panel1", Path("uri1"), Path("python.exe"))
32+
client.start_streamlit_panel("panel2", Path("uri2"), Path("python.exe"))
3133

3234
client.stop_panel("panel1", reset=True)
3335

@@ -40,8 +42,8 @@ def test___start_panels___stop_panel_1_without_reset___enumerate_has_both_panels
4042
fake_panel_channel: grpc.Channel,
4143
) -> None:
4244
client = _PanelClient(grpc_channel=fake_panel_channel)
43-
client.start_streamlit_panel("panel1", "uri1", "python.exe")
44-
client.start_streamlit_panel("panel2", "uri2", "python.exe")
45+
client.start_streamlit_panel("panel1", Path("uri1"), Path("python.exe"))
46+
client.start_streamlit_panel("panel2", Path("uri2"), Path("python.exe"))
4547

4648
client.stop_panel("panel1", reset=False)
4749

0 commit comments

Comments
 (0)