Manual tweak.
[dcpomatic.git] / doc / manual / verifier.py
1 #!/usr/bin/python3
2
3 from pathlib import Path
4 import re
5 import sys
6 import subprocess
7
8 if len(sys.argv) < 3:
9     print(f"Syntax: {sys.argv[0]} <path-to-libdcp-source-tree> <ERROR|BV21_ERROR|WARNING>")
10     sys.exit(1)
11
12 libdcp = Path(sys.argv[1])
13 type = sys.argv[2]
14 header = libdcp / "src" / "verify.h"
15
16 types = ("BV21_ERROR", "ERROR", "WARNING")
17
18 def find_type(name):
19     """
20     Search source code to find where a given code is used and hence find out whether
21     it represents an error, Bv2.1 "error" or warning.
22     """
23     previous = ''
24     for source in ["verify_j2k.cc", "dcp.cc", "verify.cc"]:
25         path = libdcp / "src" / source
26         with open(path) as s:
27             for line in s:
28                 if line.find(name) != -1:
29                     line_with_previous = previous + line
30                     for t in types:
31                         if line_with_previous.find(t) != -1:
32                             return t
33                     assert False
34                 previous = line
35
36
37 print('<itemizedlist>')
38
39 active = False
40 with open(header) as h:
41     for line in h:
42         strip = line.strip()
43         if strip == "enum class Code {":
44             active = True
45         elif strip == "};":
46             active = False
47         elif active:
48             if strip.startswith('/**'):
49                 text = strip.replace('/**', '').replace('*/', '').strip()
50             elif not strip.startswith('/*') and not strip.startswith('*') and strip.endswith(','):
51                 this_type = find_type(strip[:-1])
52                 if this_type == type:
53                     text = re.sub(r"\[.*?\]", lambda m: f'(Bv2.1 {m[0][7:-1]})', text)
54                     text = text.replace('<', '&lt;')
55                     text = text.replace('>', '&gt;')
56                     text = re.sub(r"_(.*?)_", r"<code>\1</code>", text)
57                     print(f'<listitem>{text}.</listitem>')
58
59 print('</itemizedlist>')