From: Serge Rehbinder Date: Wed, 27 Jan 2016 15:15:16 +0000 (+0100) Subject: Add help prints and terminal command mechanism X-Git-Tag: sprint-01~10 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=0ac0d584b3ad847fdaabd94db760c693734da0a2;p=tools%2Fsat.git Add help prints and terminal command mechanism --- diff --git a/src/common/options.py b/src/common/options.py index e3c5545..97e73e8 100644 --- a/src/common/options.py +++ b/src/common/options.py @@ -130,7 +130,7 @@ class Options: # call to getopt.getopt function to get the option passed in the command regarding the available options optlist, args = getopt.getopt(argList, shortNameOption, longNameOption) - + # instantiate and completing the optResult that will be returned optResult = OptResult() for option in self.options: @@ -163,6 +163,5 @@ class Options: option['result'].extend(elts) optResult.__setattr__(option['destName'], option['result']) - return optResult, args diff --git a/src/config.py b/src/config.py index b82e4af..954075c 100644 --- a/src/config.py +++ b/src/config.py @@ -375,7 +375,11 @@ def print_value(config, path, show_label, level=0, show_full_path=False): index = index + 1 else: # case where val is just a str sys.stdout.write("%s\n" % val) - + +def description(): + return _("The config command allows manipulation and operation on config files.") + + def run(args, runner): (options, args) = parser.parse_args(args) print('Je suis dans la commande config ! Bien joué ! COUCOU') diff --git a/src/salomeTools.py b/src/salomeTools.py index 57f235a..e5d8b78 100755 --- a/src/salomeTools.py +++ b/src/salomeTools.py @@ -62,19 +62,20 @@ class salomeTools(object): except Exception as exc: write_exception(exc) sys.exit(-1) - - if options.help: - try: - sat.print_help(args) - except Exception as exc: - code = 1 - write_exception(exc) - + self.__dict__ = dict() self.options = options self.dataDir = dataDir # set the commands self.__setCommands__(srcdir) + + if options.help: + try: + self.print_help(args) + sys.exit(0) + except Exception as exc: + code = 1 + write_exception(exc) def __getattr__(self, name): if name in self.__dict__: @@ -85,6 +86,7 @@ class salomeTools(object): def __setattr__(self, name, value): object.__setattr__(self,name,value) + def __setCommands__(self, dirPath): for nameCmd in lCommand: (file_, pathname, description) = imp.find_module(nameCmd, [dirPath]) @@ -112,17 +114,47 @@ class salomeTools(object): func = types.FunctionType(run_command.__code__, globals_up, run_command.__name__,run_command.__defaults__, run_command.__closure__) self.__setattr__(nameCmd, func) - + def print_help(self, argv): + '''Prints help for a command + ''' + command = argv[0] + # read the configuration from all the pyconf files + cfgManager = config.ConfigManager() + self.cfg = cfgManager.getConfig(dataDir=self.dataDir) + if not hasattr(self, command): + raise common.SatException(_("Command '%s' does not exist") % command) -def print_help(options): - #from config import ConfigManager - #cfgManager = ConfigManager(STAND_ALONE_VERSION) - #cfg = cfgManager.getConfig(None, options) - #print_version(cfg) - #print + print_version() + + module = self.get_module(command) + + if hasattr( module, "description" ) : + print(common.printcolors.printcHeader( _("Description:") )) + print(module.description() + '\n') + + if hasattr( module, "parser" ) : + module.parser.print_help() + + def get_module(self, module): + if not hasattr(self, module): + raise common.SatException(_("Command '%s' does not exist") % module) + + # reduce variables substitution and so on + (file_, pathname, description) = imp.find_module(module, [srcdir]) + module = imp.load_module(module, file_, pathname, description) + return module + +def print_version(): + cfgManager = config.ConfigManager() + cfg = cfgManager.getConfig() + print(common.printcolors.printcHeader( _("Version: ") ) + cfg.INTERNAL.sat_version + '\n') + +def print_help(options): + print_version() + print(common.printcolors.printcHeader( _("Usage: ") ) + "sat [sat_options] [product] [command_options]\n") parser.print_help() @@ -147,7 +179,7 @@ if __name__ == "__main__": cmd_line = " ".join(sys.argv) # Initialize the code that will be returned by the terminal command code = 0 - + (options, args) = parser.parse_args(sys.argv[1:]) if len(args) == 0: @@ -155,6 +187,12 @@ if __name__ == "__main__": print_help(options) else: sat = salomeTools(sys.argv[1:]) + command = args[0] + fun_command = sat.__getattr__(command) + if len(args[1:]) == 0: + print(common.printcolors.printcWarning(_("No arguments"))) + sys.exit(0) + fun_command(' '.join(args[1:])) diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..39673e7 --- /dev/null +++ b/test/test.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +#-*- coding:utf-8 -*- +# Copyright (C) 2010-2012 CEA/DEN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import os, sys + +# get execution path +testdir = os.path.dirname(os.path.realpath(__file__)) +sys.path.append(os.path.join(testdir, '..', 'src')) + +from salomeTools import salomeTools + +sat = salomeTools('--help') +sat.config('appli-test -v APPLICATION')