Michael Merickel
2011-08-16 a83d944e5ca7fc565ea947b5c89f3f0710cd2ec8
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
3 files deleted
2 files modified
101 ■■■■■ changed files
pyramid/paster.py 41 ●●●● patch | view | raw | blame | history
pyramid/tests/pshellapp/__init__.py patch | view | raw | blame | history
pyramid/tests/pshellapp/no_all.py 5 ●●●●● patch | view | raw | blame | history
pyramid/tests/pshellapp/with_all.py 7 ●●●●● patch | view | raw | blame | history
pyramid/tests/test_paster.py 48 ●●●●● patch | view | raw | blame | history
pyramid/paster.py
@@ -129,17 +129,18 @@
                      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 @@
        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 @@
            '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)
@@ -217,9 +219,6 @@
            help += '\n\nCustom Variables:'
            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()
pyramid/tests/pshellapp/__init__.py
pyramid/tests/pshellapp/no_all.py
File was deleted
pyramid/tests/pshellapp/with_all.py
File was deleted
pyramid/tests/test_paster.py
@@ -21,7 +21,7 @@
            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 @@
        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 @@
            '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 @@
    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 @@
        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)