Add support for raster and constant data to JPK driver.
[hooke.git] / hooke / ui / commandline.py
index 192918edfe24b846160d33440d9ad69dbf473625..c82d5c5e9a47c29acb7a1dddc43368e4a5bf6ed5 100644 (file)
@@ -24,16 +24,18 @@ import codecs
 import cmd
 import logging
 import optparse
+import pprint
 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.')
+    logging.warn('could not import readline, bash-like line editing disabled.')
 import shlex
+import sys
 
 from ..command import CommandExit, Exit, Command, Argument, StoreValue
-from ..engine import CommandMessage
-from ..interaction import Request, ReloadUserInterfaceConfig
+from ..engine import CommandMessage, CloseEngine
+from ..interaction import EOFResponse, Request, ReloadUserInterfaceConfig
 from ..ui import UserInterface
 from ..util.convert import from_string
 from ..util.encoding import get_input_encoding, get_output_encoding
@@ -41,6 +43,13 @@ from ..util.encoding import get_input_encoding, get_output_encoding
 
 # Define a few helper classes.
 
+class EOF (EOFError):
+    """Raise upon reaching the end of the input file.
+
+    After this point, no more user interaction is possible.
+    """
+    pass
+
 class Default (object):
     """Marker for options not given on the command line.
     """
@@ -63,15 +72,25 @@ class CommandLineParser (optparse.OptionParser):
                 type = a.type
                 if type == 'bool':
                     if a.default == True:
-                        self.add_option(
-                            '--disable-%s' % name, dest=name, default=Default,
-                            action='store_false')
+                        try:
+                            self.add_option(
+                                '--disable-%s' % name, dest=name,
+                                default=Default, action='store_false',
+                                help=self._argument_help(a))
+                        except optparse.OptionConflictError, e:
+                            logging.warn('error in %s: %s' % (command, e))
+                            raise
                         self.command_opts.append(a)
                         continue
                     elif a.default == False:
-                        self.add_option(
-                            '--enable-%s' % name, dest=name, default=Default,
-                            action='store_true')
+                        try:
+                            self.add_option(
+                                '--enable-%s' % name, dest=name,
+                                default=Default, action='store_true',
+                                help=self._argument_help(a))
+                        except optparse.OptionConflictError, e:
+                            logging.warn('error in %s: %s' % (command, e))
+                            raise
                         self.command_opts.append(a)
                         continue
                     else:
@@ -79,8 +98,13 @@ class CommandLineParser (optparse.OptionParser):
                 elif type not in ['string', 'int', 'long', 'choice', 'float',
                                   'complex']:
                     type = 'string'
-                self.add_option(
-                    '--%s' % name, dest=name, type=type, default=Default)
+                try:
+                    self.add_option(
+                        '--%s' % name, dest=name, type=type, default=Default,
+                        help=self._argument_help(a))
+                except optparse.OptionConflictError, e:
+                    logging.warn('error in %s: %s' % (command, e))
+                    raise
                 self.command_opts.append(a)
             else:
                 self.command_args.append(a)
@@ -93,6 +117,10 @@ class CommandLineParser (optparse.OptionParser):
             self.command_args.remove(infinite_counter)
             self.command_args.append(infinite_counter)
 
+    def _argument_help(self, argument):
+        return '%s (%s)' % (argument._help, argument.default)
+        # default in the case of callbacks, config-backed values, etc.?
+
     def exit(self, status=0, msg=None):
         """Override :meth:`optparse.OptionParser.exit` which calls
         :func:`sys.exit`.
@@ -120,33 +148,38 @@ class DoCommand (CommandMethod):
     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:
             args = self._parse_args(args)
         except optparse.OptParseError, e:
-            self.cmd.stdout.write(str(e).lstrip()+'\n')
+            self.cmd.stdout.write(unicode(e).lstrip()+'\n')
             self.cmd.stdout.write('Failure\n')
             return
         cm = CommandMessage(self.command.name, args)
-        self.log.debug('executing %s' % cm)
-        self.cmd.inqueue.put(cm)
+        self.cmd.ui._submit_command(cm, self.cmd.inqueue)
         while True:
             msg = self.cmd.outqueue.get()
             if isinstance(msg, Exit):
                 return True
             elif isinstance(msg, CommandExit):
                 self.cmd.stdout.write(msg.__class__.__name__+'\n')
-                self.cmd.stdout.write(str(msg).rstrip()+'\n')
+                self.cmd.stdout.write(unicode(msg).rstrip()+'\n')
                 break
             elif isinstance(msg, ReloadUserInterfaceConfig):
                 self.cmd.ui.reload_config(msg.config)
                 continue
             elif isinstance(msg, Request):
-                self._handle_request(msg)
+                try:
+                    self._handle_request(msg)
+                except EOF:
+                    return True
                 continue
-            self.cmd.stdout.write(str(msg).rstrip()+'\n')
+            if isinstance(msg, dict):
+                text = pprint.pformat(msg)
+            else:
+                text = unicode(msg)
+            self.cmd.stdout.write(text.rstrip()+'\n')
 
     def _parse_args(self, args):
         options,args = self.parser.parse_args(args)
@@ -214,9 +247,19 @@ class DoCommand (CommandMethod):
         while True:
             if error != None:
                 self.cmd.stdout.write(''.join([
-                        error.__class__.__name__, ': ', str(error), '\n']))
+                        error.__class__.__name__, ': ', unicode(error), '\n']))
             self.cmd.stdout.write(prompt_string)
-            value = parser(msg, self.cmd.stdin.readline())
+            stdin = sys.stdin
+            try:
+                sys.stdin = self.cmd.stdin
+                raw_response = raw_input()
+            except EOFError, e:
+                self.cmd.inqueue.put(EOFResponse())
+                self.cmd.inqueue.put(CloseEngine())
+                raise EOF
+            finally:
+                sys.stdin = stdin
+            value = parser(msg, raw_response)
             try:
                 response = msg.response(value)
                 break
@@ -305,14 +348,15 @@ class HelpCommand (CommandMethod):
         self.parser = CommandLineParser(self.command, self.name_fn)
 
     def __call__(self):
-        blocks = [self.command.help(name_fn=self.name_fn),
+        blocks = [self.parser.format_help(),
+                  self._command_message(),
                   '----',
                   'Usage: ' + self._usage_string(),
                   '']
         self.cmd.stdout.write('\n'.join(blocks))
 
-    def _message(self):
-        return self.command.help(name_fn=self.name_fn)
+    def _command_message(self):
+        return self.command._help
 
     def _usage_string(self):
         if len(self.parser.command_opts) == 0: