From a5d9ff4e135feb8a8132cfd93dcb51fa2eacd35d Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Tue, 16 Aug 2011 02:07:16 -0500 Subject: Added support for --import option to pshell. --- pyramid/paster.py | 9 +++++++++ pyramid/tests/test_paster.py | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/pyramid/paster.py b/pyramid/paster.py index 87abdddd4..99ead1af3 100644 --- a/pyramid/paster.py +++ b/pyramid/paster.py @@ -129,6 +129,11 @@ class PShellCommand(PCommand): action='store_true', dest='disable_ipython', help="Don't use IPython even if it is available") + parser.add_option('--import-script', + dest='use_script', + help=("Execute the script and import all variables from " + "a dotted Python path. This option will override " + "the 'import' key in the [pshell] ini section.")) ConfigParser = ConfigParser.ConfigParser # testing @@ -176,6 +181,10 @@ class PShellCommand(PCommand): env_help['root_factory'] = ( 'Default root factory used to create `root`.') + # override use_script with command-line options + if self.options.use_script: + self.use_script = self.options.use_script + if self.use_script: # store the env before muddling it with the script orig_env = env.copy() diff --git a/pyramid/tests/test_paster.py b/pyramid/tests/test_paster.py index 45e36f129..cb30dcc12 100644 --- a/pyramid/tests/test_paster.py +++ b/pyramid/tests/test_paster.py @@ -21,6 +21,7 @@ class TestPShellCommand(unittest.TestCase): class Options(object): pass self.options = Options() self.options.disable_ipython = True + self.options.use_script = None cmd.options = self.options return cmd @@ -196,6 +197,28 @@ class TestPShellCommand(unittest.TestCase): self.assertTrue(self.bootstrap.closer.called) self.assertTrue(shell.help) + def test_command_loads_use_script_override(self): + command = self._makeOne() + model = Dummy() + self.config_factory.items = [('import', 'abc'), + ('m', model)] + command.options.use_script = 'pyramid.tests.pshellapp' + 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(shell.env, { + 'app':self.bootstrap.app, 'root':'root override', + 'registry':self.bootstrap.registry, + 'request':self.bootstrap.request, + 'root_factory':self.bootstrap.root_factory, + 'a':1, 'm':model, + }) + self.assertTrue(self.bootstrap.closer.called) + self.assertTrue(shell.help) + def test_command_custom_section_override(self): command = self._makeOne() dummy = Dummy() -- cgit v1.2.3