Log execution debug messages from the commandline UI (matching the GUI).
[hooke.git] / hooke / ui / commandline.py
index c601ddcc27e9f2bb6568274508503142567bbe7c..53291a25e1a4df2420fb0f43dcfbf1e1302d8b61 100644 (file)
@@ -22,13 +22,19 @@ line.
 
 import codecs
 import cmd
 
 import codecs
 import cmd
+import logging
 import optparse
 import optparse
-import readline # including readline makes cmd.Cmd.cmdloop() smarter
+try:
+    import readline # including readline makes cmd.Cmd.cmdloop() smarter
+except ImportError, e:
+    import logging
+    logging.warn('Could not import readline, bash-like line editing disabled.')
 import shlex
 
 from ..command import CommandExit, Exit, Command, Argument, StoreValue
 from ..interaction import Request, BooleanRequest, ReloadUserInterfaceConfig
 from ..ui import UserInterface, CommandMessage
 import shlex
 
 from ..command import CommandExit, Exit, Command, Argument, StoreValue
 from ..interaction import Request, BooleanRequest, ReloadUserInterfaceConfig
 from ..ui import UserInterface, CommandMessage
+from ..util.convert import from_string
 from ..util.encoding import get_input_encoding, get_output_encoding
 
 
 from ..util.encoding import get_input_encoding, get_output_encoding
 
 
@@ -53,8 +59,27 @@ class CommandLineParser (optparse.OptionParser):
                 continue # 'help' is a default OptionParser option
             if a.optional == True:
                 name = name_fn(a.name)
                 continue # 'help' is a default OptionParser option
             if a.optional == True:
                 name = name_fn(a.name)
+                type = a.type
+                if type == 'bool':
+                    if a.default == True:
+                        self.add_option(
+                            '--disable-%s' % name, dest=name, default=Default,
+                            action='store_false')
+                        self.command_opts.append(a)
+                        continue
+                    elif a.default == False:
+                        self.add_option(
+                            '--enable-%s' % name, dest=name, default=Default,
+                            action='store_true')
+                        self.command_opts.append(a)
+                        continue
+                    else:
+                        type = 'string'
+                elif type not in ['string', 'int', 'long', 'choice', 'float',
+                                  'complex']:
+                    type = 'string'
                 self.add_option(
                 self.add_option(
-                    '--%s' % name, dest=name, default=Default)
+                    '--%s' % name, dest=name, type=type, default=Default)
                 self.command_opts.append(a)
             else:
                 self.command_args.append(a)
                 self.command_opts.append(a)
             else:
                 self.command_args.append(a)
@@ -94,6 +119,7 @@ class DoCommand (CommandMethod):
     def __init__(self, *args, **kwargs):
         super(DoCommand, self).__init__(*args, **kwargs)
         self.parser = CommandLineParser(self.command, self.name_fn)
     def __init__(self, *args, **kwargs):
         super(DoCommand, self).__init__(*args, **kwargs)
         self.parser = CommandLineParser(self.command, self.name_fn)
+        self.log = logging.getLogger('hooke')
 
     def __call__(self, args):
         try:
 
     def __call__(self, args):
         try:
@@ -102,6 +128,7 @@ class DoCommand (CommandMethod):
             self.cmd.stdout.write(str(e).lstrip()+'\n')
             self.cmd.stdout.write('Failure\n')
             return
             self.cmd.stdout.write(str(e).lstrip()+'\n')
             self.cmd.stdout.write('Failure\n')
             return
+        self.log.debug('executing %s with %s' % (self.command.name, args))
         self.cmd.inqueue.put(CommandMessage(self.command, args))
         while True:
             msg = self.cmd.outqueue.get()
         self.cmd.inqueue.put(CommandMessage(self.command, args))
         while True:
             msg = self.cmd.outqueue.get()
@@ -130,12 +157,15 @@ class DoCommand (CommandMethod):
         arg_index = 0
         for argument in self.parser.command_args:
             if argument.count == 1:
         arg_index = 0
         for argument in self.parser.command_args:
             if argument.count == 1:
-                params[argument.name] = args[arg_index]
+                params[argument.name] = from_string(args[arg_index],
+                                                    argument.type)
             elif argument.count > 1:
             elif argument.count > 1:
-                params[argument.name] = \
-                    args[arg_index:arg_index+argument.count]
+                params[argument.name] = [
+                    from_string(a, argument.type)
+                    for a in args[arg_index:arg_index+argument.count]]
             else: # argument.count == -1:
             else: # argument.count == -1:
-                params[argument.name] = args[arg_index:]
+                params[argument.name] = [
+                    from_string(a, argument.type) for a in args[arg_index:]]
             arg_index += argument.count
         return params
 
             arg_index += argument.count
         return params
 
@@ -217,12 +247,17 @@ class DoCommand (CommandMethod):
         return msg.msg + d
 
     def _string_request_parser(self, msg, response):
         return msg.msg + d
 
     def _string_request_parser(self, msg, response):
+        response = response.strip()
+        if response == '':
+            return msg.default
         return response.strip()
 
     def _float_request_prompt(self, msg):
         return self._string_request_prompt(msg)
 
     def _float_request_parser(self, msg, resposne):
         return response.strip()
 
     def _float_request_prompt(self, msg):
         return self._string_request_prompt(msg)
 
     def _float_request_parser(self, msg, resposne):
+        if response.strip() == '':
+            return msg.default
         return float(response)
 
     def _selection_request_prompt(self, msg):
         return float(response)
 
     def _selection_request_prompt(self, msg):
@@ -234,9 +269,29 @@ class DoCommand (CommandMethod):
             prompt = '? '
         else:
             prompt = '? [%d] ' % msg.default
             prompt = '? '
         else:
             prompt = '? [%d] ' % msg.default
-        return '\n'.join([msg,options,prompt])
+        return '\n'.join([msg.msg,options,prompt])
     
     def _selection_request_parser(self, msg, response):
     
     def _selection_request_parser(self, msg, response):
+        if response.strip() == '':
+            return msg.default
+        return int(response)
+
+    def _point_request_prompt(self, msg):
+        block = msg.curve.data[msg.block]
+        block_info = ('(curve: %s, block: %s, %d points)'
+                      % (msg.curve.name,
+                         block.info['name'],
+                         block.shape[0]))
+
+        if msg.default == None:
+            prompt = '? '
+        else:
+            prompt = '? [%d] ' % msg.default
+        return ' '.join([msg.msg,block_info,prompt])
+    
+    def _point_request_parser(self, msg, response):
+        if response.strip() == '':
+            return msg.default
         return int(response)
 
 
         return int(response)
 
 
@@ -374,11 +429,13 @@ class HookeCmd (cmd.Cmd):
         argv = shlex.split(line, comments=True, posix=True)
         if len(argv) == 0:
             return None, None, '' # return an empty line
         argv = shlex.split(line, comments=True, posix=True)
         if len(argv) == 0:
             return None, None, '' # return an empty line
-        elif argv[0] == '?':
-            argv[0] = 'help'
-        elif argv[0] == '!':
-            argv[0] = 'system'
-        return argv[0], argv[1:], line
+        cmd = argv[0]
+        args = argv[1:]
+        if cmd == '?':
+            cmd = 'help'
+        elif cmd == '!':
+            cmd = 'system'
+        return cmd, args, line
 
     def do_help(self, arg):
         """Wrap Cmd.do_help to handle our .parseline argument list.
 
     def do_help(self, arg):
         """Wrap Cmd.do_help to handle our .parseline argument list.
@@ -387,7 +444,7 @@ class HookeCmd (cmd.Cmd):
             return cmd.Cmd.do_help(self, '')
         return cmd.Cmd.do_help(self, arg[0])
 
             return cmd.Cmd.do_help(self, '')
         return cmd.Cmd.do_help(self, arg[0])
 
-    def empytline(self):
+    def emptyline(self):
         """Override Cmd.emptyline to not do anything.
 
         Repeating the last non-empty command seems unwise.  Explicit
         """Override Cmd.emptyline to not do anything.
 
         Repeating the last non-empty command seems unwise.  Explicit
@@ -395,6 +452,7 @@ class HookeCmd (cmd.Cmd):
         """
         pass
 
         """
         pass
 
+
 class CommandLine (UserInterface):
     """Command line interface.  Simple and powerful.
     """
 class CommandLine (UserInterface):
     """Command line interface.  Simple and powerful.
     """