Browse files

Embed a interactive shell for debugging purpose

  • Loading branch information...
1 parent 6c320fc commit 36a63b3f54d24379fab60668e0f63ce02f76f652 @Mic92 Mic92 committed Mar 7, 2012
Showing with 30 additions and 2 deletions.
  1. +5 −0 sonata/cli.py
  2. +25 −2 sonata/launcher.py
View
5 sonata/cli.py
@@ -17,6 +17,7 @@ def __init__(self):
self.logger = logging.getLogger(__name__)
self.skip_gui = False
self.start_visibility = None
+ self.start_shell = False
def parse(self, argv):
"""Parse the command line arguments.
@@ -64,6 +65,9 @@ def parse(self, argv):
help=_("start app visible (requires systray)"))
parser.add_option("--profile", dest="profile", metavar="NUM",
help=_("start with profile NUM"), type=int)
+ parser.add_option("--shell", dest="start_shell",
+ action="store_true",
+ help=_("start app with a interactive (ipython) shell"))
parser.add_option("-v", "--verbose", dest="log_level",
action="append_const", const=-10,
help=_("Increase log verbosity"))
@@ -83,6 +87,7 @@ def parse(self, argv):
options.start_visibility = False
self.start_visibility = options.start_visibility
self.arg_profile = options.profile
+ self.start_shell = options.start_shell
for cmd in self.cmds:
if cmd in mpd_cmds:
View
27 sonata/launcher.py
@@ -30,6 +30,7 @@
import os
import platform
import sys
+import threading # needed for interactive shell
def run():
@@ -161,13 +162,35 @@ class FakeModule(object):
args.execute_cmds()
-
## Load the main application:
from sonata import main
app = main.Base(args)
+
+ ## Load the shell
+ # yo dawg, I heard you like python,
+ # so I put a python shell in your python application
+ # so you can debug while you run it.
+ if args.start_shell:
+ # the enviroment used for the shell
+ scope = dict(list(globals().items()) + list(locals().items()))
+ def run_shell():
+ try:
+ import IPython
+ IPython.embed(user_ns=scope)
+ except ImportError as e: # fallback if ipython is not avaible
+ import code
+ shell = code.InteractiveConsole(scope)
+ shell.interact()
+ # quit program if shell is closed,
+ # This is the only way to close the program clone in this mode,
+ # because we can't close the shell thread easily
+ from gi.repository import Gtk
+ Gtk.main_quit()
+ threading.Thread(target=run_shell).start()
+
try:
app.main()
except KeyboardInterrupt:
- pass
+ Gtk.main_quit()

0 comments on commit 36a63b3

Please sign in to comment.