1############################################################################
2# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3#
4# SPDX-License-Identifier: MPL-2.0
5#
6# This Source Code Form is subject to the terms of the Mozilla Public
7# License, v. 2.0. If a copy of the MPL was not distributed with this
8# file, you can obtain one at https://mozilla.org/MPL/2.0/.
9#
10# See the COPYRIGHT file distributed with this work for additional
11# information regarding copyright ownership.
12############################################################################
13
14# Depends on CWD - Sphinx plugin
15
16import json
17from pathlib import Path
18
19import parsegrammar
20
21
22def read_zone():
23    zone_grammars = {}
24    for file in Path("../misc/").glob("*.zoneopt"):
25        # in-view is not really a zone type
26        if file.stem == "in-view":
27            zone_type = "in-view"
28        else:
29            zone_type = f"type {file.stem}"
30
31        with file.open(encoding="ascii") as fp:
32            zonegrammar = parsegrammar.parse_mapbody(fp)
33            assert len(zonegrammar) == 1
34            assert "zone" in zonegrammar
35            zone_grammars[zone_type] = zonegrammar["zone"]
36            zone_grammars[zone_type]["_pprint_name"] = "zone"
37
38    return {"zone": {"_mapbody": zone_grammars, "_ignore_this_level": True}}
39
40
41def read_main():
42    with Path("../misc/options").open(encoding="ascii") as fp:
43        optgrammar = parsegrammar.parse_mapbody(fp)
44    return optgrammar
45
46
47def combine():
48    zones = read_zone()
49    assert zones
50    rest = read_main()
51    assert rest
52    rest.update(zones)
53
54    # this is a terrible hack
55    # but cfg_test cannot print zone grammars inside view
56    rest["view"]["_mapbody"].update(zones)
57
58    return rest
59
60
61if __name__ == "__main__":
62    full_grammar = combine()
63    print(json.dumps(full_grammar))
64