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