gv_doc_writer.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #!/usr/bin/env python3
  2. """man page generator for language bindings"""
  3. import dataclasses
  4. import os
  5. import re
  6. import time
  7. from pathlib import Path
  8. from typing import Dict, List, Tuple
  9. @dataclasses.dataclass
  10. class Syntax:
  11. """a language-specific syntax description"""
  12. nameprefix: str # prefix of SWIG-generated functions
  13. paramstart: str # start of a function declaration
  14. paramsep: str # function parameter separator
  15. paramend: str # end of a function declaration
  16. @dataclasses.dataclass
  17. class Lang:
  18. """template substitutions for a given language binding"""
  19. types: Dict[str, str] # syntax translations, C→this language
  20. syntax: Syntax # how to describe a function in this language
  21. synopsis: str = "" # content for the `.SH SYNOPSIS` man page section
  22. usage: str = "" # content for the `.SH USAGE` man page section
  23. LANGS = {
  24. "sharp": Lang(
  25. {
  26. "Agraph_t*": "SWIGTYPE_p_Agraph_t",
  27. "Agnode_t*": "SWIGTYPE_p_Agnode_t",
  28. "Agedge_t*": "SWIGTYPE_p_Agedge_t",
  29. "Agsym_t*": "SWIGTYPE_p_Agsym_t",
  30. "char*": "string",
  31. "const char*": "string",
  32. "char**": "outdata",
  33. "FILE*": "SWIGTYPE_p_FILE",
  34. "bool": "bool",
  35. "int": "int",
  36. "void": None,
  37. },
  38. Syntax("gv.", "(", ", ", ");"),
  39. ),
  40. "go": Lang(
  41. {
  42. "Agraph_t* g": "graph_handle",
  43. "Agraph_t* sg": "subgraph_handle",
  44. "Agnode_t* n": "node_handle",
  45. "Agnode_t* t": "tail_node_handle",
  46. "Agnode_t* h": "head_node_handle",
  47. "Agedge_t* e": "edge_handle",
  48. "Agsym_t* a": "attr_handle",
  49. "char* gne": "type",
  50. "char* name": "name",
  51. "char* tname": "tail_name",
  52. "char* hname": "head_name",
  53. "char* attr": "attr_name",
  54. "char* val": "attr_value",
  55. "char* engine": "engine",
  56. "char* string": "string",
  57. "char** outdata": "outdata",
  58. "char* format": "format",
  59. "FILE* f": "channel",
  60. "void** data": "data_handle",
  61. "Agraph_t*": "graph_handle",
  62. "Agnode_t*": "node_handle",
  63. "Agedge_t*": "edge_handle",
  64. "Agsym_t*": "attribute_handle",
  65. "char*": "string",
  66. "const char*": "string",
  67. "char**": "outdata",
  68. "FILE*": "channel",
  69. "bool": "bool",
  70. "int": "int",
  71. "void**": "data_handle",
  72. "void": None,
  73. },
  74. Syntax("gv.", "(", ", ", ");"),
  75. ),
  76. "guile": Lang(
  77. {
  78. "Agraph_t* g": "graph_handle",
  79. "Agraph_t* sg": "subgraph_handle",
  80. "Agnode_t* n": "node_handle",
  81. "Agnode_t* t": "tail_node_handle",
  82. "Agnode_t* h": "head_node_handle",
  83. "Agedge_t* e": "edge_handle",
  84. "Agsym_t* a": "attr_handle",
  85. "char* gne": "type",
  86. "char* name": "name",
  87. "char* tname": "tail_name",
  88. "char* hname": "head_name",
  89. "char* attr": "attr_name",
  90. "char* val": "attr_value",
  91. "char* engine": "engine",
  92. "char* string": "string",
  93. "char** outdata": "outdata",
  94. "char* format": "format",
  95. "FILE* f": "channel",
  96. "void** data": "data_handle",
  97. "Agraph_t*": "graph_handle",
  98. "Agnode_t*": "node_handle",
  99. "Agedge_t*": "edge_handle",
  100. "Agsym_t*": "attribute_handle",
  101. "char*": "string",
  102. "const char*": "string",
  103. "char**": "outdata",
  104. "FILE*": "channel",
  105. "bool": "bool",
  106. "int": "int",
  107. "void**": "data_handle",
  108. "void": None,
  109. },
  110. Syntax("gv.", "(", ", ", ");"),
  111. '(load-extension "./libgv.so" "SWIG_init")',
  112. ),
  113. "java": Lang(
  114. {
  115. "Agraph_t*": "SWIGTYPE_p_Agraph_t",
  116. "Agnode_t*": "SWIGTYPE_p_Agnode_t",
  117. "Agedge_t*": "SWIGTYPE_p_Agedge_t",
  118. "Agsym_t*": "SWIGTYPE_p_Agsym_t",
  119. "char*": "string",
  120. "const char*": "string",
  121. "char**": "outdata",
  122. "FILE*": "SWIGTYPE_p_FILE",
  123. "bool": "bool",
  124. "int": "int",
  125. "void": None,
  126. },
  127. Syntax("gv.", "(", ", ", ");"),
  128. 'System.loadLibrary("gv");',
  129. ),
  130. "lua": Lang(
  131. {
  132. "Agraph_t* g": "graph_handle",
  133. "Agraph_t* sg": "subgraph_handle",
  134. "Agnode_t* n": "node_handle",
  135. "Agnode_t* t": "tail_node_handle",
  136. "Agnode_t* h": "head_node_handle",
  137. "Agedge_t* e": "edge_handle",
  138. "Agsym_t* a": "attr_handle",
  139. "char* gne": "type",
  140. "char* name": "name",
  141. "char* tname": "tail_name",
  142. "char* hname": "head_name",
  143. "char* attr": "attr_name",
  144. "char* val": "attr_value",
  145. "char* engine": "engine",
  146. "char* string": "string",
  147. "char** outdata": "outdata",
  148. "char* format": "format",
  149. "FILE* f": "channel",
  150. "void** data": "data_handle",
  151. "Agraph_t*": "graph_handle",
  152. "Agnode_t*": "node_handle",
  153. "Agedge_t*": "edge_handle",
  154. "Agsym_t*": "attribute_handle",
  155. "char*": "string",
  156. "const char*": "string",
  157. "char**": "outdata",
  158. "FILE*": "channel",
  159. "bool": "bool",
  160. "int": "int",
  161. "void**": "data_handle",
  162. "void": None,
  163. },
  164. Syntax("gv.", "(", ", ", ");"),
  165. """\
  166. #!/usr/bin/lua
  167. require('gv')""",
  168. ),
  169. "perl": Lang(
  170. {
  171. "Agraph_t* g": "graph_handle",
  172. "Agraph_t* sg": "subgraph_handle",
  173. "Agnode_t* n": "node_handle",
  174. "Agnode_t* t": "tail_node_handle",
  175. "Agnode_t* h": "head_node_handle",
  176. "Agedge_t* e": "edge_handle",
  177. "Agsym_t* a": "attr_handle",
  178. "char* gne": "type",
  179. "char* name": "name",
  180. "char* tname": "tail_name",
  181. "char* hname": "head_name",
  182. "char* attr": "attr_name",
  183. "char* val": "attr_value",
  184. "char* engine": "engine",
  185. "char* string": "string",
  186. "char** outdata": "outdata",
  187. "char* format": "format",
  188. "FILE* f": "channel",
  189. "void** data": "data_handle",
  190. "Agraph_t*": "graph_handle",
  191. "Agnode_t*": "node_handle",
  192. "Agedge_t*": "edge_handle",
  193. "Agsym_t*": "attribute_handle",
  194. "char*": "string",
  195. "const char*": "string",
  196. "char**": "outdata",
  197. "FILE*": "channel",
  198. "bool": "bool",
  199. "int": "int",
  200. "void**": "data_handle",
  201. "void": None,
  202. },
  203. Syntax("gv::", "(", ", ", ");"),
  204. """\
  205. #!/usr/bin/perl
  206. use gv;""",
  207. ),
  208. "php": Lang(
  209. {
  210. "Agraph_t* g": "graph_handle",
  211. "Agraph_t* sg": "subgraph_handle",
  212. "Agnode_t* n": "node_handle",
  213. "Agnode_t* t": "tail_node_handle",
  214. "Agnode_t* h": "head_node_handle",
  215. "Agedge_t* e": "edge_handle",
  216. "Agsym_t* a": "attr_handle",
  217. "char* gne": "type",
  218. "char* name": "name",
  219. "char* tname": "tail_name",
  220. "char* hname": "head_name",
  221. "char* attr": "attr_name",
  222. "char* val": "attr_value",
  223. "char* engine": "engine",
  224. "char* string": "string",
  225. "char** outdata": "outdata",
  226. "char* format": "format",
  227. "FILE* f": "channel",
  228. "void** data": "data_handle",
  229. "Agraph_t*": "graph_handle",
  230. "Agnode_t*": "node_handle",
  231. "Agedge_t*": "edge_handle",
  232. "Agsym_t*": "attribute_handle",
  233. "char*": "string",
  234. "const char*": "string",
  235. "char**": "outdata",
  236. "FILE*": "channel",
  237. "bool": "bool",
  238. "int": "int",
  239. "void**": "data_handle",
  240. "void": None,
  241. },
  242. Syntax("gv::", "(", ", ", ");"),
  243. """\
  244. #!/usr/bin/php
  245. <?
  246. include("gv.php")
  247. ...
  248. ?>""",
  249. ),
  250. "python": Lang(
  251. {
  252. "Agraph_t* g": "graph_handle",
  253. "Agraph_t* sg": "subgraph_handle",
  254. "Agnode_t* n": "node_handle",
  255. "Agnode_t* t": "tail_node_handle",
  256. "Agnode_t* h": "head_node_handle",
  257. "Agedge_t* e": "edge_handle",
  258. "Agsym_t* a": "attr_handle",
  259. "char* gne": "type",
  260. "char* name": "name",
  261. "char* tname": "tail_name",
  262. "char* hname": "head_name",
  263. "char* attr": "attr_name",
  264. "char* val": "attr_value",
  265. "char* engine": "engine",
  266. "char* string": "string",
  267. "char** outdata": "outdata",
  268. "char* format": "format",
  269. "FILE* f": "channel",
  270. "void** data": "data_handle",
  271. "Agraph_t*": "graph_handle",
  272. "Agnode_t*": "node_handle",
  273. "Agedge_t*": "edge_handle",
  274. "Agsym_t*": "attribute_handle",
  275. "char*": "string",
  276. "const char*": "string",
  277. "char**": "outdata",
  278. "FILE*": "channel",
  279. "bool": "bool",
  280. "int": "int",
  281. "void**": "data_handle",
  282. "void": None,
  283. },
  284. Syntax("gv.", "(", ", ", ");"),
  285. """\
  286. #!/usr/bin/python
  287. import sys
  288. import gv""",
  289. ),
  290. "R": Lang(
  291. {
  292. "Agraph_t*": "SWIGTYPE_p_Agraph_t",
  293. "Agnode_t*": "SWIGTYPE_p_Agnode_t",
  294. "Agedge_t*": "SWIGTYPE_p_Agedge_t",
  295. "Agsym_t*": "SWIGTYPE_p_Agsym_t",
  296. "char*": "string",
  297. "const char*": "string",
  298. "char**": "outdata",
  299. "FILE*": "SWIGTYPE_p_FILE",
  300. "bool": "bool",
  301. "int": "int",
  302. "void": None,
  303. },
  304. Syntax("gv.", "(", ", ", ");"),
  305. 'System.loadLibrary("gv");',
  306. ),
  307. "ruby": Lang(
  308. {
  309. "Agraph_t* g": "graph_handle",
  310. "Agraph_t* sg": "subgraph_handle",
  311. "Agnode_t* n": "node_handle",
  312. "Agnode_t* t": "tail_node_handle",
  313. "Agnode_t* h": "head_node_handle",
  314. "Agedge_t* e": "edge_handle",
  315. "Agsym_t* a": "attr_handle",
  316. "char* gne": "type",
  317. "char* name": "name",
  318. "char* tname": "tail_name",
  319. "char* hname": "head_name",
  320. "char* attr": "attr_name",
  321. "char* val": "attr_value",
  322. "char* engine": "engine",
  323. "char* string": "string",
  324. "char** outdata": "outdata",
  325. "char* format": "format",
  326. "FILE* f": "channel",
  327. "void** data": "data_handle",
  328. "Agraph_t*": "graph_handle",
  329. "Agnode_t*": "node_handle",
  330. "Agedge_t*": "edge_handle",
  331. "Agsym_t*": "attribute_handle",
  332. "char*": "string",
  333. "const char*": "string",
  334. "char**": "outdata",
  335. "FILE*": "channel",
  336. "bool": "bool",
  337. "int": "int",
  338. "void**": "data_handle",
  339. "void": None,
  340. },
  341. Syntax("Gv.", "(", ", ", ");"),
  342. """\
  343. #!/usr/bin/ruby
  344. require 'gv'""",
  345. ),
  346. "tcl": Lang(
  347. {
  348. "Agraph_t* g": "<graph_handle>",
  349. "Agraph_t* sg": "<subgraph_handle>",
  350. "Agnode_t* n": "<node_handle>",
  351. "Agnode_t* t": "<tail_node_handle>",
  352. "Agnode_t* h": "<head_node_handle>",
  353. "Agedge_t* e": "<edge_handle>",
  354. "Agsym_t* a": "<attr_handle>",
  355. "char* gne": "<type>",
  356. "char* name": "<name>",
  357. "char* tname": "<tail_name>",
  358. "char* hname": "<head_name>",
  359. "char* attr": "<attr_name>",
  360. "char* val": "<attr_value>",
  361. "char* engine": "<engine>",
  362. "char* string": "<string>",
  363. "char** outdata": "<outdata>",
  364. "char* format": "<format>",
  365. "FILE* f": "<channel>",
  366. "void** data": "<data_handle>",
  367. "Agraph_t*": "<graph_handle>",
  368. "Agnode_t*": "<node_handle>",
  369. "Agedge_t*": "<edge_handle>",
  370. "Agsym_t*": "<attr_handle>",
  371. "char*": "<string>",
  372. "const char*": "<string>",
  373. "char**": "<outdata>",
  374. "FILE*": "<channel>",
  375. "bool": "<boolean_string>",
  376. "int": "<integer_string>",
  377. "void**": "<data_handle>",
  378. "void": None,
  379. },
  380. Syntax("gv::", "", " ", ""),
  381. """\
  382. #!/usr/bin/tclsh
  383. package require gv""",
  384. "Requires tcl8.3 or later.",
  385. ),
  386. }
  387. # man page content template
  388. TEMPLATE = """\
  389. .TH gv 3{lang_lower} "{buildtime}"
  390. .SH NAME
  391. gv_{lang} - graph manipulation in {lang}
  392. .SH SYNOPSIS
  393. {synopsis}
  394. .SH USAGE
  395. {usage}
  396. .SH INTRODUCTION
  397. .B gv_{lang}
  398. is a dynamically loaded extension for
  399. .B {lang}
  400. that provides access to the graph facilities of
  401. .B graphviz.
  402. .SH COMMANDS
  403. {commands}
  404. .SH KEYWORDS
  405. graph, dot, neato, fdp, circo, twopi, {lang}.
  406. """
  407. def gv_doc_synopsis(lang: Lang) -> str:
  408. """generate a substitution for the `.SH SYNOPSIS` section"""
  409. return "\n.br\n".join(lang.synopsis.split("\n"))
  410. def gv_doc_usage(lang: Lang) -> str:
  411. """generate a substitution for the `.SH USAGE` section"""
  412. return "\n.P\n".join(lang.usage.split("\n"))
  413. def gv_doc_commands(lang: Lang) -> str:
  414. """generate a substitution for the `.SH COMMANDS` section"""
  415. res = []
  416. fn = Path(__file__).parent / "gv.i"
  417. with open(fn, "rt", encoding="utf-8") as f:
  418. t = f.read()
  419. t = re.sub(r"(.|\n)*?%\{", "", t, count=1, flags=re.MULTILINE)
  420. t = re.sub(r"%\}(.|\n)*", "", t, count=1, flags=re.MULTILINE)
  421. t = re.sub(r"\bextern\b", "", t)
  422. for rec in t.split("\n"):
  423. rec = rec.strip(" \t;)")
  424. if len(rec) == 0:
  425. continue
  426. if c := re.match(r"/\*\*\* (?P<docstring>.*) \*/$", rec):
  427. res += [".TP", c.group("docstring"), ".br"]
  428. continue
  429. if c := re.match(r"/\*\* (?P<docstring>.*) \*/$", rec):
  430. res += [".TP", f"\\fB{c.group('docstring')}\\fR", ".br"]
  431. continue
  432. if re.match(r"/\*.*\*/$", rec):
  433. continue
  434. if re.match("//", rec):
  435. continue
  436. if re.match("#", rec):
  437. continue
  438. params: List[Tuple[str, str]] = []
  439. func = None
  440. functype = None
  441. for i, type_name in enumerate(re.split(r"[\(,]", rec)):
  442. type_name = type_name.strip()
  443. type_name = re.sub(r"[ ]+(\**)", r"\1 ", type_name)
  444. type, name = type_name.rsplit(maxsplit=1)
  445. if i == 0:
  446. func = name
  447. functype = type
  448. else:
  449. params += [(type, name)]
  450. par = []
  451. for paramtype, param in params:
  452. if translation := lang.types.get(f"{paramtype} {param}"):
  453. par += [translation]
  454. else:
  455. par += [f"{lang.types[paramtype]} {param}"]
  456. rtype = ""
  457. if function_type := lang.types[functype]:
  458. rtype = f"\\fI{function_type}\\fR "
  459. res += [
  460. f"{rtype}\\fB{lang.syntax.nameprefix}{func}\\fR "
  461. f"\\fI{lang.syntax.paramstart}"
  462. f"{lang.syntax.paramsep.join(par)}{lang.syntax.paramend}\\fR",
  463. ".br",
  464. ]
  465. return "\n".join(res)
  466. def main():
  467. """entry point"""
  468. buildtime = os.environ.get("SOURCE_DATE_EPOCH")
  469. if buildtime is None:
  470. buildtime = time.strftime("%d %B %Y", time.gmtime())
  471. for name, lang in LANGS.items():
  472. with open(f"gv.3{name.lower()}", "wt", encoding="utf-8") as f:
  473. f.write(
  474. TEMPLATE.format(
  475. lang_lower=name.lower(),
  476. buildtime=buildtime,
  477. lang=name,
  478. synopsis=gv_doc_synopsis(lang),
  479. usage=gv_doc_usage(lang),
  480. commands=gv_doc_commands(lang),
  481. )
  482. )
  483. if __name__ == "__main__":
  484. main()