Skip to content

Commit

Permalink
Update sync Python docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Oct 16, 2024
1 parent 480aec5 commit c688b26
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 123 deletions.
8 changes: 4 additions & 4 deletions packages/python-sdk/e2b/sandbox_async/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async def send_stdin(
:param pid Process ID of the command. You can get the list of processes using `sandbox.commands.list()`.
:param data: Data to send to the command
:param request_timeout: Request timeout
:param request_timeout: Timeout for the request in **seconds**
"""
try:
await self._rpc.asend_input(
Expand Down Expand Up @@ -184,7 +184,7 @@ async def run(
:param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
:param request_timeout: Timeout for the request in **seconds**
:return: `CommandHandle` handle to interact with the running command
:return: `AsyncCommandHandle` handle to interact with the running command
"""
...

Expand Down Expand Up @@ -271,15 +271,15 @@ async def connect(
) -> AsyncCommandHandle:
"""
Connects to a running command.
You can use `CommandHandle.wait()` to wait for the command to finish and get execution results.
You can use `AsyncCommandHandle.wait()` to wait for the command to finish and get execution results.
:param pid: Process ID of the command to connect to. You can get the list of processes using `sandbox.commands.list()`
:param request_timeout: Request timeout in **seconds**
:param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
:param on_stdout: Callback for command stdout output
:param on_stderr: Callback for command stderr output
:return: `CommandHandle` handle to interact with the running command
:return: `AsyncCommandHandle` handle to interact with the running command
"""
events = self._rpc.aconnect(
process_pb2.ConnectRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async def read(
:return: File content as a `bytearray`
"""
...

@overload
async def read(
Expand Down
2 changes: 1 addition & 1 deletion packages/python-sdk/e2b/sandbox_async/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AsyncSandbox(SandboxSetup, SandboxApi):
@property
def files(self) -> Filesystem:
"""
Module for interacting with the sandbox filesystem..
Module for interacting with the sandbox filesystem.
"""
return self._filesystem

Expand Down
89 changes: 57 additions & 32 deletions packages/python-sdk/e2b/sandbox_sync/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@


class Commands:
"""
Module for executing commands in the sandbox.
"""

def __init__(
self,
envd_api_url: str,
Expand All @@ -37,10 +41,11 @@ def list(
request_timeout: Optional[float] = None,
) -> List[ProcessInfo]:
"""
Lists all running processes.
Lists all running commands and PTY sessions.
:param request_timeout: Timeout for the request in **seconds**
:param request_timeout: Request timeout
:return: List of running processes
:return: List of running commands and PTY sessions
"""
try:
res = self._rpc.list(
Expand Down Expand Up @@ -69,11 +74,13 @@ def kill(
request_timeout: Optional[float] = None,
) -> bool:
"""
Kills a process.
Kills a running command specified by its process ID.
It uses `SIGKILL` signal to kill the command.
:param pid: Process ID to connect to. You can get the list of processes using `sandbox.commands.list()`.
:param request_timeout: Request timeout
:return: `True` if the process was killed, `False` if the process was not found
:param pid: Process ID of the command. You can get the list of processes using `sandbox.commands.list()`
:param request_timeout: Timeout for the request in **seconds**
:return: `True` if the command was killed, `False` if the command was not found
"""
try:
self._rpc.send_signal(
Expand All @@ -99,11 +106,11 @@ def send_stdin(
request_timeout: Optional[float] = None,
):
"""
Sends data to the stdin of a process.
Send data to command stdin.
:param pid Process ID to send data to. You can get the list of processes using `sandbox.commands.list()`.
:param data: Data to send to the process
:param request_timeout: Request timeout
:param pid Process ID of the command. You can get the list of processes using `sandbox.commands.list()`.
:param data: Data to send to the command
:param request_timeout: Timeout for the request in **seconds**
"""
try:
self._rpc.send_input(
Expand Down Expand Up @@ -132,7 +139,23 @@ def run(
on_stderr: Optional[Callable[[str], None]] = None,
timeout: Optional[float] = 60,
request_timeout: Optional[float] = None,
) -> CommandResult: ...
) -> CommandResult:
"""
Start a new command and wait until it finishes executing.
:param cmd: Command to execute
:param background: **`False` if the command should be executed in the foreground**, `True` if the command should be executed in the background
:param envs: Environment variables used for the command
:param user: User to run the command as
:param cwd: Working directory to run the command
:param on_stdout: Callback for command stdout output
:param on_stderr: Callback for command stderr output
:param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
:param request_timeout: Timeout for the request in **seconds**
:return: `CommandResult` result of the command execution
"""
...

@overload
def run(
Expand All @@ -146,7 +169,21 @@ def run(
on_stderr: None = None,
timeout: Optional[float] = 60,
request_timeout: Optional[float] = None,
) -> CommandHandle: ...
) -> CommandHandle:
"""
Start a new command and return a handle to interact with it.
:param cmd: Command to execute
:param background: `False` if the command should be executed in the foreground, **`True` if the command should be executed in the background**
:param envs: Environment variables used for the command
:param user: User to run the command as
:param cwd: Working directory to run the command
:param timeout: Timeout for the command connection in **seconds**. Using `0` will not limit the command connection time
:param request_timeout: Timeout for the request in **seconds**
:return: `CommandHandle` handle to interact with the running command
"""
...

def run(
self,
Expand All @@ -160,21 +197,6 @@ def run(
timeout: Optional[float] = 60,
request_timeout: Optional[float] = None,
):
"""
Starts a new process and depending on the `background` parameter, waits for the process to finish or not.
:param cmd Command to execute
:param background:
If `True`, the function will return a `ProcessHandle` object that can be used to interact with the process.
If `False`, the function will wait for the process to finish and return a `ProcessResult` object.
:param envs: Environment variables
:param user: User to run the process as
:param cwd: Working directory
:param on_stdout: Callback for stdout
:param on_stderr: Callback for stderr
:param timeout: Timeout for the maximum time the process is allowed to run
:param request_timeout: Timeout for the request
:return: `ProcessHandle` if `background` is `True`, `ProcessResult` if `background` is `False`
"""
proc = self._start(
cmd,
envs,
Expand Down Expand Up @@ -244,11 +266,14 @@ def connect(
request_timeout: Optional[float] = None,
):
"""
Connects to an existing process.
Connects to a running command.
You can use `CommandHandle.wait()` to wait for the command to finish and get execution results.
:param pid: Process ID of the command to connect to. You can get the list of processes using `sandbox.commands.list()`
:param timeout: Timeout for the connection in **seconds**. Using `0` will not limit the connection time
:param request_timeout: Timeout for the request in **seconds**
:param pid: Process ID to connect to. You can get the list of processes using `sandbox.commands.list()`.
:param timeout: Timeout for the connection
:param request_timeout: Request timeout
:return: `CommandHandle` handle to interact with the running command
"""
events = self._rpc.connect(
process_pb2.ConnectRequest(
Expand Down
31 changes: 22 additions & 9 deletions packages/python-sdk/e2b/sandbox_sync/commands/command_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

class CommandHandle:
"""
Class representing a process. It provides methods for waiting and killing the process.
It is also used to iterate over the process output.
Command execution handle.
It provides methods for waiting for the command to finish, retrieving stdout/stderr, and killing the command.
"""

@property
def pid(self):
"""
Get the process ID.
Command process ID.
"""
return self._pid

Expand All @@ -43,6 +44,11 @@ def __init__(
self._iteration_exception: Optional[Exception] = None

def __iter__(self):
"""
Iterate over the command output.
:return: Generator of command outputs
"""
return self._handle_events()

def _handle_events(
Expand Down Expand Up @@ -78,7 +84,10 @@ def _handle_events(

def disconnect(self) -> None:
"""
Disconnect from the process. It does not kill the process. It only stops receiving events from the process.
Disconnect from the command.
The command is not killed, but SDK stops receiving events from the command.
You can reconnect to the command using `sandbox.commands.connect` method.
"""
self._events.close()

Expand All @@ -89,13 +98,14 @@ def wait(
on_stderr: Optional[Callable[[str], None]] = None,
) -> CommandResult:
"""
Waits for the process to finish and returns the result.
If the process exits with a non-zero exit code, it throws a `CommandExitException`.
Wait for the command to finish and returns the result.
If the command exits with a non-zero exit code, it throws a `CommandExitException`.
:param on_pty: Callback for pty output
:param on_stdout: Callback for stdout output
:param on_stderr: Callback for stderr output
:return: Process result
:return: `CommandResult` result of command execution
"""
try:
for stdout, stderr, pty in self:
Expand Down Expand Up @@ -128,7 +138,10 @@ def wait(

def kill(self) -> bool:
"""
Kills the process.
:return: Whether the process was killed successfully
Kills the command.
It uses `SIGKILL` signal to kill the command.
:return: Whether the command was killed successfully
"""
return self._handle_kill()
Loading

0 comments on commit c688b26

Please sign in to comment.