Context-managed sandbox session bound to one sandbox id.
| 637 | |
| 638 | |
| 639 | class Sandbox: |
| 640 | """Context-managed sandbox session bound to one sandbox id.""" |
| 641 | |
| 642 | def __init__( |
| 643 | self, |
| 644 | *, |
| 645 | cluster: str | None = None, |
| 646 | sandbox: str | SandboxRef | None = None, |
| 647 | delete_on_exit: bool = True, |
| 648 | spec: openshell_pb2.SandboxSpec | None = None, |
| 649 | timeout: float = 30.0, |
| 650 | ready_timeout_seconds: float = 120.0, |
| 651 | auto_refresh: bool = True, |
| 652 | write_back: bool = True, |
| 653 | insecure: bool = False, |
| 654 | ) -> None: |
| 655 | """Bind a Sandbox context to the active gateway. |
| 656 | |
| 657 | OIDC kwargs (`auto_refresh`, `write_back`, `insecure`) forward |
| 658 | directly to `SandboxClient.from_active_cluster` and have the |
| 659 | same semantics. They're surfaced on `Sandbox` so callers using |
| 660 | the higher-level wrapper get parity with `SandboxClient` for |
| 661 | OIDC-protected gateways (e.g. passing `insecure=True` for a |
| 662 | self-signed dev IdP). Non-OIDC gateways ignore them. |
| 663 | """ |
| 664 | self._cluster = cluster |
| 665 | self._sandbox_input = sandbox |
| 666 | self._delete_on_exit = delete_on_exit |
| 667 | self._spec = spec |
| 668 | self._timeout = timeout |
| 669 | self._ready_timeout_seconds = ready_timeout_seconds |
| 670 | self._auto_refresh = auto_refresh |
| 671 | self._write_back = write_back |
| 672 | self._insecure = insecure |
| 673 | self._client: SandboxClient | None = None |
| 674 | self._session: SandboxSession | None = None |
| 675 | |
| 676 | @property |
| 677 | def id(self) -> str: |
| 678 | if self._session is None: |
| 679 | raise SandboxError("sandbox context has not been entered") |
| 680 | return self._session.id |
| 681 | |
| 682 | @property |
| 683 | def sandbox(self) -> SandboxRef: |
| 684 | if self._session is None: |
| 685 | raise SandboxError("sandbox context has not been entered") |
| 686 | return self._session.sandbox |
| 687 | |
| 688 | def __enter__(self) -> Sandbox: |
| 689 | client = SandboxClient.from_active_cluster( |
| 690 | cluster=self._cluster, |
| 691 | timeout=self._timeout, |
| 692 | auto_refresh=self._auto_refresh, |
| 693 | write_back=self._write_back, |
| 694 | insecure=self._insecure, |
| 695 | ) |
| 696 | self._client = client |
no outgoing calls