Belle II Software  light-2403-persian
doxygen_filter.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import sys
12 import os
13 import re
14 
15 
16 image_path = 'build/module_io'
17 files = []
18 if os.path.isdir(image_path):
19  files = os.listdir(image_path)
20 
21 # list of modules with I/O plots, _without_ "Module" suffix
22 modules_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
25 headers = [m.lower() + 'module.h' for m in modules_with_plots]
26 
27 filename = sys.argv[1]
28 subdirs = os.path.relpath(filename).split(os.path.sep)
29 group = subdirs[0]
30 if 'modules' in subdirs:
31  group += '_modules'
32 elif 'dataobjects' in subdirs:
33  group += '_dataobjects'
34 
35 found_idx = None
36 try:
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'
41 except Exception:
42  pass
43 
44 belle2ns = False
45 for 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
89 try:
90  sys.stdout.close()
91 except Exception:
92  pass
93 try:
94  sys.stderr.close()
95 except Exception:
96  pass