Coverage for src/lexigram/web/routing/execution_context.py: 59%

34 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 04:37 +0800

1"""Web execution context implementation. 

2 

3Provides the concrete ExecutionContextProtocol for HTTP requests. 

4""" 

5 

6from __future__ import annotations 

7 

8from typing import TYPE_CHECKING, Any 

9 

10from lexigram.web.protocols import ExecutionContextProtocol 

11 

12if TYPE_CHECKING: 

13 from collections.abc import Callable 

14 

15 

16class WebExecutionContext(ExecutionContextProtocol): 

17 """Concrete execution context for HTTP requests. 

18 

19 Provides access to request metadata, handler information, and route details 

20 for use by interceptors, guards, and pipelines. 

21 """ 

22 

23 def __init__( 

24 self, 

25 request: Any, 

26 handler: Callable, 

27 controller_class: type | None = None, 

28 method_name: str | None = None, 

29 route_metadata: dict[str, Any] | None = None, 

30 container: Any = None, 

31 ): 

32 """Initialize the execution context. 

33 

34 Args: 

35 request: The current HTTP request. 

36 handler: The handler function that will be called. 

37 controller_class: The controller class, if this is a controller method. 

38 method_name: The name of the method being called. 

39 route_metadata: Metadata about the route. 

40 container: Scoped DI container for the request. 

41 """ 

42 self._request = request 

43 self._handler = handler 

44 self._controller_class = controller_class 

45 self._method_name = method_name 

46 self._route_metadata = route_metadata or {} 

47 self._container = container 

48 

49 @property 

50 def request(self) -> Any: 

51 """The current HTTP request.""" 

52 return self._request 

53 

54 @property 

55 def handler(self) -> Callable: 

56 """The handler function that will be called.""" 

57 return self._handler 

58 

59 @property 

60 def controller_class(self) -> type | None: 

61 """The controller class, if this is a controller method.""" 

62 return self._controller_class 

63 

64 @property 

65 def method_name(self) -> str | None: 

66 """The name of the method being called.""" 

67 return self._method_name 

68 

69 @property 

70 def route_metadata(self) -> dict[str, Any]: 

71 """Metadata about the route.""" 

72 return self._route_metadata 

73 

74 @property 

75 def container(self) -> Any: 

76 """The scoped DI container for this request.""" 

77 return self._container 

78 

79 def get(self, key: str, default: Any = None) -> Any: 

80 """Get a custom value from the context.""" 

81 return self._route_metadata.get(key, default) 

82 

83 def set(self, key: str, value: Any) -> None: 

84 """Set a custom value on the context.""" 

85 self._route_metadata[key] = value 

86 

87 

88__all__ = ["WebExecutionContext"]