Skip to content

Commit 1915599

Browse files
committed
feat: remove sync context support and related code
1 parent d9644e2 commit 1915599

7 files changed

Lines changed: 2 additions & 611 deletions

File tree

docs/api/context.md

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -61,56 +61,3 @@ asyncio.run(run_all_hosts())
6161
`AsyncHostContext` scopes the helper to a single host (specified by name or by a
6262
`Host` instance) but otherwise behaves the same. Both helpers accept a
6363
`hosts=` override when you need to target a subset of hosts for a specific call.
64-
65-
## SyncContext & SyncHostContext
66-
67-
For synchronous code, `pyinfra.sync_context.SyncContext` and
68-
`pyinfra.sync_context.SyncHostContext` mirror the async API without any asyncio
69-
wrapping. The synchronous helpers still manage host connections for you and make
70-
host facts available via `host.get_fact(...)`.
71-
72-
```python
73-
from pyinfra.api import Config, State, deploy
74-
from pyinfra.facts.server import Hostname
75-
from pyinfra.context import host
76-
from pyinfra.operations import server
77-
from pyinfra.sync_context import SyncContext, SyncHostContext
78-
79-
80-
def run_all_hosts():
81-
inventory = ...
82-
state = State(inventory, Config())
83-
84-
@deploy("Example sync deploy")
85-
def my_sync_deploy():
86-
server.shell(name="Sync deploy op", commands="echo sync deploy")
87-
88-
with SyncContext(state):
89-
server.shell("echo hello from sync")
90-
hostnames = {
91-
host: host.get_fact(Hostname)
92-
for host in state.inventory
93-
}
94-
print(f"Hostnames: {hostnames}")
95-
96-
my_sync_deploy()
97-
98-
99-
def run_single_host(state, host_name):
100-
@deploy("Sync per-host deploy")
101-
def my_sync_host_deploy():
102-
server.shell(name="Sync host deploy op", commands="echo sync host deploy")
103-
104-
with SyncHostContext(state, host_name):
105-
assert host is not None
106-
server.shell("echo from sync single host")
107-
hostname = host.get_fact(Hostname)
108-
print(hostname)
109-
110-
my_sync_host_deploy()
111-
```
112-
113-
`SyncHostContext` accepts either the host name or the `Host` object directly and
114-
ensures only that host is connected for the duration of the context. The sync
115-
and async helpers can be mixed within the same project—choose the one that fits
116-
the surrounding code.

src/pyinfra/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from .version import __version__ # noqa
1818

1919
from .async_context import AsyncContext as AsyncContext, AsyncHostContext as AsyncHostContext # noqa: E402,F401
20-
from .sync_context import SyncContext as SyncContext, SyncHostContext as SyncHostContext # noqa: E402,F401
2120

2221
# Initialise base classes - this sets the context modules to point at the underlying
2322
# class objects (Host, etc), which makes ipython/etc work as expected.

src/pyinfra/api/deploy.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from .arguments import pop_global_arguments
1717
from .arguments_typed import PyinfraOperation
18-
from .operation import get_async_context, get_sync_context
18+
from .operation import get_async_context
1919
from .exceptions import PyinfraError
2020
from .host import Host
2121
from .state import StateStage
@@ -91,10 +91,6 @@ def decorated_func(*args: P.args, **kwargs: P.kwargs) -> Any:
9191
if async_context is not None:
9292
return async_context._call_wrapped_deploy(decorated_func, args, kwargs)
9393

94-
sync_context = get_sync_context()
95-
if sync_context is not None:
96-
return sync_context._call_wrapped_deploy(decorated_func, args, kwargs)
97-
9894
deploy_kwargs, _ = pop_global_arguments(context.state, context.host, kwargs)
9995

10096
deploy_data = getattr(func, "deploy_data", None)

src/pyinfra/api/host.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,16 +372,12 @@ def get_fact(self, name_or_cls, *args, **kwargs):
372372
"""
373373
Get a fact for this host, reading from the cache if present.
374374
"""
375-
from pyinfra.api.operation import get_async_context, get_sync_context
375+
from pyinfra.api.operation import get_async_context
376376

377377
async_context = get_async_context()
378378
if async_context is not None:
379379
return async_context._call_wrapped_fact(self, name_or_cls, args, kwargs)
380380

381-
sync_context = get_sync_context()
382-
if sync_context is not None:
383-
return sync_context._call_wrapped_fact(self, name_or_cls, args, kwargs)
384-
385381
return _load_fact(self.state, self, name_or_cls, args=args, kwargs=kwargs)
386382

387383
# Connector proxy

src/pyinfra/api/operation.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@
4242
"pyinfra_current_async_context",
4343
default=None,
4444
)
45-
_current_sync_context: ContextVar[Any | None] = ContextVar(
46-
"pyinfra_current_sync_context",
47-
default=None,
48-
)
49-
50-
5145
def push_async_context(ctx: Any) -> Token:
5246
return _current_async_context.set(ctx)
5347

@@ -64,22 +58,6 @@ def get_async_context() -> Any | None:
6458
return _current_async_context.get()
6559

6660

67-
def push_sync_context(ctx: Any) -> Token:
68-
return _current_sync_context.set(ctx)
69-
70-
71-
def reset_sync_context(token: Token) -> None:
72-
_current_sync_context.reset(token)
73-
74-
75-
def suspend_sync_context() -> Token:
76-
return _current_sync_context.set(None)
77-
78-
79-
def get_sync_context() -> Any | None:
80-
return _current_sync_context.get()
81-
82-
8361
if TYPE_CHECKING:
8462
from pyinfra.connectors.util import CommandOutput
8563

@@ -314,10 +292,6 @@ def decorated_func(*args: P.args, **kwargs: P.kwargs) -> OperationMeta:
314292
if async_context is not None:
315293
return async_context._call_wrapped_operation(decorated_func, args, kwargs)
316294

317-
sync_context = get_sync_context()
318-
if sync_context is not None:
319-
return sync_context._call_wrapped_operation(decorated_func, args, kwargs)
320-
321295
state = context.state
322296
host = context.host
323297

0 commit comments

Comments
 (0)