/usr/lib/python2.7/dist-packages/pyramid/scripts/pshell.py is in python-pyramid 1.4.5+dfsg-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | from code import interact
import optparse
import sys
import textwrap
from pyramid.compat import configparser
from pyramid.util import DottedNameResolver
from pyramid.paster import bootstrap
from pyramid.paster import setup_logging
from pyramid.scripts.common import parse_vars
def main(argv=sys.argv, quiet=False):
command = PShellCommand(argv, quiet)
return command.run()
class PShellCommand(object):
usage = '%prog config_uri'
description = """\
Open an interactive shell with a Pyramid app loaded. This command
accepts one positional argument named "config_uri" which specifies the
PasteDeploy config file to use for the interactive shell. The format is
"inifile#name". If the name is left off, the Pyramid default application
will be assumed. Example: "pshell myapp.ini#main"
If you do not point the loader directly at the section of the ini file
containing your Pyramid application, the command will attempt to
find the app for you. If you are loading a pipeline that contains more
than one Pyramid application within it, the loader will use the
last one.
"""
bootstrap = (bootstrap,) # for testing
parser = optparse.OptionParser(
usage,
description=textwrap.dedent(description)
)
parser.add_option('-p', '--python-shell',
action='store', type='string', dest='python_shell',
default='', help='ipython | bpython | python')
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 = {}
setup = None
def __init__(self, argv, quiet=False):
self.quiet = quiet
self.options, self.args = self.parser.parse_args(argv[1:])
def pshell_file_config(self, filename):
config = self.ConfigParser()
config.read(filename)
try:
items = config.items('pshell')
except configparser.NoSectionError:
return
resolver = DottedNameResolver(None)
self.loaded_objects = {}
self.object_help = {}
self.setup = None
for k, v in items:
if k == 'setup':
self.setup = v
else:
self.loaded_objects[k] = resolver.maybe_resolve(v)
self.object_help[k] = v
def out(self, msg): # pragma: no cover
if not self.quiet:
print(msg)
def run(self, shell=None):
if not self.args:
self.out('Requires a config file argument')
return 2
config_uri = self.args[0]
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[1:]))
# remove the closer from the env
closer = env.pop('closer')
# setup help text for default environment
env_help = dict(env)
env_help['app'] = 'The WSGI application.'
env_help['root'] = 'Root of the default resource tree.'
env_help['registry'] = 'Active Pyramid registry.'
env_help['request'] = 'Active request object.'
env_help['root_factory'] = (
'Default root factory used to create `root`.')
# override use_script with command-line options
if self.options.setup:
self.setup = self.options.setup
if self.setup:
# store the env before muddling it with the script
orig_env = env.copy()
# 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 env.items():
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)
# eliminate duplicates from env, allowing custom vars to override
for k in self.loaded_objects:
if k in env_help:
del env_help[k]
# generate help text
help = ''
if env_help:
help += 'Environment:'
for var in sorted(env_help.keys()):
help += '\n %-12s %s' % (var, env_help[var])
if self.object_help:
help += '\n\nCustom Variables:'
for var in sorted(self.object_help.keys()):
help += '\n %-12s %s' % (var, self.object_help[var])
if shell is None:
shell = self.make_shell()
try:
shell(env, help)
finally:
closer()
def make_shell(self):
shell = None
user_shell = self.options.python_shell.lower()
if not user_shell:
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_bpython_shell()
elif user_shell == 'ipython':
shell = self.make_ipython_v0_11_shell()
if shell is None:
shell = self.make_ipython_v0_10_shell()
elif user_shell == 'bpython':
shell = self.make_bpython_shell()
if shell is None:
shell = self.make_default_shell()
return shell
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\n' + help + '\n'
interact(banner, local=env)
return shell
def make_bpython_shell(self, BPShell=None):
if BPShell is None: # pragma: no cover
try:
from bpython import embed
BPShell = embed
except ImportError:
return None
def shell(env, help):
BPShell(locals_=env, banner=help + '\n')
return shell
def make_ipython_v0_11_shell(self, IPShellFactory=None):
if IPShellFactory is None: # pragma: no cover
try:
from IPython.frontend.terminal.embed import (
InteractiveShellEmbed)
IPShellFactory = InteractiveShellEmbed
except ImportError:
return None
def shell(env, help):
IPShell = IPShellFactory(banner2=help + '\n', user_ns=env)
IPShell()
return shell
def make_ipython_v0_10_shell(self, IPShellFactory=None):
if IPShellFactory is None: # pragma: no cover
try:
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
|