Salome HOME
Add help prints and terminal command mechanism
authorSerge Rehbinder <serge.rehbinder@cea.fr>
Wed, 27 Jan 2016 15:15:16 +0000 (16:15 +0100)
committerSerge Rehbinder <serge.rehbinder@cea.fr>
Wed, 27 Jan 2016 15:15:16 +0000 (16:15 +0100)
src/common/options.py
src/config.py
src/salomeTools.py
test/test.py [new file with mode: 0644]

index e3c55456c4ccf4cbfcc8ae4be8afd6537b24a247..97e73e854debb8b172ec39b28c52319f23ee7481 100644 (file)
@@ -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
 
index b82e4af38a67778464e71569005a4d442c17e0b9..954075c8cd6f94a66bc19ac4b1546e2a20152226 100644 (file)
@@ -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')
index 57f235a3b992ec4245d9dfd82dbad630aa346974..e5d8b78b9726d0b404a89ad6b1be8050f6406730 100755 (executable)
@@ -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] <command> [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 (file)
index 0000000..39673e7
--- /dev/null
@@ -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')