in utils/generate-examples.py [0:0]
def main():
for filepath in asciidocs_dir.iterdir():
if filepath.name.endswith(".asciidoc"):
filepath.unlink()
if not flight_recorder_dir.exists() or not report_path.exists():
raise RuntimeError(
f"clients-flight-recorder repository not checked out at {flight_recorder_dir}"
)
with report_path.open() as f:
report = json.loads(f.read())
t = jinja_env.get_template("example")
for exm in report:
if exm["lang"] != "console":
continue
if exm["source_location"]["file"] not in files_to_generate:
continue
parsed_sources = []
for src in exm["parsed_source"]:
params = (src.get("params") or {}).copy()
params.update(src.get("query") or {})
params = {
k: (list(v.split(",")) if isinstance(v, str) and "," in v else v)
for k, v in params.items()
}
parsed_sources.append(
ParsedSource(
api=src["api"],
params={
substitutions.get(k, k): repr(v) for k, v in params.items()
},
body=src.get("body", None) or None,
)
)
with tempfile.NamedTemporaryFile("w+", delete=False) as tmp_file:
tmp_file.write(t.render(parsed_sources=parsed_sources))
try:
blacken(tmp_file.name)
except AssertionError:
loc = exm["source_location"]
print(f"Failed to format {loc['file']}:{loc['line']}, skipping.")
continue
with open(tmp_file.name) as f:
data = f.read()
data = data.rstrip().replace(",)", ")")
os.unlink(tmp_file.name)
with (asciidocs_dir / f"{exm['digest']}.asciidoc").open(mode="w") as f:
f.truncate()
f.write(
f"""// {exm['source_location']['file']}:{exm['source_location']['line']}
[source, python]
----
{data}
----"""
)