Salome HOME
8c541963c46baf78b8d5e492188547e95bbfa8a5
[tools/medcoupling.git] / doc / developer / doxygen / BuildPyExamplesFromCPP.py
1 #!/usr/bin/env python
2 # Copyright (C) 2007-2019  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, or (at your option) any later version.
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 import sys
22 import re
23 import os
24
25 # :TRICKY:
26 # In input file, contents delimited by two lines containing BEGIN_CPP_ONLY and
27 # END_CPP_ONLY strings respectively, will only appear in C++ documentation.
28 # The same rule applies for Python-specific contents.
29
30 def Cpp2Python(contents):
31     cpp_only = False
32     output = []
33     for st in contents:
34         if "BEGIN_CPP_ONLY" in st:
35             cpp_only = True
36         elif "END_CPP_ONLY" in st:
37             cpp_only = False
38         elif not cpp_only:
39             st=st.replace("C++","Python")
40             st=st.replace("Cxx","Py")
41             st=st.replace("Cpp","Py")
42             st=st.replace("cxx","py")
43             st=st.replace("cpp","py")
44             output.append(st)
45             pass
46         pass
47
48     return output
49 #
50 def discardPythonFrom(contents):
51     python_only = False
52     output = []
53     for st in contents:
54         if "BEGIN_PYTHON_ONLY" in st:
55             python_only = True
56         elif "END_PYTHON_ONLY" in st:
57             python_only = False
58         elif not python_only:
59             output.append(st)
60             pass
61         pass
62
63     return output
64     #
65 #
66
67 # Usage: BuildPyExamplesFromCPP.py <examples.in> <output directory>
68
69 with open(sys.argv[1], "r") as fCpp:
70     cppCont = fCpp.readlines()
71 pyCont=cppCont[:]
72 pyCont=[elt.replace("medcouplingcppexamples","medcouplingpyexamples") for elt in pyCont]
73 pyCont=Cpp2Python(pyCont)
74
75 s= "Be sure to take a look at the page \\ref python-api before proceeding."
76 pyCont.insert(2, s)
77 cppCont=discardPythonFrom(cppCont) # remove Python-only contents from Cpp
78
79 # Save CPP and PY examples in two separate dox files
80 outFileName=os.path.join(sys.argv[2],os.path.basename(sys.argv[1]))
81
82 with open(os.path.splitext(outFileName)[0] + "CPP.dox", "w") as f:
83     f.writelines(cppCont)
84
85 with open(os.path.splitext(outFileName)[0] + "PY.dox", "w") as f:
86     f.writelines(pyCont)