Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'pulp-smash shell' command. #1188

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/about.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ Learning Pulp Smash
Not sure where to start? Consider reading some existing tests in `Pulp 2
Tests`_.

Pulp Smash Interactive Console
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The command ``pulp-smash shell`` opens an interactive Python console with Pulp-Smash
most common used objects already imported in to the context.

The configuration for the shell is read from
``XDG HOME`` usually ``~/.config/pulp_smash/settings.json``
optionally it is possible to set on env ``export PULP_SMASH_CONFIG_FILE=/path/to/settings.json``
or by passing it to the command line as in ``pulp-smash shell --config ~/path/to/settings.json``

.. raw:: html

<script id="asciicast-235178" src="https://asciinema.org/a/235178.js" async></script>
<a href="https://asciinema.org/a/235178">https://asciinema.org/a/235178</a>

Code Standards
~~~~~~~~~~~~~~

Expand Down
66 changes: 66 additions & 0 deletions pulp_smash/pulp_smash_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""The entry point for Pulp Smash's command line interface."""
import json
import sys
import warnings

import click
from packaging.version import Version
Expand Down Expand Up @@ -364,5 +365,70 @@ def settings_validate(ctx):
) from err


@pulp_smash.command()
@click.option('--ipython/--no-ipython', default=True, help='Disable ipython')
@click.option(
'--config',
'_settingspath',
default=None,
help='Optional path to settings file',
type=click.Path(exists=True)
)
def shell(ipython, _settingspath): # pragma: no cover
"""Run a Python shell with Pulp-Smash context.

Start an interactive shell with pulp-smash objects available, if `ipython`
is installed it will start ipython session, else it will start standard
python shell.
"""
# pylint:disable=R0914
import code
import readline
import rlcompleter

# trick to to make subpackages available
from pulp_smash import api, cli, utils, pulp2, pulp3, constants # pylint:disable=W0641
from pulp_smash.pulp2 import constants as pulp2_constants # pylint:disable=W0641
from pulp_smash.pulp3 import constants as pulp3_constants # pylint:disable=W0641
from pulp_smash.pulp2 import utils as pulp2_utils # pylint:disable=W0641
from pulp_smash.pulp3 import utils as pulp3_utils # pylint:disable=W0641
# //

banner_msg = (
'Welcome to Pulp-Smash interactive shell\n'
'\tAuto imported: api, cli, config, utils, pulp2, pulp3, constants, '
'exceptions\n'
)
_vars = globals()
try:
cfg = config.get_config()
except exceptions.ConfigFileNotFoundError as exc:
warnings.warn(str(exc))
cfg = 'Please create your instance of cfg or set the settings location'
else:
api.client = api.Client(cfg)
cli.client = cli.Client(cfg)
banner_msg += '\tAvailable objects: api.client, cli.client, cfg\n'
finally:
_vars.update(locals())

readline.set_completer(rlcompleter.Completer(_vars).complete)
readline.parse_and_bind('tab: complete')
try:
if ipython is True:
from IPython import start_ipython
from traitlets.config import Config
conf = Config()
conf.TerminalInteractiveShell.banner2 = banner_msg
start_ipython(argv=[], user_ns=_vars, config=conf)
else:
raise ImportError
except ImportError:
if ipython is True:
warnings.warn('Cannot load ipython, please `pip install ipython`')
_shell = code.InteractiveConsole(_vars)
_shell.interact(banner=banner_msg)


if __name__ == '__main__':
pulp_smash() # pragma: no cover