Skip to content

Commit 9959e34

Browse files
committed
refactor: Improve import order and cleanup environment creation in examples
1 parent 614f191 commit 9959e34

File tree

8 files changed

+23
-13
lines changed

8 files changed

+23
-13
lines changed

examples/anthropic_tool_use.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from __future__ import annotations
44

5-
from gitpod import AsyncGitpod
6-
import gitpod.lib as util
75
from anthropic import Anthropic
86
from anthropic.types import ToolParam, MessageParam
97

8+
import gitpod.lib as util
9+
from gitpod import AsyncGitpod
10+
1011
gpclient = AsyncGitpod()
1112
llmclient = Anthropic()
1213

@@ -33,7 +34,7 @@
3334
},
3435
]
3536

36-
async def create_environment(args: dict[str, str]) -> str:
37+
async def create_environment(args: dict[str, str], cleanup: util.Disposables) -> str:
3738
env_class = await util.find_most_used_environment_class(gpclient)
3839
if not env_class:
3940
raise Exception("No environment class found. Please create one first.")
@@ -46,6 +47,7 @@ async def create_environment(args: dict[str, str]) -> str:
4647
"machine": {"class": env_class.id},
4748
}
4849
)).environment.id
50+
cleanup.add(lambda: asyncio.run(gpclient.environments.delete(environment_id=environment_id)))
4951
print(f"\nCreated environment: {environment_id} - waiting for it to be ready...")
5052
await util.wait_for_environment_ready(gpclient, environment_id)
5153
print(f"\nEnvironment is ready: {environment_id}")
@@ -81,8 +83,7 @@ async def main(cleanup: util.Disposables):
8183
for tool in (c for c in message.content if c.type == "tool_use"):
8284
try:
8385
if tool.name == "create_environment":
84-
environment_id = await create_environment(tool.input)
85-
cleanup.add(lambda: asyncio.run(gpclient.environments.delete(environment_id=environment_id)))
86+
environment_id = await create_environment(tool.input, cleanup)
8687
messages.append({
8788
"role": "user",
8889
"content": [{

examples/fs_access.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#!/usr/bin/env python
22

3-
from io import StringIO
43
import sys
54
import asyncio
5+
from io import StringIO
6+
67
import paramiko
7-
from gitpod import AsyncGitpod
8+
89
import gitpod.lib as util
10+
from gitpod import AsyncGitpod
911
from gitpod.types.environment_spec_param import EnvironmentSpecParam
1012

13+
1114
# Examples:
1215
# - ./examples/fs_access.py
1316
# - ./examples/fs_access.py https://github.com/gitpod-io/empty

examples/run_command.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import sys
44
import asyncio
5-
from gitpod import AsyncGitpod
5+
66
import gitpod.lib as util
7+
from gitpod import AsyncGitpod
78
from gitpod.types.environment_spec_param import EnvironmentSpecParam
89

10+
911
# Examples:
1012
# - ./examples/run_command.py 'echo "Hello World!"'
1113
# - ./examples/run_command.py 'echo "Hello World!"' https://github.com/gitpod-io/empty

examples/run_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import sys
44
import asyncio
5-
from gitpod import AsyncGitpod
5+
66
import gitpod.lib as util
7+
from gitpod import AsyncGitpod
78
from gitpod.types.environment_spec_param import EnvironmentSpecParam
89

10+
911
# Examples:
1012
# - ./examples/run_service.py
1113
# - ./examples/run_service.py https://github.com/gitpod-io/empty

src/gitpod/lib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .automation import run_command, run_service
2-
from .environment import find_most_used_environment_class, EnvironmentState, wait_for_environment_ready
32
from .disposables import Disposables
3+
from .environment import EnvironmentState, wait_for_environment_ready, find_most_used_environment_class
44

55
__all__ = [
66
'find_most_used_environment_class',

src/gitpod/lib/automation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import asyncio
2+
from typing import Callable, Optional, Awaitable, AsyncIterator
3+
24
import httpx
3-
from typing import AsyncIterator, Awaitable, Callable, Optional
45

56
from gitpod._client import AsyncGitpod
67
from gitpod.types.environments.automations import service_create_params
8+
79
TASK_REFERENCE = "gitpod-python-sdk"
810

911
async def run_service(

src/gitpod/lib/disposables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from typing import Any, Callable, List
21
import logging
2+
from typing import Any, List, Callable
33

44
log = logging.getLogger(__name__)
55

src/gitpod/lib/environment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Optional, Callable, List, TypeVar
21
import asyncio
32
import logging
3+
from typing import List, TypeVar, Callable, Optional
44

55
from gitpod import AsyncGitpod
66
from gitpod.types.shared import EnvironmentClass

0 commit comments

Comments
 (0)