ipython support in ./bin/gmg shell!
authorChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 14 Mar 2012 21:00:34 +0000 (16:00 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Wed, 14 Mar 2012 21:00:34 +0000 (16:00 -0500)
mediagoblin/gmg_commands/shell.py

index fe15e9f76b70480777970334cd57a12ca97104e2..3169abe2c26cb00c6cc19d6574fb8321d5c1f70c 100644 (file)
@@ -22,7 +22,9 @@ from mediagoblin.gmg_commands import util as commands_util
 
 
 def shell_parser_setup(subparser):
-    pass
+    subparser.add_argument(
+        '--ipython', help='Use ipython',
+        action="store_true")
 
 
 SHELL_BANNER = """\
@@ -34,16 +36,43 @@ Available vars:
  - db: database instance
 """
 
+def py_shell(**user_namespace):
+    """
+    Run a shell using normal python shell.
+    """
+    code.interact(
+        banner=SHELL_BANNER,
+        local=user_namespace)
+
+
+def ipython_shell(**user_namespace):
+    """
+    Run a shell for the user using ipython.
+    """
+    try:
+        from IPython import embed
+    except:
+        print "IPython not available... exiting!"
+        return
+    
+    embed(
+        banner1=SHELL_BANNER,
+        user_ns=user_namespace)
+
 
 def shell(args):
     """
     Setup a shell for the user
+    either a normal Python shell
+    or an IPython one
     """
-    mgoblin_app = commands_util.setup_app(args)
 
-    code.interact(
-        banner=SHELL_BANNER,
-        local={
-            'mgoblin_app': mgoblin_app,
-            'mg_globals': mg_globals,
-            'db': mg_globals.database})
+    user_namespace = {
+        'mg_globals': mg_globals,
+        'mgoblin_app': commands_util.setup_app(args),
+        'db': mg_globals.database}
+
+    if args.ipython:
+        ipython_shell(**user_namespace)
+    else:
+        py_shell(**user_namespace)