Salome HOME
Python 3 porting
[modules/geom.git] / doc / salome / gui / GEOM / collect_geom_methods.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2012-2016  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 #################################################################################
22 #
23 # File:   collect_geom_methods.py
24 # Author: Roman NIKOLAEV, Open CASCADE S.A.S (roman.nikolaev@opencascade.com)
25 #
26 #################################################################################
27 #
28 # Extraction of the methods dynamically added by the Geometry 
29 # module plug-in(s) to the geomBuilder class.
30
31 # This script is intended for internal usage - only
32 # for generatation of the extra developer documentation for
33 # the Geometry module plug-in(s).
34
35 # Usage:
36 #       collect_geom_methods.py <plugin_name>
37 # where
38 #   <plugin_name> is a name of the plug-in module
39 #
40 # Notes:
41 # - the script is supposed to be run in correct environment
42 # i.e. PYTHONPATH, GEOM_PluginsList and other important
43 # variables are set properly; otherwise the script will fail.
44 #
45 ################################################################################
46
47 import sys
48 import inspect
49
50 def generate(plugin_name, output):
51     import types
52     plugin_module_name  = plugin_name + "Builder"
53     plugin_module       = "salome.%s.%s" % (plugin_name, plugin_module_name)
54     import_str          = "from salome.%s import %s" % (plugin_name, plugin_module_name)
55     execLine            = "from salome.%s import %s\nimport %s\nmod = %s" % (plugin_name, plugin_module_name, plugin_module, plugin_module)
56     print(execLine)
57     namespace = {}
58     exec(execLine , namespace)
59     functions = []
60     for attr in dir(namespace["mod"]):
61         if attr.startswith( '_' ): continue # skip an internal methods
62         item = getattr(namespace["mod"], attr)
63         if isinstance(item, types.FunctionType):
64             if item not in functions:
65                 functions.append(item)
66                 pass
67             pass
68         pass
69     if functions:
70         for function in functions:
71             comments = inspect.getcomments(function)
72             if comments:
73                 comments = comments.strip().split("\n")
74                 comments = "\t" + "\n\t".join(comments)
75                 output.append(comments)
76                 pass                
77             sources = inspect.getsource(function)
78             if sources is not None:
79                 sources_list = sources.split("\n")
80                 sources_new_list = []
81                 found = False
82                 for item in sources_list:
83                     if '"""' in item:
84                         if found == True:
85                             found = False                                
86                             continue
87                         else:
88                             found = True
89                             continue
90                             pass
91                         pass                
92                     if found == False:
93                         sources_new_list.append(item)
94                         pass
95                     pass
96                 sources = "\n".join(sources_new_list)
97                 sources = "\t" + sources.replace("\n", "\n\t")
98                 output.append(sources)
99                 pass
100             pass
101         pass
102     pass
103
104 if __name__ == "__main__":
105     import argparse
106     parser = argparse.ArgumentParser()
107     h  = "Output file (geomBuilder.py by default)"
108     parser.add_argument("-o", "--output", dest="output",
109                         action="store", default='geomBuilder.py', metavar="file",
110                         help=h)
111     h  = "If this option is True, dummy help for geomBuiler class is added. "
112     h += "This option should be False (default) when building documentation for Geometry module "
113     h += "and True when building documentation for Geometry module plug-ins."
114     parser.add_argument("-d", "--dummy-geom-help", dest="dummygeomhelp",
115                         action="store_true", default=False,
116                         help=h)
117     parser.add_argument("plugin", nargs='+', help='Name of plugin')
118     args = parser.parse_args()
119
120     plugins_names = " ".join(args.plugin) + 'plugin'
121     if len(args.plugin) > 1:
122         plugins_names += 's'
123     output = []
124     if args.dummygeomhelp:
125         output.append("## @package geomBuilder")
126         output.append("#  Documentation of the methods dynamically added by the " + plugins_names + " to the @b %geomBuilder class.")
127         # Add dummy Geometry help
128         # This is supposed to be done when generating documentation for Geometry module plug-ins
129         output.append("#  @note The documentation below does not provide complete description of class @b %geomBuilder")
130         output.append("#  from @b geomBuilder package. This documentation provides only information about")
131         output.append("#  the methods dynamically added to the %geomBuilder class by the " + plugins_names + ".")
132         output.append("#  For more details on the %geomBuilder class, please refer to the SALOME %Geometry module")
133         output.append("#  documentation.")
134         pass
135     else:
136         # Extend documentation for geomBuilder class with information about dynamically added methods.
137         # This is supposed to be done only when building documentation for Geometry module
138         output.append("## @package geomBuilder")
139         output.append("#  @note Some methods are dynamically added to the @b %geomBuilder class in runtime by the")
140         output.append("#  plug-in modules. If you fail to find help on some methods in the documentation of Geometry module, ")
141         output.append("#  try to look into the documentation for the Geometry module plug-ins.")
142         pass
143     output.append("class geomBuilder():")
144     
145     for plugin_name in args.plugin:
146        generate( plugin_name, output )
147     pass
148
149     with open(args.output, "w", encoding='utf8') as f:
150         f.write('\n'.join(output))
151