Salome HOME
Merge from V6_main 13/12/2012
[modules/kernel.git] / salome_adm / cmake_files / prepare_generating_doc.py
1 #!/usr/bin/env python
2 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # ...
22 # Usage: prepare_generating_doc.py <output_file> <input_file> <myClass>
23 # ...
24 # 1. myClassDC is replaced by myClass
25 # 2. all methods of myClassDC become global methods of Python package
26 # ...
27 # myClass is passed as command line argument
28 # ...
29
30 import os, sys, re
31
32 # check command line
33 if len( sys.argv ) < 4:
34     sys.exit("Usage: %s <output_file> <input_file> <myClass>" % os.path.basename(sys.argv[0]))
35
36 # open input file
37 try:
38     infile = open(sys.argv[2], 'rb')
39 except:
40     sys.exit("File %s is not found" % sys.argv[2])
41     pass
42
43 # open output file
44 try:
45     outfile = open(sys.argv[1], 'wb')
46 except:
47     sys.exit("File %s cannot be opened for write" % sys.argv[1])
48     pass
49
50 # parse input file
51
52 isCom   = False
53 isShift = False
54
55 for line in infile.readlines():
56     dc_class    = sys.argv[3]    
57     dc_class_dc = dc_class+'DC'
58
59     if isShift and ( re.match('^class\s+', line) or re.match('^def\s+', line) ):
60         # stop shifting lines as soon as myClassDC definition is over
61         isShift = False
62         pass
63     if re.match('class\s+%s' % dc_class_dc, line):
64         # start shifting lines
65         isShift = True
66         # omit this line (to remove myClassDC from the package)
67         continue
68     
69     # process documentation
70     n = line.find('"""')
71     n1 = line[(n+2):].find('"""')
72     if (n > -1) and (n1 > -1):
73         continue
74     if isCom:
75         if n > -1:
76             isCom = False
77             pass
78         continue
79     else:
80         if n > -1:
81             isCom = True
82             continue
83         pass
84
85     # replacements
86     if isShift:
87         line = re.sub(r'^\s+#', '#', line) 
88         line = re.sub(r'^\s+def', 'def', line)
89         pass
90     line = re.sub(dc_class_dc, dc_class, line)
91
92     # write resulting line
93     outfile.write(line)
94
95     pass # end of for
96
97 # close input and output files
98 infile.close()
99 outfile.close()