Coverage for /usr/local/lib/python3.10/site-packages/opal_common-0.0.0-py3.10.egg/opal_common/engine/parsing.py: 29%

10 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 11:54 +0000

1import re 

2from typing import Optional 

3 

4# This regex matches the package declaration at the top of a valid .rego file 

5REGO_PACKAGE_DECLARATION = re.compile(r"^package\s+([a-zA-Z0-9\.\"\[\]]+)$") 

6 

7 

8def get_rego_package(contents: str) -> Optional[str]: 

9 """Try to parse the package name from rego file contents. 

10 

11 return None if failed to parse (probably invalid .rego file) 

12 """ 

13 lines = contents.splitlines() 

14 for line in lines: 

15 match = REGO_PACKAGE_DECLARATION.match(line) 

16 if match is not None: 

17 return match.group(1) 

18 return None