|
37 | 37 | from cfbs.validate import validate_config |
38 | 38 | from cfbs.cfbs_config import CFBSConfig |
39 | 39 | from cfbs.utils import find |
40 | | -from cfengine_cli.policy_language import ( |
41 | | - DEPRECATED_PROMISE_TYPES, |
42 | | - ALLOWED_BUNDLE_TYPES, |
43 | | - BUILTIN_PROMISE_TYPES, |
44 | | - BUILTIN_FUNCTIONS, |
45 | | -) |
46 | 40 | from cfengine_cli.utils import UserError |
47 | 41 |
|
48 | 42 | LINT_EXTENSIONS = (".cf", ".json") |
49 | 43 |
|
50 | 44 |
|
| 45 | +def _load_syntax_description(path: str | None = None) -> dict: |
| 46 | + """Load and return the parsed syntax-description.json file.""" |
| 47 | + if path is None: |
| 48 | + path = os.path.join(os.path.dirname(__file__), "syntax-description.json") |
| 49 | + with open(path, "r") as f: |
| 50 | + return json.load(f) |
| 51 | + |
| 52 | + |
| 53 | +def _derive_syntax_sets(data: dict) -> tuple: |
| 54 | + """Derive the four sets used for linting from a loaded syntax-description dict. |
| 55 | +
|
| 56 | + Returns: (ALLOWED_BUNDLE_TYPES, BUILTIN_PROMISE_TYPES, BUILTIN_FUNCTIONS, DEPRECATED_PROMISE_TYPES) |
| 57 | + """ |
| 58 | + builtin_body_types = set(data.get("bodyTypes", {}).keys()) |
| 59 | + |
| 60 | + allowed_bundle_types = data.get("bundleTypes", {}).keys() |
| 61 | + |
| 62 | + builtin_promise_types = set(data.get("promiseTypes", {}).keys()) |
| 63 | + |
| 64 | + builtin_functions = set(data.get("functions", {}).keys()) |
| 65 | + |
| 66 | + deprecated_promise_types = { |
| 67 | + "defaults", |
| 68 | + "guest_environments", |
| 69 | + } # Has to be hardcoded, not tagged in syntax-description.json |
| 70 | + |
| 71 | + return ( |
| 72 | + builtin_body_types, |
| 73 | + allowed_bundle_types, |
| 74 | + builtin_promise_types, |
| 75 | + builtin_functions, |
| 76 | + deprecated_promise_types, |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +_SYNTAX_DATA = _load_syntax_description() |
| 81 | +( |
| 82 | + _, |
| 83 | + ALLOWED_BUNDLE_TYPES, |
| 84 | + BUILTIN_PROMISE_TYPES, |
| 85 | + BUILTIN_FUNCTIONS, |
| 86 | + DEPRECATED_PROMISE_TYPES, |
| 87 | +) = _derive_syntax_sets(_SYNTAX_DATA) |
| 88 | + |
| 89 | + |
51 | 90 | def _qualify(name: str, namespace: str) -> str: |
52 | 91 | """If name is already qualified (contains ':'), return as-is. Otherwise prepend namespace.""" |
53 | 92 | assert '"' not in namespace |
|
0 commit comments