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, ...@@ -47,6 +47,7 @@ setup(name=name,
'cliff', 'cliff',
'requests', 'requests',
'ipython', 'ipython',
'bpython',
] + additional_install_requires, ] + additional_install_requires,
extra_requires={'docs': ( extra_requires={'docs': (
'Sphinx', 'Sphinx',
......
...@@ -6,6 +6,10 @@ from slapos.cli.config import ClientConfigCommand ...@@ -6,6 +6,10 @@ from slapos.cli.config import ClientConfigCommand
from slapos.client import init, do_console, ClientConfig from slapos.client import init, do_console, ClientConfig
class ShellNotFound(Exception):
pass
class ConsoleCommand(ClientConfigCommand): class ConsoleCommand(ClientConfigCommand):
""" """
open python console with slap library imported open python console with slap library imported
...@@ -34,33 +38,73 @@ class ConsoleCommand(ClientConfigCommand): ...@@ -34,33 +38,73 @@ class ConsoleCommand(ClientConfigCommand):
ap.add_argument('-c', '--cert_file', ap.add_argument('-c', '--cert_file',
help='SSL Authorisation certificate 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 return ap
def take_action(self, args): def take_action(self, args):
configp = self.fetch_config(args) configp = self.fetch_config(args)
conf = ClientConfig(args, configp) conf = ClientConfig(args, configp)
local = init(conf) local = init(conf)
if not any([args.python, args.ipython, args.bpython]):
args.bpython = True
if args.ipython:
try: try:
import IPython
do_ipython_console(local) do_ipython_console(local)
except ImportError: 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) 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): def do_ipython_console(local):
try:
from IPython import embed from IPython import embed
embed(banner1=textwrap.dedent("""\ except ImportError:
slapos console allows you interact with slap API. You can play with the global raise ShellNotFound
"slap" object and with the global request() and supply() methods. embed(banner1=console_banner,
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.',
user_ns=local) 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