1+ import copy
2+
3+ from ..exceptions .validation_error import ValidationException
4+ from ..constants .context import ExecutionContextType
5+
6+ class ExecutionContext :
7+ """
8+ Stores the Execution context and provides helper methods to manage it.
9+
10+ Methods
11+ -------
12+ update(execution_context:dict, reset:bool=False):
13+ Updates the Execution Context.
14+
15+ get_context_by_key(key:str):
16+ Returns the execution context stored for the provided key.
17+
18+ get_context() -> dict:
19+ Returns the entire execution context.
20+
21+ reset(key:str):
22+ Resets the context.
23+ """
24+ ALLOWED_KEYS = [e .value for e in ExecutionContextType ]
25+
26+ def __init__ (self , execution_context : dict ):
27+ self ._context = {}
28+ for key in execution_context .keys ():
29+ if key not in self .ALLOWED_KEYS :
30+ # TODO: Create Validation error
31+ raise ValidationException (f'Invalid execution context type: { key } ' )
32+ else :
33+ self ._context [key ] = execution_context [key ]
34+
35+ def update (self , execution_context : dict , reset = False ):
36+ """
37+ Updates the execution context.
38+
39+ Args:
40+ execution_context (dict): Execution context to be updated.
41+ reset (bool, optional): If True, replaces the entire context. If False, updates only the provided values. Defaults to False.
42+
43+ Raises:
44+ ValidationException: If an invalid key is provided.
45+ """
46+ if reset :
47+ self .reset ()
48+
49+ for key in execution_context .keys ():
50+ if key not in self .ALLOWED_KEYS :
51+ raise ValidationException (f'Invalid execution context type: { key } ' )
52+ else :
53+ self ._context [key ] = execution_context [key ]
54+
55+ def get_context_by_key (self , key : str ) -> str :
56+ """
57+ Returns the execution context for the specified key
58+
59+ Args:
60+ key (str): key
61+
62+ Returns:
63+ any: Execution Context
64+ """
65+ return copy .deepcopy (self ._context [key ])
66+
67+ def get_context (self ) -> dict :
68+ """
69+ Returns the execution context.
70+
71+ Returns:
72+ dict: Entire execution context
73+ """
74+ return copy .deepcopy (self ._context )
75+
76+ def reset (self ):
77+ """
78+ Resets the execution context.
79+ """
80+ self ._context = {}
0 commit comments