contree-sdk 0.4.0.dev5__tar.gz → 0.5.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.1}/.gitignore +2 -0
- contree_sdk-0.5.1/PKG-INFO +226 -0
- contree_sdk-0.5.1/README.md +179 -0
- contree_sdk-0.5.1/contree_sdk/__init__.py +39 -0
- contree_sdk-0.5.1/contree_sdk/compat.py +12 -0
- contree_sdk-0.5.1/contree_sdk/connection.py +165 -0
- contree_sdk-0.5.1/contree_sdk/docker/__init__.py +55 -0
- contree_sdk-0.5.1/contree_sdk/docker/asyncio.py +195 -0
- contree_sdk-0.5.1/contree_sdk/docker/context.py +548 -0
- contree_sdk-0.5.1/contree_sdk/docker/copy_layer.py +135 -0
- contree_sdk-0.5.1/contree_sdk/docker/copy_source.py +148 -0
- contree_sdk-0.5.1/contree_sdk/docker/dockerignore.py +94 -0
- contree_sdk-0.5.1/contree_sdk/docker/events.py +83 -0
- contree_sdk-0.5.1/contree_sdk/docker/interpolation.py +15 -0
- contree_sdk-0.5.1/contree_sdk/docker/keyword.py +45 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_add.py +275 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_arg.py +46 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_copy.py +417 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_env.py +37 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_from.py +185 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_run.py +156 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_skipped.py +30 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_user.py +34 -0
- contree_sdk-0.5.1/contree_sdk/docker/kw_workdir.py +45 -0
- contree_sdk-0.5.1/contree_sdk/docker/local_context.py +149 -0
- contree_sdk-0.5.1/contree_sdk/docker/parser.py +126 -0
- contree_sdk-0.5.1/contree_sdk/docker/sync.py +185 -0
- contree_sdk-0.5.1/contree_sdk/docker/url_fetch.py +118 -0
- contree_sdk-0.5.1/contree_sdk/exceptions.py +24 -0
- contree_sdk-0.5.1/contree_sdk/execution.py +160 -0
- contree_sdk-0.5.1/contree_sdk/file_io.py +131 -0
- contree_sdk-0.5.1/contree_sdk/file_sources.py +144 -0
- contree_sdk-0.5.1/contree_sdk/files.py +327 -0
- contree_sdk-0.5.1/contree_sdk/harbor/__init__.py +6 -0
- contree_sdk-0.5.1/contree_sdk/harbor/environment.py +279 -0
- contree_sdk-0.5.1/contree_sdk/images.py +406 -0
- contree_sdk-0.5.1/contree_sdk/inspection.py +251 -0
- contree_sdk-0.5.1/contree_sdk/langchain/__init__.py +4 -0
- contree_sdk-0.5.1/contree_sdk/langchain/sandbox.py +166 -0
- contree_sdk-0.5.1/contree_sdk/reuse.py +92 -0
- contree_sdk-0.5.1/contree_sdk/sandbox.py +500 -0
- contree_sdk-0.5.1/contree_sdk/scope.py +30 -0
- contree_sdk-0.5.1/contree_sdk/session/__init__.py +58 -0
- contree_sdk-0.5.1/contree_sdk/session/asyncio.py +956 -0
- contree_sdk-0.5.1/contree_sdk/session/base.py +93 -0
- contree_sdk-0.5.1/contree_sdk/session/cleanup.py +83 -0
- contree_sdk-0.5.1/contree_sdk/session/commit_policy.py +34 -0
- contree_sdk-0.5.1/contree_sdk/session/contracts.py +204 -0
- contree_sdk-0.5.1/contree_sdk/session/detached.py +71 -0
- contree_sdk-0.5.1/contree_sdk/session/inspection.py +299 -0
- contree_sdk-0.5.1/contree_sdk/session/lazy_async.py +664 -0
- contree_sdk-0.5.1/contree_sdk/session/lazy_state.py +97 -0
- contree_sdk-0.5.1/contree_sdk/session/lazy_sync.py +636 -0
- contree_sdk-0.5.1/contree_sdk/session/live_files.py +53 -0
- contree_sdk-0.5.1/contree_sdk/session/models.py +26 -0
- contree_sdk-0.5.1/contree_sdk/session/operation_async.py +521 -0
- contree_sdk-0.5.1/contree_sdk/session/operation_sync.py +476 -0
- contree_sdk-0.5.1/contree_sdk/session/records.py +71 -0
- contree_sdk-0.5.1/contree_sdk/session/snapshot_policy.py +266 -0
- contree_sdk-0.5.1/contree_sdk/session/stdin.py +39 -0
- contree_sdk-0.5.1/contree_sdk/session/subprocess_result.py +157 -0
- contree_sdk-0.5.1/contree_sdk/session/sync.py +867 -0
- contree_sdk-0.5.1/contree_sdk/session/tree.py +91 -0
- contree_sdk-0.5.1/contree_sdk/store/__init__.py +21 -0
- contree_sdk-0.5.1/contree_sdk/store/base.py +156 -0
- contree_sdk-0.5.1/contree_sdk/store/memory.py +120 -0
- contree_sdk-0.5.1/contree_sdk/store/sqlite.py +477 -0
- contree_sdk-0.5.1/contree_sdk/store/tree.py +101 -0
- contree_sdk-0.5.1/contree_sdk/store/ttl.py +18 -0
- contree_sdk-0.5.1/contree_sdk/upload_cache.py +206 -0
- contree_sdk-0.5.1/contree_sdk/utils/models/file.py +140 -0
- {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.1}/pyproject.toml +29 -37
- contree_sdk-0.4.0.dev5/PKG-INFO +0 -590
- contree_sdk-0.4.0.dev5/README.md +0 -524
- contree_sdk-0.4.0.dev5/contree_sdk/__init__.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/README.md +0 -17
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/client/provider.py +0 -138
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/client/transport.py +0 -124
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/codecs.py +0 -28
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/operation_waiter.py +0 -289
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/typing.py +0 -66
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/wiring.py +0 -107
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/io/writer_wrapper.py +0 -85
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/lib/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/lib/helpers.py +0 -33
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/auth_ini.py +0 -51
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/circuit_retrier.py +0 -224
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/config.py +0 -34
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/deprecation.py +0 -58
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/exception.py +0 -72
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/other.py +0 -15
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/typing.py +0 -30
- contree_sdk-0.4.0.dev5/contree_sdk/_internals/utils/wrapper.py +0 -140
- contree_sdk-0.4.0.dev5/contree_sdk/auth.py +0 -72
- contree_sdk-0.4.0.dev5/contree_sdk/config.py +0 -54
- contree_sdk-0.4.0.dev5/contree_sdk/langchain/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/langchain/sandbox.py +0 -76
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/client/__init__.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/client/_async.py +0 -42
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/client/_base.py +0 -196
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/client/_sync.py +0 -45
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/__init__.py +0 -53
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/api.py +0 -77
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/base.py +0 -16
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/image.py +0 -44
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/operation.py +0 -27
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/exceptions/other.py +0 -8
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/_base.py +0 -6
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/files/__init__.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/files/_async.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/files/_base.py +0 -40
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/files/_sync.py +0 -10
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/images/__init__.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/images/_async.py +0 -46
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/images/_base.py +0 -328
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/managers/images/_sync.py +0 -46
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image/__init__.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image/_async.py +0 -8
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image/_base.py +0 -4
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image/_sync.py +0 -8
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_fs/__init__.py +0 -10
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_fs/_async.py +0 -21
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_fs/_base.py +0 -40
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_fs/_sync.py +0 -22
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/_async.py +0 -115
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/_base.py +0 -500
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/_sync.py +0 -184
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/result.py +0 -40
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/image_like/state.py +0 -81
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/run.py +0 -43
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/session/__init__.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/session/_async.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/session/_base.py +0 -16
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/session/_sync.py +0 -5
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/subprocess/__init__.py +0 -6
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/subprocess/_async.py +0 -24
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/subprocess/_base.py +0 -72
- contree_sdk-0.4.0.dev5/contree_sdk/sdk/objects/subprocess/_sync.py +0 -29
- contree_sdk-0.4.0.dev5/contree_sdk/shell/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/utils/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/utils/io.py +0 -26
- contree_sdk-0.4.0.dev5/contree_sdk/utils/models/__init__.py +0 -0
- contree_sdk-0.4.0.dev5/contree_sdk/utils/models/auth.py +0 -10
- contree_sdk-0.4.0.dev5/contree_sdk/utils/models/file.py +0 -62
- contree_sdk-0.4.0.dev5/contree_sdk/utils/models/image.py +0 -6
- contree_sdk-0.4.0.dev5/contree_sdk/utils/models/operation.py +0 -26
- contree_sdk-0.4.0.dev5/contree_sdk/utils/models/stream.py +0 -16
- contree_sdk-0.4.0.dev5/contree_sdk/utils/oci.py +0 -49
- {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.1}/LICENSE +0 -0
- {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.1}/SECURITY.md +0 -0
- {contree_sdk-0.4.0.dev5/contree_sdk/_internals → contree_sdk-0.5.1/contree_sdk/shell}/__init__.py +0 -0
- {contree_sdk-0.4.0.dev5 → contree_sdk-0.5.1}/contree_sdk/shell/__main__.py +0 -0
- {contree_sdk-0.4.0.dev5/contree_sdk/_internals/client → contree_sdk-0.5.1/contree_sdk/utils}/__init__.py +0 -0
- {contree_sdk-0.4.0.dev5/contree_sdk/_internals/io → contree_sdk-0.5.1/contree_sdk/utils/models}/__init__.py +0 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: contree-sdk
|
|
3
|
+
Version: 0.5.1
|
|
4
|
+
Summary: SDK for ConTree container runtime with versioned filesystem state
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
8
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Requires-Python: <3.15,>=3.10
|
|
16
|
+
Requires-Dist: contree-client[httpx]>=0.5.3
|
|
17
|
+
Requires-Dist: typing-extensions; python_version < '3.11'
|
|
18
|
+
Provides-Extra: async
|
|
19
|
+
Requires-Dist: aiosqlite>=0.20; extra == 'async'
|
|
20
|
+
Requires-Dist: contree-client[aiohttp]; extra == 'async'
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: aiosqlite>=0.20; extra == 'dev'
|
|
23
|
+
Requires-Dist: contree-client[aiohttp]; extra == 'dev'
|
|
24
|
+
Requires-Dist: deepagents==0.7.10; (python_version >= '3.11') and extra == 'dev'
|
|
25
|
+
Requires-Dist: dprint-py; extra == 'dev'
|
|
26
|
+
Requires-Dist: langchain-tests>=1.1.9; (python_version >= '3.11') and extra == 'dev'
|
|
27
|
+
Requires-Dist: markdown-pytest[async]>=0.6.6; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest-asyncio; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest-html; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest-mock; extra == 'dev'
|
|
32
|
+
Requires-Dist: pytest-xdist; extra == 'dev'
|
|
33
|
+
Requires-Dist: pytest>=8.4.1; extra == 'dev'
|
|
34
|
+
Requires-Dist: rich; extra == 'dev'
|
|
35
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
36
|
+
Requires-Dist: tomli; (python_version < '3.11') and extra == 'dev'
|
|
37
|
+
Requires-Dist: typing-extensions; extra == 'dev'
|
|
38
|
+
Requires-Dist: ty~=0.0.51; extra == 'dev'
|
|
39
|
+
Provides-Extra: docs
|
|
40
|
+
Requires-Dist: contree-docs; extra == 'docs'
|
|
41
|
+
Provides-Extra: harbor
|
|
42
|
+
Requires-Dist: aiosqlite>=0.20; (python_version >= '3.12') and extra == 'harbor'
|
|
43
|
+
Requires-Dist: harbor<0.23,>=0.22; (python_version >= '3.12') and extra == 'harbor'
|
|
44
|
+
Provides-Extra: langchain
|
|
45
|
+
Requires-Dist: deepagents==0.7.10; (python_version >= '3.11') and extra == 'langchain'
|
|
46
|
+
Description-Content-Type: text/markdown
|
|
47
|
+
|
|
48
|
+
# ConTree SDK
|
|
49
|
+
|
|
50
|
+
[](https://pypi.org/project/contree-sdk/)
|
|
51
|
+
[](https://pypi.org/project/contree-sdk/)
|
|
52
|
+
|
|
53
|
+
Use one sandbox session to run commands, read and write files, search contents,
|
|
54
|
+
and list directories. Save the filesystem as an image when you need to continue
|
|
55
|
+
later or try a different approach.
|
|
56
|
+
|
|
57
|
+
This checkout contains the breaking **0.5 development API**. Install it locally:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install -e .
|
|
61
|
+
# Include async transport and SQLite support when needed:
|
|
62
|
+
pip install -e ".[async]"
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Use a sandbox session
|
|
66
|
+
|
|
67
|
+
The five tools are `run`, `read_file`, `write_file`, `grep`, and `list_dir`.
|
|
68
|
+
Initialize the session [from a token](docs/python_sdk/getting-started.md#create-a-session-from-a-token)
|
|
69
|
+
or [from a saved profile](docs/python_sdk/getting-started.md#create-a-session-from-a-saved-profile).
|
|
70
|
+
Use an image available on your endpoint.
|
|
71
|
+
|
|
72
|
+
### Sync
|
|
73
|
+
|
|
74
|
+
<!-- name: test_readme; fixtures: sandbox_tools_api, capsys -->
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
"""Use the five sandbox tools in one session."""
|
|
78
|
+
|
|
79
|
+
from contree_sdk import create_sandbox_session
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def main() -> None:
|
|
83
|
+
with create_sandbox_session(image="tag:alpine:latest") as sandbox_session:
|
|
84
|
+
# Write a file in the sandbox.
|
|
85
|
+
sandbox_session.write_file("/work/message.txt", b"Hello from ConTree!\n")
|
|
86
|
+
|
|
87
|
+
# Run a command and print its output.
|
|
88
|
+
result = sandbox_session.run("cat", args=["/work/message.txt"])
|
|
89
|
+
print(result.stdout)
|
|
90
|
+
|
|
91
|
+
# Read the file as bytes.
|
|
92
|
+
content = sandbox_session.read_file("/work/message.txt")
|
|
93
|
+
print(content.decode())
|
|
94
|
+
|
|
95
|
+
# Search file contents.
|
|
96
|
+
matches = sandbox_session.grep("Hello", path="/work/message.txt")
|
|
97
|
+
print(matches)
|
|
98
|
+
|
|
99
|
+
# List the directory with file metadata.
|
|
100
|
+
directory = sandbox_session.list_dir("/work")
|
|
101
|
+
print(directory)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
main()
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
<!--
|
|
108
|
+
name: test_readme
|
|
109
|
+
```python
|
|
110
|
+
assert (
|
|
111
|
+
capsys.readouterr().out
|
|
112
|
+
== "Hello from ConTree!\n\nHello from ConTree!\n\n/work/message.txt:1:Hello from ConTree!\n\n-rw-r--r-- 1 root root 20 1970-01-01 00:00 message.txt\n\n"
|
|
113
|
+
)
|
|
114
|
+
client = sandbox_tools_api.sync
|
|
115
|
+
assert len(client.calls_for("spawn_instance")) == 1
|
|
116
|
+
assert len(client.calls_for("operation_subprocess_create")) == 2
|
|
117
|
+
assert client.calls_for("inspect_instance_download")[0].args == ("op-1", "/work/message.txt")
|
|
118
|
+
assert client.calls_for("inspect_instance_grep")[0].kwargs["path"] == "/work/message.txt"
|
|
119
|
+
assert client.calls_for("inspect_instance_list")[0].args == ("op-1", "/work")
|
|
120
|
+
assert len(client.calls_for("operation_subprocess_kill")) == 1
|
|
121
|
+
```
|
|
122
|
+
-->
|
|
123
|
+
|
|
124
|
+
### Async
|
|
125
|
+
|
|
126
|
+
<!-- name: test_readme_async; fixtures: sandbox_tools_api, capsys -->
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
import asyncio
|
|
130
|
+
|
|
131
|
+
from contree_sdk import create_sandbox_session
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
async def main() -> None:
|
|
135
|
+
async with create_sandbox_session(image="tag:alpine:latest") as sandbox_session:
|
|
136
|
+
# Write a file in the sandbox.
|
|
137
|
+
await sandbox_session.write_file("/work/message.txt", b"Hello from ConTree!\n")
|
|
138
|
+
|
|
139
|
+
# Run a command and print its output.
|
|
140
|
+
result = await sandbox_session.run("cat", args=["/work/message.txt"])
|
|
141
|
+
print(result.stdout)
|
|
142
|
+
|
|
143
|
+
# Read the file as bytes.
|
|
144
|
+
content = await sandbox_session.read_file("/work/message.txt")
|
|
145
|
+
print(content.decode())
|
|
146
|
+
|
|
147
|
+
# Search file contents.
|
|
148
|
+
matches = await sandbox_session.grep("Hello", path="/work/message.txt")
|
|
149
|
+
print(matches)
|
|
150
|
+
|
|
151
|
+
# List the directory with file metadata.
|
|
152
|
+
directory = await sandbox_session.list_dir("/work")
|
|
153
|
+
print(directory)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
asyncio.run(main())
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
<!--
|
|
160
|
+
name: test_readme_async
|
|
161
|
+
```python
|
|
162
|
+
assert (
|
|
163
|
+
capsys.readouterr().out
|
|
164
|
+
== "Hello from ConTree!\n\nHello from ConTree!\n\n/work/message.txt:1:Hello from ConTree!\n\n-rw-r--r-- 1 root root 20 1970-01-01 00:00 message.txt\n\n"
|
|
165
|
+
)
|
|
166
|
+
client = sandbox_tools_api.async_client
|
|
167
|
+
assert len(client.calls_for("spawn_instance")) == 1
|
|
168
|
+
assert len(client.calls_for("operation_subprocess_create")) == 2
|
|
169
|
+
assert client.calls_for("inspect_instance_download")[0].args == ("op-1", "/work/message.txt")
|
|
170
|
+
assert client.calls_for("inspect_instance_grep")[0].kwargs["path"] == "/work/message.txt"
|
|
171
|
+
assert client.calls_for("inspect_instance_list")[0].args == ("op-1", "/work")
|
|
172
|
+
assert len(client.calls_for("operation_subprocess_kill")) == 1
|
|
173
|
+
```
|
|
174
|
+
-->
|
|
175
|
+
|
|
176
|
+
The command and file read both print `Hello from ConTree!`. The search prints
|
|
177
|
+
`/work/message.txt:1:Hello from ConTree!`, and the listing shows metadata for `message.txt`.
|
|
178
|
+
|
|
179
|
+
The default `LazySession` (`AsyncLazySession` for async code) starts a VM on the
|
|
180
|
+
first command and reuses it for later commands. After 10 idle seconds, it saves
|
|
181
|
+
the filesystem and stops the VM automatically. The next command starts a new VM
|
|
182
|
+
from those saved files. Processes and memory do not survive a snapshot.
|
|
183
|
+
Normal context exit also saves pending changes.
|
|
184
|
+
|
|
185
|
+
To save after each command, select `session_type=ContreeSession` for Sync or
|
|
186
|
+
`session_type=ContreeAsyncSession` for Async. Each command then uses a separate
|
|
187
|
+
VM. See the complete [save-after-every-command examples](docs/python_sdk/getting-started.md#save-after-every-command).
|
|
188
|
+
|
|
189
|
+
## Follow a workflow
|
|
190
|
+
|
|
191
|
+
| Task | Guide |
|
|
192
|
+
| ------------------------------------------------ | ---------------------------------------------------------- |
|
|
193
|
+
| Configure credentials and use sync or async code | [Your first sandbox](docs/python_sdk/getting-started.md) |
|
|
194
|
+
| Import a Docker image | [Import a Docker image](docs/python_sdk/images.md) |
|
|
195
|
+
| Build from a Dockerfile | [Build a Docker image](docs/python_sdk/building-images.md) |
|
|
196
|
+
| Select components and control their behavior | [Advanced usage](docs/python_sdk/advanced.md) |
|
|
197
|
+
| Upload inputs and download results | [Files](docs/python_sdk/files.md) |
|
|
198
|
+
| Continue after restarting Python | [Save and resume](docs/python_sdk/sessions.md) |
|
|
199
|
+
| Try alternatives from a checkpoint | [Branches](docs/python_sdk/branching.md) |
|
|
200
|
+
| Control a running sandbox | [Operations](docs/python_sdk/operation.md) |
|
|
201
|
+
| Connect deepagents | [Agent integration](docs/integrations/langchain.md) |
|
|
202
|
+
| Override policy or replace components | [Customization](docs/python_sdk/customization.md) |
|
|
203
|
+
| Upgrade from the old SDK | [Migration](docs/python_sdk/migration.md) |
|
|
204
|
+
|
|
205
|
+
The [documentation](https://nebius.github.io/contree-sdk/) includes API references and
|
|
206
|
+
failure-handling examples. The mini-swe-agent 2.4.6 bundled adapter needs a port
|
|
207
|
+
before it can use this API; see the [compatibility note](docs/integrations/mini-swe-agent.md).
|
|
208
|
+
|
|
209
|
+
## Develop and test
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
uv sync --extra dev --extra docs
|
|
213
|
+
uv run pytest
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
The regular test run executes the Python scenarios in README and `docs/` through
|
|
217
|
+
[markdown-pytest](https://mosquito.github.io/markdown-pytest/). The SDK and stores run
|
|
218
|
+
normally; fixtures supply transport responses. No server credentials are required.
|
|
219
|
+
See [contributing](docs/contributing.md) for checks and how to add a scenario.
|
|
220
|
+
|
|
221
|
+
Licensed under [Apache 2.0](LICENSE). Report vulnerabilities through the process
|
|
222
|
+
in [SECURITY.md](SECURITY.md).
|
|
223
|
+
|
|
224
|
+
Standalone programs with embedded tests are in [`examples/sync/`](examples/sync/)
|
|
225
|
+
and [`examples/async/`](examples/async/). See the [examples guide](examples/README.md)
|
|
226
|
+
for prerequisites and execution commands.
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# ConTree SDK
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/contree-sdk/)
|
|
4
|
+
[](https://pypi.org/project/contree-sdk/)
|
|
5
|
+
|
|
6
|
+
Use one sandbox session to run commands, read and write files, search contents,
|
|
7
|
+
and list directories. Save the filesystem as an image when you need to continue
|
|
8
|
+
later or try a different approach.
|
|
9
|
+
|
|
10
|
+
This checkout contains the breaking **0.5 development API**. Install it locally:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install -e .
|
|
14
|
+
# Include async transport and SQLite support when needed:
|
|
15
|
+
pip install -e ".[async]"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Use a sandbox session
|
|
19
|
+
|
|
20
|
+
The five tools are `run`, `read_file`, `write_file`, `grep`, and `list_dir`.
|
|
21
|
+
Initialize the session [from a token](docs/python_sdk/getting-started.md#create-a-session-from-a-token)
|
|
22
|
+
or [from a saved profile](docs/python_sdk/getting-started.md#create-a-session-from-a-saved-profile).
|
|
23
|
+
Use an image available on your endpoint.
|
|
24
|
+
|
|
25
|
+
### Sync
|
|
26
|
+
|
|
27
|
+
<!-- name: test_readme; fixtures: sandbox_tools_api, capsys -->
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
"""Use the five sandbox tools in one session."""
|
|
31
|
+
|
|
32
|
+
from contree_sdk import create_sandbox_session
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def main() -> None:
|
|
36
|
+
with create_sandbox_session(image="tag:alpine:latest") as sandbox_session:
|
|
37
|
+
# Write a file in the sandbox.
|
|
38
|
+
sandbox_session.write_file("/work/message.txt", b"Hello from ConTree!\n")
|
|
39
|
+
|
|
40
|
+
# Run a command and print its output.
|
|
41
|
+
result = sandbox_session.run("cat", args=["/work/message.txt"])
|
|
42
|
+
print(result.stdout)
|
|
43
|
+
|
|
44
|
+
# Read the file as bytes.
|
|
45
|
+
content = sandbox_session.read_file("/work/message.txt")
|
|
46
|
+
print(content.decode())
|
|
47
|
+
|
|
48
|
+
# Search file contents.
|
|
49
|
+
matches = sandbox_session.grep("Hello", path="/work/message.txt")
|
|
50
|
+
print(matches)
|
|
51
|
+
|
|
52
|
+
# List the directory with file metadata.
|
|
53
|
+
directory = sandbox_session.list_dir("/work")
|
|
54
|
+
print(directory)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
main()
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
<!--
|
|
61
|
+
name: test_readme
|
|
62
|
+
```python
|
|
63
|
+
assert (
|
|
64
|
+
capsys.readouterr().out
|
|
65
|
+
== "Hello from ConTree!\n\nHello from ConTree!\n\n/work/message.txt:1:Hello from ConTree!\n\n-rw-r--r-- 1 root root 20 1970-01-01 00:00 message.txt\n\n"
|
|
66
|
+
)
|
|
67
|
+
client = sandbox_tools_api.sync
|
|
68
|
+
assert len(client.calls_for("spawn_instance")) == 1
|
|
69
|
+
assert len(client.calls_for("operation_subprocess_create")) == 2
|
|
70
|
+
assert client.calls_for("inspect_instance_download")[0].args == ("op-1", "/work/message.txt")
|
|
71
|
+
assert client.calls_for("inspect_instance_grep")[0].kwargs["path"] == "/work/message.txt"
|
|
72
|
+
assert client.calls_for("inspect_instance_list")[0].args == ("op-1", "/work")
|
|
73
|
+
assert len(client.calls_for("operation_subprocess_kill")) == 1
|
|
74
|
+
```
|
|
75
|
+
-->
|
|
76
|
+
|
|
77
|
+
### Async
|
|
78
|
+
|
|
79
|
+
<!-- name: test_readme_async; fixtures: sandbox_tools_api, capsys -->
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
import asyncio
|
|
83
|
+
|
|
84
|
+
from contree_sdk import create_sandbox_session
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
async def main() -> None:
|
|
88
|
+
async with create_sandbox_session(image="tag:alpine:latest") as sandbox_session:
|
|
89
|
+
# Write a file in the sandbox.
|
|
90
|
+
await sandbox_session.write_file("/work/message.txt", b"Hello from ConTree!\n")
|
|
91
|
+
|
|
92
|
+
# Run a command and print its output.
|
|
93
|
+
result = await sandbox_session.run("cat", args=["/work/message.txt"])
|
|
94
|
+
print(result.stdout)
|
|
95
|
+
|
|
96
|
+
# Read the file as bytes.
|
|
97
|
+
content = await sandbox_session.read_file("/work/message.txt")
|
|
98
|
+
print(content.decode())
|
|
99
|
+
|
|
100
|
+
# Search file contents.
|
|
101
|
+
matches = await sandbox_session.grep("Hello", path="/work/message.txt")
|
|
102
|
+
print(matches)
|
|
103
|
+
|
|
104
|
+
# List the directory with file metadata.
|
|
105
|
+
directory = await sandbox_session.list_dir("/work")
|
|
106
|
+
print(directory)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
asyncio.run(main())
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
<!--
|
|
113
|
+
name: test_readme_async
|
|
114
|
+
```python
|
|
115
|
+
assert (
|
|
116
|
+
capsys.readouterr().out
|
|
117
|
+
== "Hello from ConTree!\n\nHello from ConTree!\n\n/work/message.txt:1:Hello from ConTree!\n\n-rw-r--r-- 1 root root 20 1970-01-01 00:00 message.txt\n\n"
|
|
118
|
+
)
|
|
119
|
+
client = sandbox_tools_api.async_client
|
|
120
|
+
assert len(client.calls_for("spawn_instance")) == 1
|
|
121
|
+
assert len(client.calls_for("operation_subprocess_create")) == 2
|
|
122
|
+
assert client.calls_for("inspect_instance_download")[0].args == ("op-1", "/work/message.txt")
|
|
123
|
+
assert client.calls_for("inspect_instance_grep")[0].kwargs["path"] == "/work/message.txt"
|
|
124
|
+
assert client.calls_for("inspect_instance_list")[0].args == ("op-1", "/work")
|
|
125
|
+
assert len(client.calls_for("operation_subprocess_kill")) == 1
|
|
126
|
+
```
|
|
127
|
+
-->
|
|
128
|
+
|
|
129
|
+
The command and file read both print `Hello from ConTree!`. The search prints
|
|
130
|
+
`/work/message.txt:1:Hello from ConTree!`, and the listing shows metadata for `message.txt`.
|
|
131
|
+
|
|
132
|
+
The default `LazySession` (`AsyncLazySession` for async code) starts a VM on the
|
|
133
|
+
first command and reuses it for later commands. After 10 idle seconds, it saves
|
|
134
|
+
the filesystem and stops the VM automatically. The next command starts a new VM
|
|
135
|
+
from those saved files. Processes and memory do not survive a snapshot.
|
|
136
|
+
Normal context exit also saves pending changes.
|
|
137
|
+
|
|
138
|
+
To save after each command, select `session_type=ContreeSession` for Sync or
|
|
139
|
+
`session_type=ContreeAsyncSession` for Async. Each command then uses a separate
|
|
140
|
+
VM. See the complete [save-after-every-command examples](docs/python_sdk/getting-started.md#save-after-every-command).
|
|
141
|
+
|
|
142
|
+
## Follow a workflow
|
|
143
|
+
|
|
144
|
+
| Task | Guide |
|
|
145
|
+
| ------------------------------------------------ | ---------------------------------------------------------- |
|
|
146
|
+
| Configure credentials and use sync or async code | [Your first sandbox](docs/python_sdk/getting-started.md) |
|
|
147
|
+
| Import a Docker image | [Import a Docker image](docs/python_sdk/images.md) |
|
|
148
|
+
| Build from a Dockerfile | [Build a Docker image](docs/python_sdk/building-images.md) |
|
|
149
|
+
| Select components and control their behavior | [Advanced usage](docs/python_sdk/advanced.md) |
|
|
150
|
+
| Upload inputs and download results | [Files](docs/python_sdk/files.md) |
|
|
151
|
+
| Continue after restarting Python | [Save and resume](docs/python_sdk/sessions.md) |
|
|
152
|
+
| Try alternatives from a checkpoint | [Branches](docs/python_sdk/branching.md) |
|
|
153
|
+
| Control a running sandbox | [Operations](docs/python_sdk/operation.md) |
|
|
154
|
+
| Connect deepagents | [Agent integration](docs/integrations/langchain.md) |
|
|
155
|
+
| Override policy or replace components | [Customization](docs/python_sdk/customization.md) |
|
|
156
|
+
| Upgrade from the old SDK | [Migration](docs/python_sdk/migration.md) |
|
|
157
|
+
|
|
158
|
+
The [documentation](https://nebius.github.io/contree-sdk/) includes API references and
|
|
159
|
+
failure-handling examples. The mini-swe-agent 2.4.6 bundled adapter needs a port
|
|
160
|
+
before it can use this API; see the [compatibility note](docs/integrations/mini-swe-agent.md).
|
|
161
|
+
|
|
162
|
+
## Develop and test
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
uv sync --extra dev --extra docs
|
|
166
|
+
uv run pytest
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
The regular test run executes the Python scenarios in README and `docs/` through
|
|
170
|
+
[markdown-pytest](https://mosquito.github.io/markdown-pytest/). The SDK and stores run
|
|
171
|
+
normally; fixtures supply transport responses. No server credentials are required.
|
|
172
|
+
See [contributing](docs/contributing.md) for checks and how to add a scenario.
|
|
173
|
+
|
|
174
|
+
Licensed under [Apache 2.0](LICENSE). Report vulnerabilities through the process
|
|
175
|
+
in [SECURITY.md](SECURITY.md).
|
|
176
|
+
|
|
177
|
+
Standalone programs with embedded tests are in [`examples/sync/`](examples/sync/)
|
|
178
|
+
and [`examples/async/`](examples/async/). See the [examples guide](examples/README.md)
|
|
179
|
+
for prerequisites and execution commands.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from contree_sdk.connection import profile_from_args
|
|
2
|
+
from contree_sdk.execution import AsyncExecutor, OperationContext, RunRequest, SyncExecutor
|
|
3
|
+
from contree_sdk.images import async_build_image, async_import_image, sync_build_image, sync_import_image
|
|
4
|
+
from contree_sdk.inspection import AsyncImageInspector, ImageInspector
|
|
5
|
+
from contree_sdk.sandbox import (
|
|
6
|
+
SandboxSessionContext,
|
|
7
|
+
create_async_sandbox_session,
|
|
8
|
+
create_sandbox_session,
|
|
9
|
+
create_sync_sandbox_session,
|
|
10
|
+
session_maker,
|
|
11
|
+
)
|
|
12
|
+
from contree_sdk.session import AsyncLazySession, ContreeAsyncSession, ContreeSession, LazySession
|
|
13
|
+
from contree_sdk.session.inspection import AsyncSessionInspector, SessionInspector
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"AsyncExecutor",
|
|
18
|
+
"AsyncImageInspector",
|
|
19
|
+
"AsyncLazySession",
|
|
20
|
+
"AsyncSessionInspector",
|
|
21
|
+
"ContreeAsyncSession",
|
|
22
|
+
"ContreeSession",
|
|
23
|
+
"ImageInspector",
|
|
24
|
+
"LazySession",
|
|
25
|
+
"OperationContext",
|
|
26
|
+
"RunRequest",
|
|
27
|
+
"SandboxSessionContext",
|
|
28
|
+
"SessionInspector",
|
|
29
|
+
"SyncExecutor",
|
|
30
|
+
"async_build_image",
|
|
31
|
+
"async_import_image",
|
|
32
|
+
"create_async_sandbox_session",
|
|
33
|
+
"create_sandbox_session",
|
|
34
|
+
"create_sync_sandbox_session",
|
|
35
|
+
"profile_from_args",
|
|
36
|
+
"session_maker",
|
|
37
|
+
"sync_build_image",
|
|
38
|
+
"sync_import_image",
|
|
39
|
+
]
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"""Shared connection resolution and ownership for public SDK factories."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
from collections.abc import AsyncIterator, Iterator
|
|
7
|
+
from contextlib import AsyncExitStack, ExitStack, asynccontextmanager, contextmanager, suppress
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Literal, cast
|
|
11
|
+
|
|
12
|
+
from contree_client import RetryPolicy, profiles
|
|
13
|
+
from contree_client.profiles import Profile
|
|
14
|
+
from contree_client.types import ContreeAsyncClient, ContreeSyncClient
|
|
15
|
+
|
|
16
|
+
from contree_sdk.session.cleanup import await_cleanup
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def profile_from_args(
|
|
20
|
+
*, token: str, url: str | None = None, project: str | None = None, auth_type: Literal["iam", "jwt"] = "iam"
|
|
21
|
+
) -> Profile:
|
|
22
|
+
"""Build an explicit client profile without reading or writing configuration.
|
|
23
|
+
|
|
24
|
+
The default configuration uses the client's endpoint when URL is omitted.
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
A regular contree-client Profile.
|
|
28
|
+
|
|
29
|
+
Raises:
|
|
30
|
+
ValueError: Credentials, URL, or authentication type are invalid.
|
|
31
|
+
|
|
32
|
+
"""
|
|
33
|
+
if auth_type not in {"iam", "jwt"}:
|
|
34
|
+
raise ValueError("auth_type must be 'iam' or 'jwt'")
|
|
35
|
+
if not token or not token.strip():
|
|
36
|
+
raise ValueError("token must not be empty")
|
|
37
|
+
if url is None and auth_type == "iam":
|
|
38
|
+
url = profiles.DEFAULT_IAM_URL
|
|
39
|
+
if not url or not url.strip():
|
|
40
|
+
raise ValueError("url is required for JWT authentication")
|
|
41
|
+
return Profile(name="explicit", url=url, token=token, project=project, auth_type=auth_type)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass
|
|
45
|
+
class ConnectionConfig:
|
|
46
|
+
url: str | None = None
|
|
47
|
+
project: str | None = None
|
|
48
|
+
token: str | None = field(default=None, repr=False)
|
|
49
|
+
auth_type: Literal["iam", "jwt"] | None = None
|
|
50
|
+
profile: Profile | str | None = None
|
|
51
|
+
config_path: str | Path | None = None
|
|
52
|
+
client: ContreeSyncClient | ContreeAsyncClient | None = None
|
|
53
|
+
client_type: type[ContreeSyncClient | ContreeAsyncClient] | None = None
|
|
54
|
+
client_kwargs: dict[str, Any] = field(default_factory=dict, repr=False)
|
|
55
|
+
|
|
56
|
+
def resolve_profile(self) -> Profile:
|
|
57
|
+
if any(value is not None for value in (self.url, self.project, self.token, self.auth_type)):
|
|
58
|
+
if self.profile is not None or self.config_path is not None:
|
|
59
|
+
raise ValueError("connection arguments cannot be combined with profile or config_path")
|
|
60
|
+
return profile_from_args(
|
|
61
|
+
token=self.token or "", url=self.url, project=self.project, auth_type=self.auth_type or "iam"
|
|
62
|
+
)
|
|
63
|
+
if isinstance(self.profile, Profile):
|
|
64
|
+
if self.config_path is not None:
|
|
65
|
+
raise ValueError("a Profile object cannot be combined with config_path")
|
|
66
|
+
return self.profile
|
|
67
|
+
if self.profile is not None or self.config_path is not None:
|
|
68
|
+
return profiles.resolve_profile(self.profile, path=self.config_path)
|
|
69
|
+
return profiles.from_env() or profiles.resolve_profile()
|
|
70
|
+
|
|
71
|
+
def validate(self, *, asynchronous: bool) -> None:
|
|
72
|
+
mode = "async with" if asynchronous else "with"
|
|
73
|
+
contract = ContreeAsyncClient if asynchronous else ContreeSyncClient
|
|
74
|
+
if self.client_type is not None and (
|
|
75
|
+
not isinstance(self.client_type, type) or not issubclass(self.client_type, contract)
|
|
76
|
+
):
|
|
77
|
+
raise TypeError(f"client_type must be a compatible client class for {mode}")
|
|
78
|
+
if self.client is not None and not isinstance(self.client, contract):
|
|
79
|
+
raise TypeError(f"client must implement the client interface for {mode}")
|
|
80
|
+
if self.client is not None and (
|
|
81
|
+
self.client_kwargs
|
|
82
|
+
or any(
|
|
83
|
+
value is not None
|
|
84
|
+
for value in (
|
|
85
|
+
self.client_type,
|
|
86
|
+
self.profile,
|
|
87
|
+
self.config_path,
|
|
88
|
+
self.url,
|
|
89
|
+
self.project,
|
|
90
|
+
self.token,
|
|
91
|
+
self.auth_type,
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
):
|
|
95
|
+
raise ValueError("client cannot be combined with client_type or connection settings")
|
|
96
|
+
if "base_url" in self.client_kwargs:
|
|
97
|
+
raise ValueError("use url or profile instead of base_url")
|
|
98
|
+
|
|
99
|
+
def open_sync(self, stack: ExitStack) -> ContreeSyncClient:
|
|
100
|
+
self.validate(asynchronous=False)
|
|
101
|
+
if self.client is not None:
|
|
102
|
+
return cast("ContreeSyncClient", self.client)
|
|
103
|
+
client_type = cast("type[ContreeSyncClient] | None", self.client_type)
|
|
104
|
+
if client_type is None:
|
|
105
|
+
from contree_client.httpx import ContreeClient
|
|
106
|
+
|
|
107
|
+
client_type = ContreeClient
|
|
108
|
+
kwargs: dict[str, Any] = {"retry": RetryPolicy(), **self.client_kwargs}
|
|
109
|
+
client = client_type.from_profile(self.resolve_profile(), **kwargs)
|
|
110
|
+
stack.callback(client.close)
|
|
111
|
+
client.open()
|
|
112
|
+
return client
|
|
113
|
+
|
|
114
|
+
async def open_async(self, stack: AsyncExitStack) -> ContreeAsyncClient:
|
|
115
|
+
self.validate(asynchronous=True)
|
|
116
|
+
if self.client is not None:
|
|
117
|
+
return cast("ContreeAsyncClient", self.client)
|
|
118
|
+
client_type = cast("type[ContreeAsyncClient] | None", self.client_type)
|
|
119
|
+
if client_type is None:
|
|
120
|
+
from contree_client.aiohttp import ContreeAsyncClient
|
|
121
|
+
|
|
122
|
+
client_type = ContreeAsyncClient
|
|
123
|
+
kwargs: dict[str, Any] = {"retry": RetryPolicy(), **self.client_kwargs}
|
|
124
|
+
client = client_type.from_profile(self.resolve_profile(), **kwargs)
|
|
125
|
+
stack.push_async_callback(client.close)
|
|
126
|
+
await client.open()
|
|
127
|
+
return client
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@contextmanager
|
|
131
|
+
def owned_resources() -> Iterator[ExitStack]:
|
|
132
|
+
"""Close resources while preserving the primary error.
|
|
133
|
+
|
|
134
|
+
Yields:
|
|
135
|
+
A stack for resources created by a helper.
|
|
136
|
+
|
|
137
|
+
"""
|
|
138
|
+
stack = ExitStack()
|
|
139
|
+
try:
|
|
140
|
+
yield stack
|
|
141
|
+
except BaseException:
|
|
142
|
+
with suppress(Exception):
|
|
143
|
+
stack.close()
|
|
144
|
+
raise
|
|
145
|
+
else:
|
|
146
|
+
stack.close()
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@asynccontextmanager
|
|
150
|
+
async def owned_resources_async() -> AsyncIterator[AsyncExitStack]:
|
|
151
|
+
"""Join cleanup despite repeated cancellation.
|
|
152
|
+
|
|
153
|
+
Yields:
|
|
154
|
+
A stack for resources created by an async helper.
|
|
155
|
+
|
|
156
|
+
"""
|
|
157
|
+
stack = AsyncExitStack()
|
|
158
|
+
try:
|
|
159
|
+
yield stack
|
|
160
|
+
except BaseException:
|
|
161
|
+
with suppress(Exception, asyncio.CancelledError):
|
|
162
|
+
await await_cleanup(stack.aclose())
|
|
163
|
+
raise
|
|
164
|
+
else:
|
|
165
|
+
await await_cleanup(stack.aclose())
|