Skip to content

Commit

Permalink
update pshell
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Mar 29, 2017
1 parent d750aa1 commit 703c731
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 60 deletions.
31 changes: 13 additions & 18 deletions pyramid/scripts/pshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pyramid.settings import aslist

from pyramid.scripts.common import get_loader
from pyramid.scripts.common import parse_vars
from pyramid.scripts.common import setup_logging

Expand Down Expand Up @@ -41,7 +42,9 @@ class PShellCommand(object):
than one Pyramid application within it, the loader will use the
last one.
"""
bootstrap = (bootstrap,) # for testing
bootstrap = staticmethod(bootstrap) # for testing
get_loader = staticmethod(get_loader) # for testing
setup_logging = staticmethod(setup_logging) # for testing
pkg_resources = pkg_resources # for testing

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -78,7 +81,6 @@ class PShellCommand(object):
"passed here.",
)

ConfigParser = configparser.ConfigParser # testing
default_runner = python_shell_runner # testing

loaded_objects = {}
Expand All @@ -91,20 +93,16 @@ def __init__(self, argv, quiet=False):
self.quiet = quiet
self.args = self.parser.parse_args(argv[1:])

def pshell_file_config(self, filename):
config = self.ConfigParser()
config.optionxform = str
config.read(filename)
try:
items = config.items('pshell')
except configparser.NoSectionError:
def pshell_file_config(self, filename, defaults):
loader = self.get_loader(filename)
if 'pshell' not in loader.get_sections():
return

settings = loader.get_settings('pshell', defaults)
resolver = DottedNameResolver(None)
self.loaded_objects = {}
self.object_help = {}
self.setup = None
for k, v in items:
for k, v in settings.items():
if k == 'setup':
self.setup = v
elif k == 'default_shell':
Expand All @@ -124,13 +122,10 @@ def run(self, shell=None):
self.out('Requires a config file argument')
return 2
config_uri = self.args.config_uri
config_file = config_uri.split('#', 1)[0]
setup_logging(config_file)
self.pshell_file_config(config_file)

# bootstrap the environ
env = self.bootstrap[0](config_uri,
options=parse_vars(self.args.config_vars))
config_vars = parse_vars(self.args.config_vars)
self.setup_logging(config_uri, global_conf=config_vars)
self.pshell_file_config(config_uri, config_vars)
env = self.bootstrap(config_uri, options=config_vars)

# remove the closer from the env
self.closer = env.pop('closer')
Expand Down
60 changes: 18 additions & 42 deletions pyramid/tests/test_scripts/test_pshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ def _getTargetClass(self):
from pyramid.scripts.pshell import PShellCommand
return PShellCommand

def _makeOne(self, patch_bootstrap=True, patch_config=True,
patch_args=True, patch_options=True):
def _makeOne(self, patch_bootstrap=True, patch_logging=True,
patch_loader=True, patch_args=True, patch_options=True):
cmd = self._getTargetClass()([])

if patch_bootstrap:
self.bootstrap = dummy.DummyBootstrap()
cmd.bootstrap = (self.bootstrap,)
if patch_config:
self.config_factory = dummy.DummyConfigParserFactory()
cmd.ConfigParser = self.config_factory
cmd.bootstrap = self.bootstrap
if patch_logging:
self.logging = dummy.dummy_setup_logging()
cmd.setup_logging = self.logging
if patch_loader:
self.loader = dummy.DummyLoader()
cmd.get_loader = self.loader
if patch_args:
class Args(object): pass
self.args = Args()
Expand Down Expand Up @@ -46,9 +49,6 @@ def test_command_loads_default_shell(self):

command.default_runner = shell
command.run()
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,
Expand Down Expand Up @@ -79,9 +79,6 @@ def out(msg):
self.assertEqual(
out_calls, ['could not find a shell named "unknown_python_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.assertTrue(self.bootstrap.closer.called)

Expand All @@ -100,9 +97,6 @@ def test_command_loads_ipython(self):
command.args.python_shell = 'ipython'

command.run()
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,
Expand Down Expand Up @@ -199,12 +193,9 @@ def test_command_loads_custom_items(self):
command = self._makeOne()
model = dummy.Dummy()
user = dummy.Dummy()
self.config_factory.items = [('m', model), ('User', user)]
self.loader.settings = {'pshell': {'m': model, 'User': user}}
shell = dummy.DummyShell()
command.run(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':self.bootstrap.root,
Expand All @@ -223,12 +214,9 @@ def setup(env):
env['a'] = 1
env['root'] = 'root override'
env['none'] = None
self.config_factory.items = [('setup', setup)]
self.loader.settings = {'pshell': {'setup': setup}}
shell = dummy.DummyShell()
command.run(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',
Expand All @@ -252,12 +240,9 @@ def test_command_default_shell_option(self):
'python': dshell,
}
)
self.config_factory.items = [
('default_shell', 'bpython python\nipython')]
self.loader.settings = {'pshell': {
'default_shell': 'bpython python\nipython'}}
command.run()
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.assertTrue(dshell.called)

Expand All @@ -268,12 +253,9 @@ def setup(env):
env['a'] = 1
env['m'] = 'model override'
env['root'] = 'root override'
self.config_factory.items = [('setup', setup), ('m', model)]
self.loader.settings = {'pshell': {'setup': setup, 'm': model}}
shell = dummy.DummyShell()
command.run(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',
Expand All @@ -291,14 +273,10 @@ def setup(env):
env['a'] = 1
env['root'] = 'root override'
model = dummy.Dummy()
self.config_factory.items = [('setup', 'abc'),
('m', model)]
self.loader.settings = {'pshell': {'setup': 'abc', 'm': model}}
command.args.setup = setup
shell = dummy.DummyShell()
command.run(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',
Expand All @@ -313,13 +291,11 @@ def setup(env):
def test_command_custom_section_override(self):
command = self._makeOne()
dummy_ = dummy.Dummy()
self.config_factory.items = [('app', dummy_), ('root', dummy_),
('registry', dummy_), ('request', dummy_)]
self.loader.settings = {'pshell': {
'app': dummy_, 'root': dummy_, 'registry': dummy_,
'request': dummy_}}
shell = dummy.DummyShell()
command.run(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':dummy_, 'root':dummy_, 'registry':dummy_, 'request':dummy_,
Expand Down

0 comments on commit 703c731

Please sign in to comment.