-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_shell.py
More file actions
102 lines (81 loc) · 3.01 KB
/
flask_shell.py
File metadata and controls
102 lines (81 loc) · 3.01 KB
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
import os
import sys
import click
from flask.cli import with_appcontext
def ptipython_shell(ctx, banner):
from ptpython.ipython import embed
from ptpython.repl import run_config
from ptpython.entry_points.run_ptpython import get_config_and_history_file, create_parser
a = create_parser().parse_args(args=[])
config_file, history_file = get_config_and_history_file(a)
def configure(repl):
if os.path.exists(config_file):
run_config(repl, config_file)
embed(banner1=banner, user_ns=ctx, history_filename=history_file, configure=configure)
def ptpython_shell(ctx, banner):
from ptpython.repl import embed, run_config
from ptpython.entry_points.run_ptpython import get_config_and_history_file, create_parser
a = create_parser().parse_args(args=[])
config_file, history_file = get_config_and_history_file(a)
def configure(repl):
if os.path.exists(config_file):
run_config(repl, config_file)
print(banner)
embed(globals=ctx, history_filename=history_file, configure=configure)
def ipython_shell(ctx, banner):
from IPython import start_ipython
from IPython.terminal.ipapp import load_default_config
config = load_default_config()
config.TerminalInteractiveShell.banner1 = banner
start_ipython(argv=[], config=config, user_ns=ctx)
def bpython_shell(ctx, banner):
from bpython import embed
embed(banner=banner, locals_=ctx)
def plain_shell(ctx, banner):
import code
code.interact(banner=banner, local=ctx)
SHELL_TYPE = ['ptipython', 'ptpython', 'ipython', 'bpython', 'plain']
SHELL_MAP = {
'ptipython': ptipython_shell,
'ptpython': ptpython_shell,
'ipython': ipython_shell,
'bpython': bpython_shell,
'plain': plain_shell,
}
@click.command('shell', short_help='Runs a shell in the app context.')
@click.option('--use-shell', type=click.Choice(SHELL_TYPE))
@with_appcontext
def shell_command(use_shell):
"""Runs an interactive Python shell in the context of a given
Flask application. The application will populate the default
namespace of this shell according to it's configuration.
This is useful for executing small snippets of management code
without having to manually configuring the application.
"""
from flask.globals import _app_ctx_stack
app = _app_ctx_stack.top.app
banner = 'Python %s on %s\nApp: %s [%s]\nInstance: %s' % (
sys.version,
sys.platform,
app.import_name,
app.env,
app.instance_path,
)
ctx = {}
startup = os.environ.get('PYTHONSTARTUP')
if startup and os.path.isfile(startup):
with open(startup, 'r') as f:
eval(compile(f.read(), startup, 'exec'), ctx)
ctx.update(app.make_shell_context())
if use_shell:
try:
SHELL_MAP.get(use_shell)(ctx, banner)
return
except ImportError:
pass
for key in SHELL_TYPE:
try:
SHELL_MAP.get(key)(ctx, banner)
return
except ImportError:
pass