-
Notifications
You must be signed in to change notification settings - Fork 135
/
shell
executable file
·85 lines (73 loc) · 2.56 KB
/
shell
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
#!/usr/bin/env python3
import pygmy
import traceback
from pygmy.core.initialize import initialize
from pygmy.config import config
from pygmy.app.link import shorten, unshorten, link_stats
from pygmy.model import *
initialize()
banner = """
************************************
* Pygmy: Open Source Link Shortner *
************************************
"""
db = config.db.store
pygmy_context = dict(pygmy=pygmy, config=config,
Link=Link, LinkManager=LinkManager,
User=User, UserManager=UserManager,
shorten=shorten, unshorten=unshorten,
link_stats=link_stats, db=db)
def ipython_shell(namespace=None, banner=None, debug=False):
"""Try to run IPython shell."""
try:
import IPython
except ImportError:
if debug:
traceback.print_exc()
print("IPython not available. Running default shell...")
return
# First try newer(IPython >=1.0) top `IPython` level import
if hasattr(IPython, 'terminal'):
from IPython.terminal.embed import InteractiveShellEmbed
kwargs = dict(user_ns=namespace)
else:
from IPython.frontend.terminal.embed import InteractiveShellEmbed
kwargs = dict(user_ns=namespace)
if banner:
kwargs = dict(banner1=banner)
return InteractiveShellEmbed(**kwargs)
def python_shell(namespace=None, banner=None, debug=False):
"""Start a vanilla Python REPL shell."""
import code
from functools import partial
try:
import readline, rlcompleter # NOQA
except ImportError:
if debug:
traceback.print_exc()
else:
readline.parse_and_bind('tab: complete')
# Add global, local and custom namespaces to current shell
default_ns = globals().copy()
default_ns.update(locals())
if namespace:
default_ns.update(namespace)
# Configure kwargs to pass banner
kwargs = dict()
if banner:
kwargs = dict(banner=banner)
shell = code.InteractiveConsole(default_ns)
return partial(shell.interact, **kwargs)
def console(use_ipython=True, namespace=None, banner=None, debug=False):
namespace = {} if namespace is None else namespace
namespace.update(pygmy_context)
# Setup the shell
shell = None
if use_ipython:
shell = ipython_shell(namespace, banner, debug)
if not use_ipython or shell is None:
shell = python_shell(namespace, banner, debug)
shell()
# In future setup.py will make pygmy a command and typing pygmy console
# will start the shell
console()