-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from contextlib import contextmanager | ||
from typing import Any, Iterator | ||
|
||
from wireup.ioc.types import ContainerProxyQualifierValue, ServiceOverride | ||
Check failure on line 6 in wireup/ioc/override_manager.py GitHub Actions / build (3.8)Ruff (TCH001)
Check failure on line 6 in wireup/ioc/override_manager.py GitHub Actions / build (3.8)Ruff (TCH001)
Check failure on line 6 in wireup/ioc/override_manager.py GitHub Actions / build (3.9)Ruff (TCH001)
Check failure on line 6 in wireup/ioc/override_manager.py GitHub Actions / build (3.9)Ruff (TCH001)
Check failure on line 6 in wireup/ioc/override_manager.py GitHub Actions / build (3.10)Ruff (TCH001)
Check failure on line 6 in wireup/ioc/override_manager.py GitHub Actions / build (3.10)Ruff (TCH001)
Check failure on line 6 in wireup/ioc/override_manager.py GitHub Actions / build (3.11)Ruff (TCH001)
|
||
|
||
|
||
class OverrideManager: | ||
Check failure on line 9 in wireup/ioc/override_manager.py GitHub Actions / build (3.8)Ruff (D101)
Check failure on line 9 in wireup/ioc/override_manager.py GitHub Actions / build (3.9)Ruff (D101)
Check failure on line 9 in wireup/ioc/override_manager.py GitHub Actions / build (3.10)Ruff (D101)
|
||
def __init__(self, active_overrides: dict[(type, ContainerProxyQualifierValue), Any]): | ||
Check failure on line 10 in wireup/ioc/override_manager.py GitHub Actions / build (3.8)Ruff (ANN204)
Check failure on line 10 in wireup/ioc/override_manager.py GitHub Actions / build (3.9)Ruff (ANN204)
Check failure on line 10 in wireup/ioc/override_manager.py GitHub Actions / build (3.10)Ruff (ANN204)
|
||
self.__active_overrides = active_overrides | ||
|
||
def set(self, target: type, new: Any, qualifier: ContainerProxyQualifierValue = None): | ||
Check failure on line 13 in wireup/ioc/override_manager.py GitHub Actions / build (3.8)Ruff (A003)
Check failure on line 13 in wireup/ioc/override_manager.py GitHub Actions / build (3.9)Ruff (A003)
Check failure on line 13 in wireup/ioc/override_manager.py GitHub Actions / build (3.10)Ruff (A003)
|
||
self.__active_overrides[target, qualifier] = new | ||
|
||
def delete(self, target: type, qualifier: ContainerProxyQualifierValue = None): | ||
if (target, qualifier) in self.__active_overrides: | ||
del self.__active_overrides[target, qualifier] | ||
|
||
@contextmanager | ||
def service(self, target: type, new: Any, qualifier: ContainerProxyQualifierValue = None) -> Iterator[None]: | ||
"""Override the target service with new for the duration of the context manager.""" | ||
try: | ||
self.set(target, new, qualifier) | ||
yield | ||
finally: | ||
self.delete(target, qualifier) | ||
|
||
@contextmanager | ||
def services(self, overrides: list[ServiceOverride]) -> Iterator[None]: | ||
"""Override the target service with new for the duration of the context manager.""" | ||
try: | ||
for override in overrides: | ||
self.set(override.target, override.new, override.qualifier) | ||
yield | ||
finally: | ||
for override in overrides: | ||
self.delete(override.target, override.qualifier) |