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