Commit 0353c365 authored by Marco Mariani's avatar Marco Mariani

console: bpython support

both ipython and bpython are built and provided with slapos. bpython is the
default now, with colorful drop down menus. we can choose later.
parent f51f1b48
......@@ -47,6 +47,7 @@ setup(name=name,
'cliff',
'requests',
'ipython',
'bpython',
] + additional_install_requires,
extra_requires={'docs': (
'Sphinx',
......
......@@ -6,6 +6,10 @@ from slapos.cli.config import ClientConfigCommand
from slapos.client import init, do_console, ClientConfig
class ShellNotFound(Exception):
pass
class ConsoleCommand(ClientConfigCommand):
"""
open python console with slap library imported
......@@ -34,33 +38,73 @@ class ConsoleCommand(ClientConfigCommand):
ap.add_argument('-c', '--cert_file',
help='SSL Authorisation certificate file')
shell = ap.add_mutually_exclusive_group()
shell.add_argument('-b', '--bpython',
action='store_true',
help='Use BPython shell if available (default)')
shell.add_argument('-i', '--ipython',
action='store_true',
help='Use IPython shell if available')
shell.add_argument('-p', '--python',
action='store_true',
help='Use plain Python shell')
return ap
def take_action(self, args):
configp = self.fetch_config(args)
conf = ClientConfig(args, configp)
local = init(conf)
try:
import IPython
do_ipython_console(local)
except ImportError:
if not any([args.python, args.ipython, args.bpython]):
args.bpython = True
if args.ipython:
try:
do_ipython_console(local)
except ShellNotFound:
self.app.log.info('IPython not available - using plain Python shell')
do_console(local)
elif args.bpython:
try:
do_bpython_console(local)
except ShellNotFound:
self.app.log.info('bpython not available - using plain Python shell')
do_console(local)
else:
do_console(local)
console_banner = """\
slapos console allows you interact with slap API. You can play with the global
"slap" object and with the global request() and supply() methods.
examples :
>>> # Request instance
>>> request(kvm, "myuniquekvm")
>>> # Request software installation on owned computer
>>> supply(kvm, "mycomputer")
>>> # Fetch instance informations on already launched instance
>>> request(kvm, "myuniquekvm").getConnectionParameter("url")
"""
def do_bpython_console(local):
try:
from bpython import embed
except ImportError:
raise ShellNotFound
embed(banner=console_banner,
locals_=local)
def do_ipython_console(local):
from IPython import embed
embed(banner1=textwrap.dedent("""\
slapos console allows you interact with slap API. You can play with the global
"slap" object and with the global request() and supply() methods.
examples :
>>> # Request instance
>>> request(kvm, "myuniquekvm")
>>> # Request software installation on owned computer
>>> supply(kvm, "mycomputer")
>>> # Fetch instance informations on already launched instance
>>> request(kvm, "myuniquekvm").getConnectionParameter("url")
"""),
#exit_msg='BYE.',
try:
from IPython import embed
except ImportError:
raise ShellNotFound
embed(banner1=console_banner,
user_ns=local)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment