-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshell.py
executable file
·82 lines (63 loc) · 2.2 KB
/
shell.py
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
#!/usr/bin/env python
# coding: utf-8
import os
import sys
import atexit
import IPython
from citadel.app import create_app
def hook_readline_hist():
try:
# Try to set up command history completion/saving/reloading
import readline
except ImportError:
return
# The place to store your command history between sessions
histfile = os.environ["HOME"] + "/.citadel_history"
readline.parse_and_bind('tab: complete')
try:
readline.read_history_file(histfile)
except IOError:
pass # It doesn't exist yet.
def save_hist():
try:
readline.write_history_file(histfile)
except:
print('Unable to save Python command history')
atexit.register(save_hist)
def get_banner():
return 'In Citadel shell now\n'
def pre_imports():
from citadel.models import User, App, Release, AppUserRelation, Container, ELBInstance, OPLog
from citadel.tasks import remove_container
from citadel.ext import db, rds, get_etcd
from citadel.rpc.client import get_core
return locals()
def ipython_shell(user_ns):
if getattr(IPython, 'version_info', None) and IPython.version_info[0] >= 1:
from IPython.terminal.ipapp import TerminalIPythonApp
from IPython.terminal.interactiveshell import TerminalInteractiveShell
else:
from IPython.frontend.terminal.ipapp import TerminalIPythonApp
from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
class ShireIPythonApp(TerminalIPythonApp):
def init_shell(self):
self.shell = TerminalInteractiveShell.instance(
config=self.config,
display_banner=False,
profile_dir=self.profile_dir,
ipython_dir=self.ipython_dir,
banner1=get_banner(),
banner2=''
)
self.shell.configurables.append(self)
app = ShireIPythonApp.instance()
app.initialize()
app.shell.user_ns.update(user_ns)
citadel_app = create_app()
with citadel_app.app_context():
sys.exit(app.start())
def main():
hook_readline_hist()
ipython_shell(pre_imports())
if __name__ == '__main__':
main()