Fix run cleanup if the UI ends without clearing non-Hooke msgs from the Queue.
[hooke.git] / hooke / hooke.py
index 859d634a2053ca5b9562887a07cb18763bf1b0b1..c2ef159d8bccc4bbbfe5ac5b789ad52ae55132f1 100644 (file)
@@ -5,15 +5,15 @@
 #
 # This file is part of Hooke.
 #
-# Hooke 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 3 of the License, or (at your option) any later version.
+# Hooke 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 3 of the
+# License, or (at your option) any later version.
 #
-# Hooke 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.
+# Hooke 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 Hooke.  If not, see
@@ -54,10 +54,14 @@ if False: # Queue pickle error debugging code
         feed(buffer, notempty, s, writelock, close)
     multiprocessing.queues.Queue._feed = staticmethod(new_feed)
 
+import logging
+import logging.config
 import multiprocessing
 import optparse
 import os.path
+import Queue
 import unittest
+import StringIO
 import sys
 
 from . import engine as engine
@@ -81,13 +85,21 @@ class Hooke (object):
                 default_settings=default_settings)
             config.read()
         self.config = config
+        self.load_log()
         self.load_plugins()
         self.load_drivers()
         self.load_ui()
         self.command = engine.CommandEngine()
-
         self.playlists = playlist.NoteIndexList()
 
+    def load_log(self):
+        config_file = StringIO.StringIO()
+        self.config.write(config_file)
+        logging.config.fileConfig(StringIO.StringIO(config_file.getvalue()))
+        # Don't attach the logger because it contains an unpicklable
+        # thread.lock.  Instead, grab it directly every time you need it.
+        #self.log = logging.getLogger('hooke')
+
     def load_plugins(self):
         self.plugins = plugin_mod.load_graph(
             plugin_mod.PLUGIN_GRAPH, self.config, include_section='plugins')
@@ -145,9 +157,14 @@ class HookeRunner (object):
         return (ui_to_command, command_to_ui, command)
 
     def _cleanup_run(self, ui_to_command, command_to_ui, command):
+        log = logging.getLogger('hooke')
+        log.debug('cleanup sending CloseEngine')
         ui_to_command.put(ui.CloseEngine())
-        hooke = command_to_ui.get()
-        assert isinstance(hooke, Hooke)
+        hooke = None
+        while not isinstance(hooke, Hooke):
+            log.debug('cleanup waiting for Hooke instance from the engine.')
+            hooke = command_to_ui.get(block=True)
+            log.debug('cleanup got %s instance' % type(hooke))
         command.join()
         return hooke
 
@@ -163,9 +180,9 @@ def main():
         help='Add a command line Hooke command to run.')
     options,arguments = p.parse_args()
     if len(arguments) > 0:
-        print >> sys.stderr, 'Too many arguments to %s: %d > 0' \
-            % (sys.argv[0], len(arguments))
-        print >> sys.stderr, p.help()
+        print >> sys.stderr, 'More than 0 arguments to %s: %s' \
+            % (sys.argv[0], arguments)
+        p.print_help(sys.stderr)
         sys.exit(1)
 
     hooke = Hooke(debug=__debug__)