-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add commands collector and info on readme
- Loading branch information
1 parent
ac25b36
commit e17d24e
Showing
3 changed files
with
157 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,57 @@ | ||
# coding: utf-8 | ||
|
||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import click | ||
import code | ||
import readline | ||
import rlcompleter | ||
# import importlib | ||
|
||
|
||
@click.group() | ||
def core_cmd(): | ||
""" Core commands wrapper """ | ||
pass | ||
|
||
|
||
# class RobotteloLoader(object): | ||
# def __getattr__(self, item): | ||
# return importlib.import_module('robottelo.{0}'.format(item)) | ||
|
||
|
||
@core_cmd.command() | ||
@click.option('--ipython/--no-ipython', default=True) | ||
def shell(ipython): | ||
"""Runs a Python shell with Robottelo context""" | ||
_vars = globals() | ||
_vars.update(locals()) | ||
auto_imported = { | ||
} | ||
_vars.update(auto_imported) | ||
banner_msg = ( | ||
'Welcome to PROGRAM interactive shell\n' | ||
'\tAuto imported: {0}\n' | ||
).format(auto_imported.keys()) | ||
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 | ||
c = Config() | ||
c.TerminalInteractiveShell.banner2 = banner_msg | ||
start_ipython(argv=[], user_ns=_vars, config=c) | ||
else: | ||
raise ImportError | ||
except ImportError: | ||
shell = code.InteractiveConsole(_vars) | ||
shell.interact(banner=banner_msg) | ||
|
||
@click.command() | ||
def main(args=None): | ||
"""Console script for manage""" | ||
click.echo("Replace this message by putting your code into " | ||
"manage.cli.main") | ||
click.echo("See click documentation at http://http://click.pocoo.org/") | ||
|
||
help_text = """ | ||
PROGRAM Interactive shell! | ||
""" | ||
main = click.CommandCollection(help=help_text) | ||
main.add_source(core_cmd) | ||
|
||
if __name__ == "__main__": | ||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import sys | ||
import os | ||
import click | ||
import importlib | ||
|
||
|
||
class CommandsCollector(click.MultiCommand): | ||
"""A MultiCommand to collect all click commands from a given | ||
modules path and base name for the module. | ||
The commands functions needs to be in a module inside commands | ||
folder and the name of the file will be used as the command name. | ||
""" | ||
|
||
def __init__(self, modules_path, base_module_name, **attrs): | ||
click.MultiCommand.__init__(self, **attrs) | ||
self.base_module_name = base_module_name | ||
self.modules_path = modules_path | ||
|
||
def list_commands(self, *args, **kwargs): | ||
commands = [] | ||
for _path, _dir, _ in os.walk(self.modules_path): | ||
if 'commands' not in _dir: | ||
continue | ||
for filename in os.listdir(os.path.join(_path, 'commands')): | ||
if filename.endswith('.py') and filename != '__init__.py': | ||
cmd = filename[:-3] | ||
_, module_name = os.path.split(_path) | ||
commands.append('{0}_{1}'.format(module_name, cmd)) | ||
commands.sort() | ||
return commands | ||
|
||
def get_command(self, ctx, name): | ||
try: | ||
if sys.version_info[0] == 2: | ||
name = name.encode('ascii', 'replace') | ||
splitted = name.split('_') | ||
if len(splitted) <= 1: | ||
return | ||
module_name, command_name = splitted | ||
if not all([module_name, command_name]): | ||
return | ||
module = '{0}.{1}.commands.{2}'.format( | ||
self.base_module_name, | ||
module_name, | ||
command_name) | ||
mod = importlib.import_module(module) | ||
except ImportError: | ||
return | ||
return getattr(mod, 'cli', None) |