]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/generate-api-swagger.py
1 """Produce API swagger YAML and JSON representations usable from C."""
9 def dump_hex(contents
, array_name
, string_name
):
10 """Dump the hex contents to stdout."""
11 array
= "static const unsigned char {0}[] = {{"
12 string
= "static const std::string {0}{{(const char*){1}, sizeof({1})}};"
15 print(array
.format(array_name
))
16 for index
, byte
in enumerate(contents
.encode()):
17 if (index
+ 1) % 15 == 0:
19 print(f
"{byte:#x}", end
=", ")
24 print(string
.format(string_name
, array_name
))
27 yaml_filename
= sys
.argv
[1]
29 with
open(yaml_filename
, mode
="r", encoding
="utf-8") as f_in
:
30 yaml_contents
= f_in
.read()
31 contents
= yaml
.safe_load(yaml_contents
)
32 json_contents
= json
.dumps(contents
, indent
=2, separators
=(",", ": "))
34 header
= "#pragma once\n#include <string>"
38 dump_hex(yaml_contents
, "api_swagger_yamlData", "g_api_swagger_yaml")
40 print("// -----------------------------------------------------------")
42 dump_hex(json_contents
, "api_swagger_jsonData", "g_api_swagger_json")