diff options
author | Ole Troan <otroan@employees.org> | 2024-01-23 18:56:23 +0100 |
---|---|---|
committer | Andrew Yourtchenko <ayourtch@gmail.com> | 2024-01-29 22:57:31 +0000 |
commit | ac0babd412e3b5282136a5c5c5be2c4cc4be6895 (patch) | |
tree | 5c58249463d7121a163534e55b1d5f6f27ae7967 /src/tools/vppapigen/vppapigen_json.py | |
parent | f34b6800de78d99192dc5757a4845e2668749c40 (diff) |
api: provide api definition over api
This patch allows a client to bootstrap itself by downloading the
JSON API definitions over the API itself.
This patch enables it for Python (probably need a dynamic language).
Call VPPApiClient with the new bootstrapapi=True parameter.
Example (Python):
from vpp_papi import VPPApiClient
vpp = VPPApiClient(bootstrapapi=True)
rv = vpp.connect("foobar")
assert rv == 0
print(f'SHOW VERSION: {vpp.api.show_version()}')
vpp.disconnect()
Type: feature
Change-Id: Id903fdccc82b2e22aa1994331d2c150253f2ccae
Signed-off-by: Ole Troan <otroan@employees.org>
Diffstat (limited to 'src/tools/vppapigen/vppapigen_json.py')
-rw-r--r-- | src/tools/vppapigen/vppapigen_json.py | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/src/tools/vppapigen/vppapigen_json.py b/src/tools/vppapigen/vppapigen_json.py index 0a1a3d37373..7239d1ea732 100644 --- a/src/tools/vppapigen/vppapigen_json.py +++ b/src/tools/vppapigen/vppapigen_json.py @@ -1,5 +1,7 @@ # JSON generation import json +import sys +import os process_imports = True @@ -88,7 +90,26 @@ def walk_defs(s, is_message=False): # # Plugin entry point # -def run(output_dir, filename, s): + + +def contents_to_c_string(contents): + # Escape backslashes and double quotes + contents = contents.replace("\\", "\\\\").replace('"', '\\"') + # Replace newlines with \n + contents = contents.replace("\n", "\\n") + return '"' + contents + '"' + + +def run(output_dir, apifilename, s): + if not output_dir: + sys.stderr.write("Missing --outputdir argument") + return None + + basename = os.path.basename(apifilename) + filename_json_repr = os.path.join(output_dir + "/" + basename + "_json.h") + filename, _ = os.path.splitext(basename) + modulename = filename.replace(".", "_") + j = {} j["types"] = walk_defs([o for o in s["types"] if o.__class__.__name__ == "Typedef"]) @@ -106,4 +127,9 @@ def run(output_dir, filename, s): j["vl_api_version"] = hex(s["file_crc"]) j["imports"] = walk_imports(i for i in s["Import"]) j["counters"], j["paths"] = walk_counters(s["Counters"], s["Paths"]) - return json.dumps(j, indent=4, separators=(",", ": ")) + r = json.dumps(j, indent=4, separators=(",", ": ")) + c_string = contents_to_c_string(r) + with open(filename_json_repr, "w", encoding="UTF-8") as f: + print(f"const char *json_api_repr_{modulename} = {c_string};", file=f) + # return json.dumps(j, indent=4, separators=(",", ": ")) + return r |