Commit 45a41d05 authored by Theophile GENTILHOMME's avatar Theophile GENTILHOMME
Browse files

[algorithms] Click implementation of algo commands

Reimplement algorithms existing
commands using  plugin-based command line mechanism (Click).
parent 68fa83e1
......@@ -25,68 +25,8 @@
# #
###############################################################################
"""Usage:
%(prog)s algorithms list [--remote]
%(prog)s algorithms check [<name>]...
%(prog)s algorithms path [<name>]...
%(prog)s algorithms edit <name>...
%(prog)s algorithms pull [--force] [<name>]...
%(prog)s algorithms push [--force] [--dry-run] [<name>]...
%(prog)s algorithms diff <name>
%(prog)s algorithms status
%(prog)s algorithms create <name>...
%(prog)s algorithms version <name>
%(prog)s algorithms fork <src> <dst>
%(prog)s algorithms rm [--remote] <name>...
%(prog)s algorithms execute <instructions>
%(prog)s algorithms execute --examples
%(prog)s algorithms --help
Commands:
list Lists all the algorithms available on the platform
path Displays local path of algorithm files
edit Edit local algorithm file
check Checks a local algorithm for validity
pull Downloads the specified algorithms from the server
push Uploads algorithms to the server
diff Shows changes between the local algorithm and the remote version
status Shows (editing) status for all available algorithms
create Creates a new local algorithm
version Creates a new version of an existing algorithm
fork Forks a local algorithm
rm Deletes a local algorithm (unless --remote is specified)
execute Execute an algorithm following instructions in a JSON file
Options:
--force Performs operation regardless of conflicts
--dry-run Doesn't really perform the task, just comments what would do
--remote Only acts on the remote copy of the algorithm
--examples Display some example JSON instruction files
--help Display this screen
Arguments for 'execute':
<instructions> JSON file containing the instructions
Examples:
To list all algorithms available locally:
$ %(prog)s al list
To list all algorithms available at the platform:
$ %(prog)s al list --remote
"""
import click
import logging
logger = logging.getLogger(__name__)
import os
import sys
import docopt
......@@ -103,8 +43,10 @@ from beat.backend.python.database import Storage as DatabaseStorage
from beat.backend.python.algorithm import Storage as AlgorithmStorage
logger = logging.getLogger(__name__)
def pull(webapi, prefix, names, force, indentation, format_cache, lib_cache):
def pull_impl(webapi, prefix, names, force, indentation, format_cache, lib_cache):
"""Copies algorithms (and required libraries/dataformats) from the server.
Parameters:
......@@ -255,7 +197,7 @@ To execute an analyzer:
def execute(prefix, cache, instructions_file):
def execute_impl(prefix, cache, instructions_file):
try:
# Load the JSON configuration
if not os.path.exists(instructions_file):
......@@ -346,70 +288,205 @@ def execute(prefix, cache, instructions_file):
def process(args):
@click.group()
@click.pass_context
def algorithms(ctx):
"""Configuration and manipulation of algorithms"""
pass
@algorithms.command()
@click.option('--remote', help='Only acts on the remote copy of the algorithm',
is_flag=True)
@click.pass_context
def list(ctx, remote):
'''Lists all the algorithms available on the platform
Example:
$ beat algorithms list --remote
'''
if remote:
with common.make_webapi(ctx.meta['config']) as webapi:
return common.display_remote_list(webapi, 'algorithm')
else:
return common.display_local_list(ctx.meta['config'].path, 'algorithm')
@algorithms.command()
@click.argument('names', nargs=-1)
@click.pass_context
def path(ctx, names):
'''Displays local path of algorithm files
if args['list']:
if args['--remote']:
with common.make_webapi(args['config']) as webapi:
return common.display_remote_list(webapi, 'algorithm')
else:
return common.display_local_list(args['config'].path, 'algorithm')
Example:
$ beat algorithms path xxx
'''
return common.display_local_path(ctx.meta['config'].path, 'algorithm', names)
@algorithms.command()
@click.argument('name', nargs=1)
@click.pass_context
def edit(ctx, name):
'''Edit local algorithm file
elif args['path']:
return common.display_local_path(args['config'].path, 'algorithm', args['<name>'])
Example:
$ beat algorithms edit xxx
'''
return common.edit_local_file(ctx.meta['config'].path,
ctx.meta['config'].editor, 'algorithm',
name)
elif args['edit']:
return common.edit_local_file(args['config'].path, args['config'].editor, 'algorithm', args['<name>'][0])
elif args['check']:
return common.check(args['config'].path, 'algorithm', args['<name>'])
@algorithms.command()
@click.argument('name', nargs=1)
@click.pass_context
def check(ctx, name):
'''Checks a local algorithm for validity
elif args['pull']:
with common.make_webapi(args['config']) as webapi:
return pull(webapi, args['config'].path, args['<name>'],
args['--force'], 0, {}, {})
Example:
$ beat algorithms check xxx
'''
return common.check(ctx.meta['config'].path, 'algorithm', name)
elif args['push']:
with common.make_webapi(args['config']) as webapi:
return common.push(webapi, args['config'].path, 'algorithm',
args['<name>'], ['name', 'declaration', 'code', 'description'],
{}, args['--force'], args['--dry-run'], 0)
elif args['diff']:
with common.make_webapi(args['config']) as webapi:
return common.diff(webapi, args['config'].path, 'algorithm',
args['<name>'][0], ['declaration', 'code', 'description'])
elif args['status']:
with common.make_webapi(args['config']) as webapi:
return common.status(webapi, args['config'].path, 'algorithm')[0]
@algorithms.command()
@click.argument('name', nargs=1)
@click.option('--force', help='Performs operation regardless of conflicts',
is_flag=True)
@click.pass_context
def pull(ctx, name, force):
'''Downloads the specified algorithms from the server
elif args['create']:
return common.create(args['config'].path, 'algorithm', args['<name>'])
Example:
$ beat algorithms pull --force yyy
'''
with common.make_webapi(ctx.meta['config']) as webapi:
return pull_impl(webapi, ctx.meta['config'].path, name, force, 0, {}, {})
elif args['version']:
return common.new_version(args['config'].path, 'algorithm',
args['<name>'][0])
elif args['fork']:
return common.fork(args['config'].path, 'algorithm',
args['<src>'], args['<dst>'])
elif args['execute']:
if args['--examples']:
print_examples()
return 0
@algorithms.command()
@click.argument('name', nargs=1)
@click.option('--force', help='Performs operation regardless of conflicts',
is_flag=True)
@click.option('--dry-run', help="Doesn't really perform the task, just "
"comments what would do", is_flag=True)
@click.pass_context
def push(ctx, name, force, dry_run):
'''Uploads algorithms to the server
return execute(args['config'].path, args['config'].cache, args['<instructions>'])
Example:
$ beat algorithms push --dry-run yyy
'''
with common.make_webapi(ctx.meta['config']) as webapi:
return common.push(webapi, ctx.meta['config'].path, 'algorithm',
name, ['name', 'declaration', 'code', 'description'],
{}, force, dry_run, 0)
elif args['rm']:
if args['--remote']:
with common.make_webapi(args['config']) as webapi:
return common.delete_remote(webapi, 'algorithm', args['<name>'])
else:
return common.delete_local(args['config'].path, 'algorithm',
args['<name>'])
# Should not happen
logger.error("unrecognized `algorithms' subcommand")
return 1
@algorithms.command()
@click.argument('name', nargs=1)
@click.pass_context
def diff(ctx, name):
'''Shows changes between the local algorithm and the remote version
Example:
$ beat algorithms diff xxx
'''
with common.make_webapi(ctx.meta['config']) as webapi:
return common.diff(webapi, ctx.meta['config'].path, 'algorithm',
name, ['declaration', 'code', 'description'])
@algorithms.command()
@click.pass_context
def status(ctx):
'''Shows (editing) status for all available algorithms
Example:
$ beat algorithms status
'''
with common.make_webapi(ctx.meta['config']) as webapi:
return common.status(webapi, ctx.meta['config'].path, 'algorithm')[0]
@algorithms.command()
@click.argument('name', nargs=1)
@click.pass_context
def create(ctx, name):
'''Creates a new local algorithm
Example:
$ beat algorithms create xxx
'''
return common.create(ctx.meta['config'].path, 'algorithm', [name])
@algorithms.command()
@click.argument('name', nargs=1)
@click.pass_context
def version(ctx, name):
'''Creates a new version of an existing algorithm
Example:
$ beat algorithms version xxx
'''
return common.new_version(ctx.meta['config'].path, 'algorithm', name)
@algorithms.command()
@click.argument('src', nargs=1)
@click.argument('dst', nargs=1)
@click.pass_context
def fork(ctx, src, dst):
'''Forks a local algorithm
Example:
$ beat algorithms fork xxx yyy
'''
return common.fork(ctx.meta['config'].path, 'algorithm', src, dst)
@algorithms.command()
@click.argument('name', nargs=1)
@click.option('--remote', help='Only acts on the remote copy of the algorithm',
is_flag=True)
@click.pass_context
def rm(ctx, name, remote):
'''Deletes a local algorithm (unless --remote is specified)
Example:
$ beat algorithms rm xxx
'''
if remote:
with common.make_webapi(ctx.meta['config']) as webapi:
return common.delete_remote(webapi, 'algorithm', name)
else:
return common.delete_local(ctx.meta['config'].path, 'algorithm', name)
@algorithms.command()
@click.argument('instructions', nargs=-1)
@click.option('--example', help='Display some example JSON instruction files',
is_flag=True)
@click.pass_context
def execute(ctx, instructions, example):
'''Execute an algorithm following instructions in a JSON file
Example:
$ beat algorithms execute <instructions>
$ beat algorithms execute --examples
'''
if example:
print_examples()
return 0
return execute(ctx.meta['config'].path, ctx.meta['config'].cache,
instructions)
......@@ -177,7 +177,7 @@ def path(ctx, names):
return common.display_local_path(ctx.meta['config'].path, 'library', names)
@library.command()
@libraries.command()
@click.argument('name', nargs=1)
@click.pass_context
def edit(ctx, name):
......
Supports Markdown
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