Python 3 porting

optparse replaced by argparse
This commit is contained in:
Gilles DAVID 2017-05-24 10:06:22 +02:00
parent d6d9a9dab6
commit 6f199e6cff

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2016 CEA/DEN, EDF R&D, OPEN CASCADE # Copyright (C) 2012-2016 CEA/DEN, EDF R&D, OPEN CASCADE
# #
# This library is free software; you can redistribute it and/or # This library is free software; you can redistribute it and/or
@ -49,20 +48,21 @@ import sys
import inspect import inspect
def generate(plugin_name, output): def generate(plugin_name, output):
import types
plugin_module_name = plugin_name + "Builder" plugin_module_name = plugin_name + "Builder"
plugin_module = "salome.%s.%s" % (plugin_name, plugin_module_name) plugin_module = "salome.%s.%s" % (plugin_name, plugin_module_name)
import_str = "from salome.%s import %s" % (plugin_name, plugin_module_name) import_str = "from salome.%s import %s" % (plugin_name, plugin_module_name)
execLine = "from salome.%s import %s\nimport %s\nmod = %s" % (plugin_name, plugin_module_name,plugin_module,plugin_module) execLine = "from salome.%s import %s\nimport %s\nmod = %s" % (plugin_name, plugin_module_name, plugin_module, plugin_module)
print(execLine) print(execLine)
namespace = {} namespace = {}
exec( execLine , namespace) exec(execLine , namespace)
functions = [] functions = []
for attr in dir( namespace["mod"] ): for attr in dir(namespace["mod"]):
if attr.startswith( '_' ): continue # skip an internal methods if attr.startswith( '_' ): continue # skip an internal methods
item = getattr( namespace["mod"], attr ) item = getattr(namespace["mod"], attr)
if type( item ).__name__ == 'function': if isinstance(item, types.FunctionType):
if item not in functions: if item not in functions:
functions.append( item ) functions.append(item)
pass pass
pass pass
pass pass
@ -89,7 +89,7 @@ def generate(plugin_name, output):
continue continue
pass pass
pass pass
if found == False : if found == False:
sources_new_list.append(item) sources_new_list.append(item)
pass pass
pass pass
@ -102,54 +102,50 @@ def generate(plugin_name, output):
pass pass
if __name__ == "__main__": if __name__ == "__main__":
import optparse import argparse
parser = optparse.OptionParser(usage="%prog [options] plugin") parser = argparse.ArgumentParser()
h = "Output file (geomBuilder.py by default)" h = "Output file (geomBuilder.py by default)"
parser.add_option("-o", "--output", dest="output", parser.add_argument("-o", "--output", dest="output",
action="store", default=None, metavar="file", action="store", default='geomBuilder.py', metavar="file",
help=h) help=h)
h = "If this option is True, dummy help for geomBuiler class is added. " h = "If this option is True, dummy help for geomBuiler class is added. "
h += "This option should be False (default) when building documentation for Geometry module " h += "This option should be False (default) when building documentation for Geometry module "
h += "and True when building documentation for Geometry module plug-ins." h += "and True when building documentation for Geometry module plug-ins."
parser.add_option("-d", "--dummy-geom-help", dest="dummygeomhelp", parser.add_argument("-d", "--dummy-geom-help", dest="dummygeomhelp",
action="store_true", default=False, action="store_true", default=False,
help=h) help=h)
(options, args) = parser.parse_args() parser.add_argument("plugin", nargs='+', help='Name of plugin')
if len( args ) < 1: sys.exit("Plugin name is not specified") args = parser.parse_args()
f = open(options.output, "w") plugins_names = " ".join(args.plugin) + 'plugin'
if len(args.plugin) > 1:
if len(args) > 1: plugins_names += 's'
plugins_names = " ".join(args) + " plugins"
elif len(args) == 1:
plugins_names = args[0] + " plugin"
else:
plugins_names = ""
output = [] output = []
if options.dummygeomhelp: if args.dummygeomhelp:
output.append( "## @package geomBuilder" ) output.append("## @package geomBuilder")
output.append( "# Documentation of the methods dynamically added by the " + plugins_names + " to the @b %geomBuilder class." ) output.append("# Documentation of the methods dynamically added by the " + plugins_names + " to the @b %geomBuilder class.")
# Add dummy Geometry help # Add dummy Geometry help
# This is supposed to be done when generating documentation for Geometry module plug-ins # This is supposed to be done when generating documentation for Geometry module plug-ins
output.append( "# @note The documentation below does not provide complete description of class @b %geomBuilder" ) output.append("# @note The documentation below does not provide complete description of class @b %geomBuilder")
output.append( "# from @b geomBuilder package. This documentation provides only information about" ) output.append("# from @b geomBuilder package. This documentation provides only information about")
output.append( "# the methods dynamically added to the %geomBuilder class by the " + plugins_names + "." ) output.append("# the methods dynamically added to the %geomBuilder class by the " + plugins_names + ".")
output.append( "# For more details on the %geomBuilder class, please refer to the SALOME %Geometry module" ) output.append("# For more details on the %geomBuilder class, please refer to the SALOME %Geometry module")
output.append( "# documentation." ) output.append("# documentation.")
pass pass
else: else:
# Extend documentation for geomBuilder class with information about dynamically added methods. # Extend documentation for geomBuilder class with information about dynamically added methods.
# This is supposed to be done only when building documentation for Geometry module # This is supposed to be done only when building documentation for Geometry module
output.append( "## @package geomBuilder" ) output.append("## @package geomBuilder")
output.append( "# @note Some methods are dynamically added to the @b %geomBuilder class in runtime by the" ) output.append("# @note Some methods are dynamically added to the @b %geomBuilder class in runtime by the")
output.append( "# plug-in modules. If you fail to find help on some methods in the documentation of Geometry module, " ) output.append("# plug-in modules. If you fail to find help on some methods in the documentation of Geometry module, ")
output.append( "# try to look into the documentation for the Geometry module plug-ins." ) output.append("# try to look into the documentation for the Geometry module plug-ins.")
pass pass
output.append("class geomBuilder():") output.append("class geomBuilder():")
for arg in args: for plugin_name in args.plugin:
generate( arg, output ) generate( plugin_name, output )
pass pass
for line in output: f.write( line + "\n" ) with open(args.output, "w", encoding='utf8') as f:
f.close() f.write('\n'.join(output))