From 282a62fc3e943a416e22575d8dc9381f2077f273 Mon Sep 17 00:00:00 2001 From: Julian Taylor Date: Sun, 24 Jul 2011 18:54:22 +0200 Subject: add compatibility with ipython 0.11 try to use 0.11 and 0.10 api to open embedded ipython shell --- pyramid/paster.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pyramid/paster.py b/pyramid/paster.py index 3aa6a5f1d..8eb8f2413 100644 --- a/pyramid/paster.py +++ b/pyramid/paster.py @@ -150,11 +150,6 @@ class PShellCommand(PCommand): def command(self, IPShell=_marker): # IPShell passed to command method is for testing purposes - if IPShell is _marker: # pragma: no cover - try: - from IPython.Shell import IPShell - except ImportError: - IPShell = None config_uri = self.args[0] config_file = config_uri.split('#', 1)[0] self.logging_file_config(config_file) @@ -197,6 +192,18 @@ class PShellCommand(PCommand): help += '\n' + if IPShell is _marker: + try: #pragma no cover + try: #pragma no cover + from IPython.frontend.terminal.embed import InteractiveShellEmbed + IPShell = InteractiveShellEmbed(banner2=help, user_ns=env) + except ImportError: #pragma no cover + from IPython.Shell import IPShellEmbed + IPShell = IPShellEmbed(argv=[], user_ns=env) + IPShell.IP.BANNER = IPShell.IP.BANNER + '\n\n' + help + except ImportError: #pragma no cover + IPShell = None + if (IPShell is None) or self.options.disable_ipython: cprt = 'Type "help" for more information.' banner = "Python %s on %s\n%s" % (sys.version, sys.platform, cprt) @@ -207,9 +214,7 @@ class PShellCommand(PCommand): closer() else: try: - shell = IPShell(argv=[], user_ns=env) - shell.IP.BANNER = shell.IP.BANNER + help - shell.mainloop() + IPShell() finally: closer() -- cgit v1.2.3 From dd91eb061edc328ae8a96df0b719624804d5ef31 Mon Sep 17 00:00:00 2001 From: Julian Taylor Date: Mon, 25 Jul 2011 10:25:35 +0200 Subject: fix banner display for ipython < 0.11 --- pyramid/paster.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyramid/paster.py b/pyramid/paster.py index 8eb8f2413..277f093d7 100644 --- a/pyramid/paster.py +++ b/pyramid/paster.py @@ -200,7 +200,7 @@ class PShellCommand(PCommand): except ImportError: #pragma no cover from IPython.Shell import IPShellEmbed IPShell = IPShellEmbed(argv=[], user_ns=env) - IPShell.IP.BANNER = IPShell.IP.BANNER + '\n\n' + help + IPShell.set_banner(IPShell.IP.BANNER + '\n' + help) except ImportError: #pragma no cover IPShell = None -- cgit v1.2.3 From be03f74bea45854fec8aabca9407ca983c65111f Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 31 Jul 2011 09:35:53 -0600 Subject: Fixed a spelling mistake in ISession. --- pyramid/interfaces.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyramid/interfaces.py b/pyramid/interfaces.py index a06cb7e52..5ef11b1cf 100644 --- a/pyramid/interfaces.py +++ b/pyramid/interfaces.py @@ -777,13 +777,13 @@ class ISession(Interface): """ Pop a queue from the flash storage. The queue is removed from flash storage after this message is called. The queue is returned; it is a list of flash messages added by - :meth:`pyramid.interfaces.ISesssion.flash`""" + :meth:`pyramid.interfaces.ISession.flash`""" def peek_flash(queue=''): """ Peek at a queue in the flash storage. The queue remains in flash storage after this message is called. The queue is returned; it is a list of flash messages added by - :meth:`pyramid.interfaces.ISesssion.flash` + :meth:`pyramid.interfaces.ISession.flash` """ def new_csrf_token(): -- cgit v1.2.3 From c337a8477658e9a41a07fa3d0a17c23bde93f26c Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 31 Jul 2011 09:46:02 -0600 Subject: Fixed spelling in the IRoute interface. --- pyramid/interfaces.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyramid/interfaces.py b/pyramid/interfaces.py index 5ef11b1cf..41e896adf 100644 --- a/pyramid/interfaces.py +++ b/pyramid/interfaces.py @@ -631,7 +631,7 @@ class IRoute(Interface): 'when this route matches (or ``None``)') predicates = Attribute( 'A sequence of :term:`route predicate` objects used to ' - 'determine if a request matches this route or not or not after ' + 'determine if a request matches this route or not after ' 'basic pattern matching has been completed.') pregenerator = Attribute('This attribute should either be ``None`` or ' 'a callable object implementing the ' -- cgit v1.2.3 From 01f9289b99be03eb203afcd64b010f3455f5db33 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 31 Jul 2011 23:54:56 -0600 Subject: Removed unused PCommand testing hooks. --- pyramid/paster.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pyramid/paster.py b/pyramid/paster.py index 277f093d7..ca343def7 100644 --- a/pyramid/paster.py +++ b/pyramid/paster.py @@ -87,11 +87,6 @@ def bootstrap(config_uri, request=None): _marker = object() class PCommand(Command): - get_app = staticmethod(get_app) # hook point - get_root = staticmethod(get_root) # hook point - group_name = 'pyramid' - interact = (interact,) # for testing - loadapp = (loadapp,) # for testing bootstrap = (bootstrap,) # testing verbose = 3 -- cgit v1.2.3 From ba06e145659f95353621fb1b4cfed9593068664c Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Mon, 1 Aug 2011 00:43:10 -0600 Subject: Refactored the PShellCommand to improve testability. --- pyramid/paster.py | 70 ++++++++++++-------- pyramid/tests/test_paster.py | 154 ++++++++++++++++++++++++++++++------------- 2 files changed, 152 insertions(+), 72 deletions(-) diff --git a/pyramid/paster.py b/pyramid/paster.py index ca343def7..3143fa91e 100644 --- a/pyramid/paster.py +++ b/pyramid/paster.py @@ -84,8 +84,6 @@ def bootstrap(config_uri, request=None): env['app'] = app return env -_marker = object() - class PCommand(Command): bootstrap = (bootstrap,) # testing verbose = 3 @@ -143,8 +141,7 @@ class PShellCommand(PCommand): self.loaded_objects[k] = resolver.maybe_resolve(v) self.object_help[k] = v - def command(self, IPShell=_marker): - # IPShell passed to command method is for testing purposes + def command(self, shell=None): config_uri = self.args[0] config_file = config_uri.split('#', 1)[0] self.logging_file_config(config_file) @@ -185,33 +182,52 @@ class PShellCommand(PCommand): for var in sorted(self.object_help.keys()): help += '\n %-12s %s' % (var, self.object_help[var]) - help += '\n' - - if IPShell is _marker: - try: #pragma no cover - try: #pragma no cover - from IPython.frontend.terminal.embed import InteractiveShellEmbed - IPShell = InteractiveShellEmbed(banner2=help, user_ns=env) - except ImportError: #pragma no cover - from IPython.Shell import IPShellEmbed - IPShell = IPShellEmbed(argv=[], user_ns=env) - IPShell.set_banner(IPShell.IP.BANNER + '\n' + help) - except ImportError: #pragma no cover - IPShell = None - - if (IPShell is None) or self.options.disable_ipython: + if shell is None and not self.options.disable_ipython: + shell = self.make_ipython_v0_11_shell() + if shell is None: + shell = self.make_ipython_v0_10_shell() + + if shell is None: + shell = self.make_default_shell() + + try: + shell(env, help) + finally: + closer() + + def make_default_shell(self, interact=interact): + def shell(env, help): cprt = 'Type "help" for more information.' banner = "Python %s on %s\n%s" % (sys.version, sys.platform, cprt) - banner += '\n' + help + banner += '\n' + help + '\n' + interact(banner, local=env) + return shell + + def make_ipython_v0_11_shell(self, IPShellFactory=None): + if IPShellFactory is None: # pragma: no cover try: - self.interact[0](banner, local=env) - finally: - closer() - else: + from IPython.frontend.terminal.embed import ( + InteractiveShellEmbed) + IPShellFactory = InteractiveShellEmbed + except ImportError: + return None + def shell(env, help): + IPShell = IPShellFactory(banner2=help, user_ns=env) + IPShell() + return shell + + def make_ipython_v0_10_shell(self, IPShellFactory=None): + if IPShellFactory is None: # pragma: no cover try: - IPShell() - finally: - closer() + from IPython.Shell import IPShellEmbed + IPShellFactory = IPShellEmbed + except ImportError: + return None + def shell(env, help): + IPShell = IPShellFactory(argv=[], user_ns=env) + IPShell.set_banner(IPShell.IP.BANNER + '\n' + help + '\n') + IPShell() + return shell BFGShellCommand = PShellCommand # b/w compat forever diff --git a/pyramid/tests/test_paster.py b/pyramid/tests/test_paster.py index 085cda810..3cf249c5c 100644 --- a/pyramid/tests/test_paster.py +++ b/pyramid/tests/test_paster.py @@ -5,12 +5,9 @@ class TestPShellCommand(unittest.TestCase): from pyramid.paster import PShellCommand return PShellCommand - def _makeOne(self, patch_interact=True, patch_bootstrap=True, - patch_config=True, patch_args=True, patch_options=True): + def _makeOne(self, patch_bootstrap=True, patch_config=True, + patch_args=True, patch_options=True): cmd = self._getTargetClass()('pshell') - if patch_interact: - self.interact = DummyInteractor() - cmd.interact = (self.interact,) if patch_bootstrap: self.bootstrap = DummyBootstrap() cmd.bootstrap = (self.bootstrap,) @@ -27,69 +24,129 @@ class TestPShellCommand(unittest.TestCase): cmd.options = self.options return cmd - def test_command_ipshell_is_None_ipython_enabled(self): + def test_make_default_shell(self): command = self._makeOne() - command.options.disable_ipython = True - command.command(IPShell=None) + interact = DummyInteractor() + shell = command.make_default_shell(interact) + shell({'foo': 'bar'}, 'a help message') + self.assertEqual(interact.local, {'foo': 'bar'}) + self.assertTrue('a help message' in interact.banner) + + def test_make_ipython_v0_11_shell(self): + command = self._makeOne() + ipshell_factory = DummyIPShellFactory() + shell = command.make_ipython_v0_11_shell(ipshell_factory) + shell({'foo': 'bar'}, 'a help message') + self.assertEqual(ipshell_factory.kw['user_ns'], {'foo': 'bar'}) + self.assertTrue('a help message' in ipshell_factory.kw['banner2']) + self.assertTrue(ipshell_factory.shell.called) + + def test_make_ipython_v0_10_shell(self): + command = self._makeOne() + ipshell_factory = DummyIPShellFactory() + shell = command.make_ipython_v0_10_shell(ipshell_factory) + shell({'foo': 'bar'}, 'a help message') + self.assertEqual(ipshell_factory.kw['argv'], []) + self.assertEqual(ipshell_factory.kw['user_ns'], {'foo': 'bar'}) + self.assertTrue('a help message' in ipshell_factory.shell.banner) + self.assertTrue(ipshell_factory.shell.called) + + def test_command_loads_default_shell(self): + command = self._makeOne() + shell = DummyShell() + command.make_ipython_v0_11_shell = lambda: None + command.make_ipython_v0_10_shell = lambda: None + command.make_default_shell = lambda: shell + command.command() self.assertTrue(self.config_factory.parser) self.assertEqual(self.config_factory.parser.filename, '/foo/bar/myapp.ini') self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') - self.assertEqual(self.interact.local, { + self.assertEqual(shell.env, { 'app':self.bootstrap.app, 'root':self.bootstrap.root, 'registry':self.bootstrap.registry, 'request':self.bootstrap.request, 'root_factory':self.bootstrap.root_factory, }) self.assertTrue(self.bootstrap.closer.called) - self.assertTrue(self.interact.banner) + self.assertTrue(shell.help) - def test_command_ipshell_is_not_None_ipython_disabled(self): + def test_command_loads_default_shell_with_ipython_disabled(self): command = self._makeOne() + shell = DummyShell() + bad_shell = DummyShell() + command.make_ipython_v0_11_shell = lambda: bad_shell + command.make_ipython_v0_10_shell = lambda: bad_shell + command.make_default_shell = lambda: shell command.options.disable_ipython = True - command.command(IPShell='notnone') + command.command() self.assertTrue(self.config_factory.parser) self.assertEqual(self.config_factory.parser.filename, '/foo/bar/myapp.ini') self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') - self.assertEqual(self.interact.local, { + self.assertEqual(shell.env, { 'app':self.bootstrap.app, 'root':self.bootstrap.root, 'registry':self.bootstrap.registry, 'request':self.bootstrap.request, 'root_factory':self.bootstrap.root_factory, }) + self.assertEqual(bad_shell.env, {}) self.assertTrue(self.bootstrap.closer.called) - self.assertTrue(self.interact.banner) + self.assertTrue(shell.help) - def test_command_ipython_enabled(self): - command = self._makeOne(patch_interact=False) + def test_command_loads_ipython_v0_11(self): + command = self._makeOne() + shell = DummyShell() + command.make_ipython_v0_11_shell = lambda: shell + command.make_ipython_v0_10_shell = lambda: None + command.make_default_shell = lambda: None command.options.disable_ipython = False - dummy_shell_factory = DummyIPShellFactory() - command.command(IPShell=dummy_shell_factory) + command.command() self.assertTrue(self.config_factory.parser) self.assertEqual(self.config_factory.parser.filename, '/foo/bar/myapp.ini') self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') - self.assertEqual(dummy_shell_factory.shell.local_ns, { + self.assertEqual(shell.env, { 'app':self.bootstrap.app, 'root':self.bootstrap.root, 'registry':self.bootstrap.registry, 'request':self.bootstrap.request, 'root_factory':self.bootstrap.root_factory, }) - self.assertEqual(dummy_shell_factory.shell.global_ns, {}) self.assertTrue(self.bootstrap.closer.called) + self.assertTrue(shell.help) + + def test_command_loads_ipython_v0_10(self): + command = self._makeOne() + shell = DummyShell() + command.make_ipython_v0_11_shell = lambda: None + command.make_ipython_v0_10_shell = lambda: shell + command.make_default_shell = lambda: None + command.options.disable_ipython = False + command.command() + self.assertTrue(self.config_factory.parser) + self.assertEqual(self.config_factory.parser.filename, + '/foo/bar/myapp.ini') + self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') + self.assertEqual(shell.env, { + 'app':self.bootstrap.app, 'root':self.bootstrap.root, + 'registry':self.bootstrap.registry, + 'request':self.bootstrap.request, + 'root_factory':self.bootstrap.root_factory, + }) + self.assertTrue(self.bootstrap.closer.called) + self.assertTrue(shell.help) def test_command_loads_custom_items(self): command = self._makeOne() model = Dummy() self.config_factory.items = [('m', model)] - command.options.disable_ipython = True - command.command(IPShell=None) + shell = DummyShell() + command.command(shell) self.assertTrue(self.config_factory.parser) self.assertEqual(self.config_factory.parser.filename, '/foo/bar/myapp.ini') self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') - self.assertEqual(self.interact.local, { + self.assertEqual(shell.env, { 'app':self.bootstrap.app, 'root':self.bootstrap.root, 'registry':self.bootstrap.registry, 'request':self.bootstrap.request, @@ -97,25 +154,25 @@ class TestPShellCommand(unittest.TestCase): 'm':model, }) self.assertTrue(self.bootstrap.closer.called) - self.assertTrue(self.interact.banner) + self.assertTrue(shell.help) def test_command_custom_section_override(self): command = self._makeOne() dummy = Dummy() self.config_factory.items = [('app', dummy), ('root', dummy), ('registry', dummy), ('request', dummy)] - command.options.disable_ipython = True - command.command(IPShell=None) + shell = DummyShell() + command.command(shell) self.assertTrue(self.config_factory.parser) self.assertEqual(self.config_factory.parser.filename, '/foo/bar/myapp.ini') self.assertEqual(self.bootstrap.a[0], '/foo/bar/myapp.ini#myapp') - self.assertEqual(self.interact.local, { + self.assertEqual(shell.env, { 'app':dummy, 'root':dummy, 'registry':dummy, 'request':dummy, 'root_factory':self.bootstrap.root_factory, }) self.assertTrue(self.bootstrap.closer.called) - self.assertTrue(self.interact.banner) + self.assertTrue(shell.help) class TestPRoutesCommand(unittest.TestCase): def _getTargetClass(self): @@ -770,23 +827,6 @@ class TestBootstrap(unittest.TestCase): class Dummy: pass -class DummyIPShellFactory(object): - def __call__(self, argv, user_ns=None): - shell = DummyIPShell() - shell(user_ns, {}) - self.shell = shell - return shell - -class DummyIPShell(object): - IP = Dummy() - IP.BANNER = 'foo' - def __call__(self, local_ns, global_ns): - self.local_ns = local_ns - self.global_ns = global_ns - - def mainloop(self): - pass - dummy_root = Dummy() class DummyRegistry(object): @@ -796,11 +836,35 @@ class DummyRegistry(object): dummy_registry = DummyRegistry() +class DummyShell(object): + env = {} + help = '' + + def __call__(self, env, help): + self.env = env + self.help = help + class DummyInteractor: def __call__(self, banner, local): self.banner = banner self.local = local +class DummyIPShell(object): + IP = Dummy() + IP.BANNER = 'foo' + + def set_banner(self, banner): + self.banner = banner + + def __call__(self): + self.called = True + +class DummyIPShellFactory(object): + def __call__(self, **kw): + self.kw = kw + self.shell = DummyIPShell() + return self.shell + class DummyLoadApp: def __init__(self, app): self.app = app -- cgit v1.2.3