Belle II Software light-2406-ragdoll
doxygen_filter.py
1#!/usr/bin/env python3
2
3
10
11import sys
12import os
13import re
14
15
16image_path = 'build/module_io'
17files = []
18if os.path.isdir(image_path):
19 files = os.listdir(image_path)
20
21# list of modules with I/O plots, _without_ "Module" suffix
22modules_with_plots = [f[:-4] for f in files if re.match(r'.*.png', f) and os.stat(os.path.join(image_path, f)).st_size > 0]
23
24# corresponding header files, in lower case
25headers = [m.lower() + 'module.h' for m in modules_with_plots]
26
27filename = sys.argv[1]
28subdirs = os.path.relpath(filename).split(os.path.sep)
29group = subdirs[0]
30if 'modules' in subdirs:
31 group += '_modules'
32elif 'dataobjects' in subdirs:
33 group += '_dataobjects'
34
35found_idx = None
36try:
37 found_idx = headers.index(os.path.basename(filename).lower())
38 # ok, we've got a header with corresponding plot
39 module = modules_with_plots[found_idx]
40 classname = module + 'Module'
41except Exception:
42 pass
43
44belle2ns = False
45for line in open(filename):
46
47 # python comments
48 try:
49 line = line.replace('## ', '## ')
50 except OSError:
51 pass # doxygen closes pipe for whatever reason
52
53 # module io plot
54 if found_idx is not None:
55 # TODO: also allow other whitespace after class?
56 if re.match(r'.*class ' + classname + '.*', line):
57 # found class declaration, add comment before it
58 print('''/**
59* \\image html ''' + module + '''.png
60*/''')
61
62 # end of belle2 namespace -> end grouping
63 try:
64 if re.match(r'^}.*', line) and belle2ns:
65 belle2ns = False
66 print(' /*! @} */')
67 except OSError:
68 pass # doxygen closes pipe for whatever reason
69
70 # addtogroup
71 try:
72 print('/**')
73 sys.stdout.write(line) # print raw, without added \n
74 except OSError:
75 pass # doxygen closes pipe for whatever reason
76
77 # beginning of belle2 namespace -> start grouping
78 try:
79 if re.match(r'namespace Belle2 {.*', line):
80 belle2ns = True
81 print(f""" /**
82 * @addtogroup {group}
83 * @{{
84 */""")
85 except OSError:
86 pass # doxygen closes pipe for whatever reason
87
88# avoid some further errors with lost stdout/stderr
89try:
90 sys.stdout.close()
91except Exception:
92 pass
93try:
94 sys.stderr.close()
95except Exception:
96 pass