Coverage for postrfp/shared/expression.py: 86%

14 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-22 21:34 +0000

1from typing import Any 

2import logging 

3 

4from cel import evaluate 

5 

6 

7log = logging.getLogger(__name__) 

8 

9 

10def evaluate_expression(expression: str, context: dict[str, Any]) -> bool: 

11 """ 

12 Evaluate a CEL policy against a given context. 

13 

14 @returns True if the policy evaluates to true, False otherwise (including on error). 

15 """ 

16 if not expression or not expression.strip(): 

17 log.debug("Empty CEL expression, denying by default.") 

18 return False 

19 try: 

20 result = evaluate(expression, context) 

21 return bool(result) 

22 except Exception as e: 

23 log.error(f"Error evaluating policy '{expression}': {e}") 

24 return False