summaryrefslogtreecommitdiff
path: root/repoze
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2009-10-19 18:43:01 +0000
committerChris McDonough <chrism@agendaless.com>2009-10-19 18:43:01 +0000
commitb3e9ee1f5863cbe0d092f29e25cb4363d7398b64 (patch)
treeb6798fb91ac73a366b6435ab0c8ca70e8a25772f /repoze
parentd5b2f46f323571ec32fa7e9c77ccf83f62a03fea (diff)
downloadpyramid-b3e9ee1f5863cbe0d092f29e25cb4363d7398b64.tar.gz
pyramid-b3e9ee1f5863cbe0d092f29e25cb4363d7398b64.tar.bz2
pyramid-b3e9ee1f5863cbe0d092f29e25cb4363d7398b64.zip
- ``paster bfgshell`` now supports IPython if it's available for
import. Thanks to Daniel Holth for the initial patch.
Diffstat (limited to 'repoze')
-rw-r--r--repoze/bfg/paster.py31
-rw-r--r--repoze/bfg/tests/test_paster.py47
2 files changed, 72 insertions, 6 deletions
diff --git a/repoze/bfg/paster.py b/repoze/bfg/paster.py
index c60d0b437..de5dc1f79 100644
--- a/repoze/bfg/paster.py
+++ b/repoze/bfg/paster.py
@@ -9,6 +9,12 @@ from paste.util.template import paste_script_template_renderer
from repoze.bfg.scripting import get_root
+try:
+ from IPython.Shell import IPShellEmbed
+except ImportError:
+ IPShellEmbed = None
+
+
class StarterProjectTemplate(Template):
_template_dir = 'paster_templates/starter'
summary = 'repoze.bfg starter project'
@@ -66,8 +72,14 @@ class BFGShellCommand(Command):
group_name = 'bfg'
parser = Command.standard_parser(simulate=True)
+ parser.add_option('-d', '--disable-ipython',
+ action='store_true',
+ dest='disable_ipython',
+ help="Don't use IPython even if it is available")
+
interact = (interact,) # for testing
loadapp = (loadapp,) # for testing
+ IPShellEmbed = IPShellEmbed # for testing
verbose = 3
def command(self):
@@ -77,8 +89,17 @@ class BFGShellCommand(Command):
config_file, section_name = self.args
app = get_app(config_file, section_name, loadapp=self.loadapp[0])
root, closer = get_root(app)
- try:
- self.interact[0](banner, local={'root':root})
- finally:
- closer()
-
+ if self.IPShellEmbed is not None and not self.options.disable_ipython:
+ shell = self.IPShellEmbed(argv=self.args)
+ shell.set_banner(shell.IP.BANNER + '\n\n' + banner)
+ try:
+ shell(local_ns={'root':root}, global_ns={})
+ finally:
+ closer()
+ else:
+ try:
+ self.interact[0](banner, local={'root':root})
+ finally:
+ closer()
+
+
diff --git a/repoze/bfg/tests/test_paster.py b/repoze/bfg/tests/test_paster.py
index 0119e3313..4650f04cd 100644
--- a/repoze/bfg/tests/test_paster.py
+++ b/repoze/bfg/tests/test_paster.py
@@ -8,14 +8,18 @@ class TestBFGShellCommand(unittest.TestCase):
def _makeOne(self):
return self._getTargetClass()('bfgshell')
- def test_command(self):
+ def test_command_ipython_disabled(self):
command = self._makeOne()
interact = DummyInteractor()
app = DummyApp()
loadapp = DummyLoadApp(app)
command.interact = (interact,)
command.loadapp = (loadapp,)
+ command.IPShellEmbed = True # fake out
command.args = ('/foo/bar/myapp.ini', 'myapp')
+ class Options(object): pass
+ command.options = Options()
+ command.options.disable_ipython =True
command.command()
self.assertEqual(loadapp.config_name, 'config:/foo/bar/myapp.ini')
self.assertEqual(loadapp.section_name, 'myapp')
@@ -28,6 +32,31 @@ class TestBFGShellCommand(unittest.TestCase):
self.failUnless(interact.banner)
self.assertEqual(len(app.threadlocal_manager.popped), 1)
+ def test_command_ipython_enabled(self):
+ command = self._makeOne()
+ interact = DummyInteractor()
+ app = DummyApp()
+ loadapp = DummyLoadApp(app)
+ command.loadapp = (loadapp,)
+ dummy_shell_factory = DummyIPShellFactory()
+ command.IPShellEmbed = dummy_shell_factory
+ command.args = ('/foo/bar/myapp.ini', 'myapp')
+ class Options(object): pass
+ command.options = Options()
+ command.options.disable_ipython = False
+ command.command()
+ self.assertEqual(loadapp.config_name, 'config:/foo/bar/myapp.ini')
+ self.assertEqual(loadapp.section_name, 'myapp')
+ self.failUnless(loadapp.relative_to)
+ self.assertEqual(len(app.threadlocal_manager.pushed), 1)
+ pushed = app.threadlocal_manager.pushed[0]
+ self.assertEqual(pushed['registry'], dummy_registry)
+ self.assertEqual(pushed['request'], None)
+ self.assertEqual(dummy_shell_factory.shell.local_ns,{'root':dummy_root})
+ self.assertEqual(dummy_shell_factory.shell.global_ns, {})
+ self.failUnless(dummy_shell_factory.shell.banner)
+ self.assertEqual(len(app.threadlocal_manager.popped), 1)
+
class TestGetApp(unittest.TestCase):
def _callFUT(self, config_file, section_name, loadapp):
from repoze.bfg.paster import get_app
@@ -46,6 +75,22 @@ class TestGetApp(unittest.TestCase):
class Dummy:
pass
+class DummyIPShellFactory(object):
+ def __call__(self, argv):
+ shell = DummyIPShell()
+ self.shell = shell
+ return shell
+
+class DummyIPShell(object):
+ IP = Dummy()
+ IP.BANNER = 'foo'
+ def set_banner(self, banner):
+ self.banner = banner
+
+ def __call__(self, local_ns, global_ns):
+ self.local_ns = local_ns
+ self.global_ns = global_ns
+
dummy_root = Dummy()
dummy_registry = Dummy()