Skip to content

Instantly share code, notes, and snippets.

@glimchb
Last active May 30, 2025 18:27
Show Gist options
  • Save glimchb/58416b643df31b27332062d775017d85 to your computer and use it in GitHub Desktop.
Save glimchb/58416b643df31b27332062d775017d85 to your computer and use it in GitHub Desktop.
SPDK doc/jsonrpc.md table formatter using tabulate package
###################################################
import re
from tabulate import tabulate
with open("doc/jsonrpc.md.out", "w") as wfile:
with open("doc/jsonrpc.md", "r") as rfile:
in_table = False
for line in rfile.readlines():
if re.search("Name.+?\|.+?Optional.+?\|.+?Type.+?\|.+?Description", line):
# print(">>", line )
in_table = True
params = []
continue
if not in_table:
wfile.write(line)
continue
if in_table:
# print("<<", line )
if "--------" in line:
# skip separator line
continue
if not line.strip():
in_table = False
params = tabulate(params, headers="keys", tablefmt="presto").replace("-+-", " | ")
wfile.write(params)
params = []
wfile.write("\n\n")
continue
fname, foptional, ftype, fdescription = line.split("|")
params.append(dict(Name=fname.strip(), Optional=foptional.strip(), Type=ftype.strip(), Description=fdescription.strip()))
###################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment