summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2011-08-16 03:53:37 -0500
committerMichael Merickel <michael@merickel.org>2011-08-16 03:53:37 -0500
commita83d944e5ca7fc565ea947b5c89f3f0710cd2ec8 (patch)
treed89ee52c7370cf3f88d3bfe41d6c2d70cefdc416
parent6007dd9a6a22e1112afa6a72ca2ee406698d775f (diff)
downloadpyramid-a83d944e5ca7fc565ea947b5c89f3f0710cd2ec8.tar.gz
pyramid-a83d944e5ca7fc565ea947b5c89f3f0710cd2ec8.tar.bz2
pyramid-a83d944e5ca7fc565ea947b5c89f3f0710cd2ec8.zip
Added an initialization callable to pshell.
Modified the use_script idea into a callable called 'setup', which expects the API: def setup(env): env['a'] = 1
-rw-r--r--pyramid/paster.py41
-rw-r--r--pyramid/tests/pshellapp/__init__.py0
-rw-r--r--pyramid/tests/pshellapp/no_all.py5
-rw-r--r--pyramid/tests/pshellapp/with_all.py7
-rw-r--r--pyramid/tests/test_paster.py48
5 files changed, 38 insertions, 63 deletions
diff --git a/pyramid/paster.py b/pyramid/paster.py
index 7beeada7a..ec55dc126 100644
--- a/pyramid/paster.py
+++ b/pyramid/paster.py
@@ -129,17 +129,18 @@ 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."))
+ parser.add_option('--setup',
+ dest='setup',
+ help=("A callable that will be passed the environment "
+ "before it is made available to the shell. This "
+ "option will override the 'setup' key in the "
+ "[pshell] ini section."))
ConfigParser = ConfigParser.ConfigParser # testing
loaded_objects = {}
object_help = {}
- use_script = None
+ setup = None
def pshell_file_config(self, filename):
config = self.ConfigParser()
@@ -152,10 +153,10 @@ class PShellCommand(PCommand):
resolver = DottedNameResolver(None)
self.loaded_objects = {}
self.object_help = {}
- self.use_script = None
+ self.setup = None
for k, v in items:
- if k == 'import':
- self.use_script = v
+ if k == 'setup':
+ self.setup = v
else:
self.loaded_objects[k] = resolver.maybe_resolve(v)
self.object_help[k] = v
@@ -182,21 +183,22 @@ class PShellCommand(PCommand):
'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.options.setup:
+ self.setup = self.options.setup
- if self.use_script:
+ if self.setup:
# store the env before muddling it with the script
orig_env = env.copy()
- # do this instead of an eval() to respect __all__
- exec 'from %s import *' % self.use_script in env
- env.pop('__builtins__', None)
+ # call the setup callable
+ resolver = DottedNameResolver(None)
+ setup = resolver.maybe_resolve(self.setup)
+ setup(env)
# remove any objects from default help that were overidden
- for k, v in orig_env.iteritems():
- if env[k] != orig_env[k]:
- del env_help[k]
+ for k, v in env.iteritems():
+ if k not in orig_env or env[k] != orig_env[k]:
+ env_help[k] = v
# load the pshell section of the ini file
env.update(self.loaded_objects)
@@ -218,9 +220,6 @@ class PShellCommand(PCommand):
for var in sorted(self.object_help.keys()):
help += '\n %-12s %s' % (var, self.object_help[var])
- if self.use_script:
- help += '\n\nAll objects from %s are available.' % self.use_script
-
if shell is None and not self.options.disable_ipython:
shell = self.make_ipython_v0_11_shell()
if shell is None:
diff --git a/pyramid/tests/pshellapp/__init__.py b/pyramid/tests/pshellapp/__init__.py
deleted file mode 100644
index e69de29bb..000000000
--- a/pyramid/tests/pshellapp/__init__.py
+++ /dev/null
diff --git a/pyramid/tests/pshellapp/no_all.py b/pyramid/tests/pshellapp/no_all.py
deleted file mode 100644
index 177a34590..000000000
--- a/pyramid/tests/pshellapp/no_all.py
+++ /dev/null
@@ -1,5 +0,0 @@
-a = 1
-b = 2
-
-root = 'root override'
-m = 'model override'
diff --git a/pyramid/tests/pshellapp/with_all.py b/pyramid/tests/pshellapp/with_all.py
deleted file mode 100644
index dd0a3e2f9..000000000
--- a/pyramid/tests/pshellapp/with_all.py
+++ /dev/null
@@ -1,7 +0,0 @@
-__all__ = ['a', 'root', 'm']
-
-a = 1
-b = 2
-
-root = 'root override'
-m = 'model override'
diff --git a/pyramid/tests/test_paster.py b/pyramid/tests/test_paster.py
index 49a54aecc..995e05d46 100644
--- a/pyramid/tests/test_paster.py
+++ b/pyramid/tests/test_paster.py
@@ -21,7 +21,7 @@ class TestPShellCommand(unittest.TestCase):
class Options(object): pass
self.options = Options()
self.options.disable_ipython = True
- self.options.use_script = None
+ self.options.setup = None
cmd.options = self.options
return cmd
@@ -157,10 +157,12 @@ class TestPShellCommand(unittest.TestCase):
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
- def test_command_loads_use_script_with_all(self):
+ def test_command_setup(self):
command = self._makeOne()
- self.config_factory.items = [
- ('import', 'pyramid.tests.pshellapp.with_all')]
+ def setup(env):
+ env['a'] = 1
+ env['root'] = 'root override'
+ self.config_factory.items = [('setup', setup)]
shell = DummyShell()
command.command(shell)
self.assertTrue(self.config_factory.parser)
@@ -172,27 +174,7 @@ class TestPShellCommand(unittest.TestCase):
'registry':self.bootstrap.registry,
'request':self.bootstrap.request,
'root_factory':self.bootstrap.root_factory,
- 'a': 1, 'm': 'model override',
- })
- self.assertTrue(self.bootstrap.closer.called)
- self.assertTrue(shell.help)
-
- def test_command_loads_use_script_without_all(self):
- command = self._makeOne()
- self.config_factory.items = [
- ('import', 'pyramid.tests.pshellapp.no_all')]
- 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, 'b': 2, 'm': 'model override',
+ 'a':1,
})
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
@@ -200,8 +182,11 @@ class TestPShellCommand(unittest.TestCase):
def test_command_loads_check_variable_override_order(self):
command = self._makeOne()
model = Dummy()
- self.config_factory.items = [
- ('import', 'pyramid.tests.pshellapp.with_all'), ('m', model)]
+ def setup(env):
+ env['a'] = 1
+ env['m'] = 'model override'
+ env['root'] = 'root override'
+ self.config_factory.items = [('setup', setup), ('m', model)]
shell = DummyShell()
command.command(shell)
self.assertTrue(self.config_factory.parser)
@@ -218,12 +203,15 @@ class TestPShellCommand(unittest.TestCase):
self.assertTrue(self.bootstrap.closer.called)
self.assertTrue(shell.help)
- def test_command_loads_use_script_override(self):
+ def test_command_loads_setup_from_options(self):
command = self._makeOne()
+ def setup(env):
+ env['a'] = 1
+ env['root'] = 'root override'
model = Dummy()
- self.config_factory.items = [('import', 'abc'),
+ self.config_factory.items = [('setup', 'abc'),
('m', model)]
- command.options.use_script = 'pyramid.tests.pshellapp.with_all'
+ command.options.setup = setup
shell = DummyShell()
command.command(shell)
self.assertTrue(self.config_factory.parser)