Commit 63c310c9 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[cmdline] Pre-commit cleanup

parent 771a2cae
Pipeline #40957 passed with stage
in 4 minutes and 15 seconds
...@@ -34,26 +34,24 @@ ...@@ -34,26 +34,24 @@
################################################################################### ###################################################################################
import click
import logging import logging
import os import os
import simplejson as json
import click
import simplejson as json
from beat.backend.python.algorithm import Storage as AlgorithmStorage
from beat.core import algorithm from beat.core import algorithm
from beat.core.execution import DockerExecutor
from beat.core.dock import Host
from beat.core import hash from beat.core import hash
from beat.backend.python.algorithm import Storage as AlgorithmStorage from beat.core.dock import Host
from beat.core.execution import DockerExecutor
from . import common
from . import commands from . import commands
from . import common
from .decorators import raise_on_error
from .click_helper import AliasedGroup from .click_helper import AliasedGroup
from .click_helper import AssetCommand from .click_helper import AssetCommand
from .click_helper import AssetInfo from .click_helper import AssetInfo
from .decorators import raise_on_error
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
......
...@@ -34,20 +34,20 @@ ...@@ -34,20 +34,20 @@
################################################################################### ###################################################################################
import os
import click
import fnmatch import fnmatch
import logging import logging
import os
import click
import simplejson import simplejson
from beat.core.data import CachedDataSource, load_data_index from beat.core.data import CachedDataSource
from beat.core.data import load_data_index
from beat.core.utils import NumpyJSONEncoder from beat.core.utils import NumpyJSONEncoder
from . import common from . import common
from .decorators import raise_on_error
from .click_helper import AliasedGroup from .click_helper import AliasedGroup
from .decorators import raise_on_error
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
......
...@@ -33,13 +33,13 @@ ...@@ -33,13 +33,13 @@
# # # #
################################################################################### ###################################################################################
import click
import types import types
from .scripts import main_cli import click
from .decorators import raise_on_error
from . import common from . import common
from .decorators import raise_on_error
from .scripts import main_cli
def copy_func(f, name=None): def copy_func(f, name=None):
......
...@@ -36,29 +36,29 @@ ...@@ -36,29 +36,29 @@
"""Utility functions that are useful to all sub-commands""" """Utility functions that are useful to all sub-commands"""
import os
import glob
import fnmatch
import difflib
import collections import collections
import difflib
import fnmatch
import glob
import logging import logging
import os
from enum import Enum
from enum import unique
import simplejson
import six import six
import termcolor import termcolor
import simplejson
from enum import Enum, unique
from beat.core import dataformat from beat.core import algorithm
from beat.core import database from beat.core import database
from beat.core import dataformat
from beat.core import experiment
from beat.core import library from beat.core import library
from beat.core import plotter from beat.core import plotter
from beat.core import plotterparameter from beat.core import plotterparameter
from beat.core import protocoltemplate from beat.core import protocoltemplate
from beat.core import algorithm
from beat.core import toolchain from beat.core import toolchain
from beat.core import experiment
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
......
...@@ -37,91 +37,85 @@ ...@@ -37,91 +37,85 @@
"""Configuration manipulation and display""" """Configuration manipulation and display"""
import os
import sys
import copy import copy
import logging
import getpass import getpass
import logging
import os
import sys
import click import click
import simplejson import simplejson
from .decorators import verbosity_option
from .click_helper import AliasedGroup from .click_helper import AliasedGroup
from .decorators import verbosity_option
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Default values for the command-line utility
DEFAULTS = { DEFAULTS = {
'platform': 'https://www.beat-eu.org/platform/', "platform": "https://www.beat-eu.org/platform/",
'user': getpass.getuser(), "user": getpass.getuser(),
'token': None, "token": None,
'prefix': os.path.realpath(os.path.join(os.curdir, 'prefix')), "prefix": os.path.realpath(os.path.join(os.curdir, "prefix")),
'cache': 'cache', "cache": "cache",
'editor': None, "editor": None,
} }
"""Default values for the command-line utility"""
# Documentation for the configuration parameters
DOC = { DOC = {
'platform': 'Web address of the BEAT platform', "platform": "Web address of the BEAT platform",
'user': 'User name for operations that create, delete or edit objects', "user": "User name for operations that create, delete or edit objects",
'token': 'Secret key of the user on the BEAT platform', "token": "Secret key of the user on the BEAT platform",
'prefix': 'Directory containing BEAT objects', "prefix": "Directory containing BEAT objects",
'cache': 'Directory to use for data caching (relative to prefix)', "cache": "Directory to use for data caching (relative to prefix)",
'editor': 'Editor to be used to edit local files', "editor": "Editor to be used to edit local files",
} }
"""Documentation for configuration parameters"""
class Configuration(object): class Configuration(object):
'''Keeps track of configuration elements''' """Keeps track of configuration elements"""
def __init__(self, args): def __init__(self, args):
self.files = [ self.files = [
os.path.expanduser('~/.beatrc'), os.path.expanduser("~/.beatrc"),
os.path.realpath('./.beatrc'), os.path.realpath("./.beatrc"),
] ]
self.__data = copy.deepcopy(DEFAULTS) self.__data = copy.deepcopy(DEFAULTS)
for k in self.files: for k in self.files:
if os.path.exists(k): if os.path.exists(k):
with open(k, 'rt') as f: tmp = simplejson.load(f) with open(k, "rt") as f:
tmp = simplejson.load(f)
self.__data.update(tmp) self.__data.update(tmp)
logger.info("Loaded configuration file `%s'", k) logger.info("Loaded configuration file `%s'", k)
for key in DEFAULTS: for key in DEFAULTS:
self.__data[key] = args.get('--%s' % key) or self.__data[key] self.__data[key] = args.get("--%s" % key) or self.__data[key]
@property @property
def path(self): def path(self):
'''The directory for the prefix''' """The directory for the prefix"""
return self.__data['prefix']
return self.__data["prefix"]
@property @property
def cache(self): def cache(self):
'''The directory for the cache''' """The directory for the cache"""
if os.path.isabs(self.__data['cache']):
return self.__data['cache']
return os.path.join(self.__data['prefix'], self.__data['cache'])
if os.path.isabs(self.__data["cache"]):
return self.__data["cache"]
return os.path.join(self.__data["prefix"], self.__data["cache"])
@property @property
def database_paths(self): def database_paths(self):
'''A dict of paths for databases''' """A dict of paths for databases"""
return dict(
(k, self.__data[k]) for k in self.__data if self.is_database_key(k)
)
return dict((k, self.__data[k]) for k in self.__data if self.is_database_key(k))
def set(self, key, value, local=False): def set(self, key, value, local=False):
'''Sets or resets a field in the configuration''' """Sets or resets a field in the configuration"""
if not self._is_valid_key(key): if not self._is_valid_key(key):
logger.error("Don't know about parameter `%s'", key) logger.error("Don't know about parameter `%s'", key)
...@@ -134,9 +128,8 @@ class Configuration(object): ...@@ -134,9 +128,8 @@ class Configuration(object):
self.save(local) self.save(local)
def save(self, local=False): def save(self, local=False):
'''Saves contents to configuration file """Saves contents to configuration file
Parameters: Parameters:
...@@ -144,39 +137,34 @@ class Configuration(object): ...@@ -144,39 +137,34 @@ class Configuration(object):
if set to ``True``, then save if set to ``True``, then save
configuration values to local configuration file (typically configuration values to local configuration file (typically
``.beatrc``) ``.beatrc``)
''' """
path = self.files[0] path = self.files[0]
if local: if local:
path = self.files[1] path = self.files[1]
flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
with os.fdopen(os.open(path, flags, 0o600), 'wt') as f: with os.fdopen(os.open(path, flags, 0o600), "wt") as f:
f.write(simplejson.dumps( f.write(
self.__data, sort_keys=True, indent=4, simplejson.dumps(
separators=(',', ': ') self.__data, sort_keys=True, indent=4, separators=(",", ": ")
)) )
)
def _is_valid_key(self, key): def _is_valid_key(self, key):
return key in DEFAULTS or self.is_database_key(key) return key in DEFAULTS or self.is_database_key(key)
def is_database_key(self, key): def is_database_key(self, key):
return key.startswith('database/') return key.startswith("database/")
def __str__(self): def __str__(self):
return simplejson.dumps( return simplejson.dumps(
self.__data, sort_keys=True, self.__data, sort_keys=True, indent=4, separators=(",", ": ")
indent=4, separators=(',', ': ')
) )
def as_dict(self): def as_dict(self):
return copy.copy(self.__data) return copy.copy(self.__data)
def __getattr__(self, key): def __getattr__(self, key):
return self.__data[key] return self.__data[key]
...@@ -188,6 +176,7 @@ def config(ctx): ...@@ -188,6 +176,7 @@ def config(ctx):
"""The manager for beat cmdline configuration.""" """The manager for beat cmdline configuration."""
pass pass
@config.command() @config.command()
@click.pass_context @click.pass_context
def show(ctx): def show(ctx):
...@@ -195,11 +184,11 @@ def show(ctx): ...@@ -195,11 +184,11 @@ def show(ctx):
Lists the configuration after resolving defaults and saved variables Lists the configuration after resolving defaults and saved variables
""" """
click.echo(ctx.meta['config']) click.echo(ctx.meta["config"])
@config.command() @config.command()
@click.argument('key') @click.argument("key")
@click.pass_context @click.pass_context
def get(ctx, key): def get(ctx, key):
"""Prints out the contents of a single field. """Prints out the contents of a single field.
...@@ -221,19 +210,22 @@ def get(ctx, key): ...@@ -221,19 +210,22 @@ def get(ctx, key):
----- -----
* If the key is not found. * If the key is not found.
""" """
value = getattr(ctx.meta['config'], key) value = getattr(ctx.meta["config"], key)
if value is None: if value is None:
# Exit the command line with ClickException in case of errors. # Exit the command line with ClickException in case of errors.
raise click.ClickException( raise click.ClickException("The requested key `{}' does not exist".format(key))
"The requested key `{}' does not exist".format(key))
click.echo(value) click.echo(value)
@config.command() @config.command()
@click.argument('args', nargs=-1) @click.argument("args", nargs=-1)
@click.option('--local/--not-local', default=False, help='Save values on the ' @click.option(
'local configuration file (.beatrc) instead of using the global ' "--local/--not-local",
'file (~/.beatrc)') default=False,
help="Save values on the "
"local configuration file (.beatrc) instead of using the global "
"file (~/.beatrc)",
)
@click.pass_context @click.pass_context
def set(ctx, args, local): def set(ctx, args, local):
"""Sets the value for a key. """Sets the value for a key.
...@@ -260,10 +252,10 @@ def set(ctx, args, local): ...@@ -260,10 +252,10 @@ def set(ctx, args, local):
""" """
if len(args) % 2 != 0: if len(args) % 2 != 0:
raise click.BadParameter('You must provide pair(s) of key/value') raise click.BadParameter("You must provide pair(s) of key/value")
try: try:
for idx in range(0, len(args), 2): for idx in range(0, len(args), 2):
ctx.meta['config'].set(args[idx], args[idx + 1], local) ctx.meta["config"].set(args[idx], args[idx + 1], local)
except Exception: except Exception:
raise click.ClickException("Failed to change the configuration.") raise click.ClickException("Failed to change the configuration.")
...@@ -34,31 +34,30 @@ ...@@ -34,31 +34,30 @@
################################################################################### ###################################################################################
import os
import click
import glob import glob
import random
import zmq
import logging import logging
import os
import random
import click
import simplejson import simplejson
import zmq
from beat.core.hash import toPath
from beat.core.hash import hashDataset
from beat.core.utils import NumpyJSONEncoder
from beat.core.database import Database
from beat.core.data import RemoteDataSource
from beat.core import dock from beat.core import dock
from beat.core import inputs from beat.core import inputs
from beat.core import utils from beat.core import utils
from beat.core.data import RemoteDataSource
from beat.core.database import Database
from beat.core.hash import hashDataset
from beat.core.hash import toPath
from beat.core.utils import NumpyJSONEncoder
from . import common
from . import commands from . import commands
from . import common
from .decorators import raise_on_error
from .click_helper import AliasedGroup from .click_helper import AliasedGroup
from .click_helper import AssetCommand from .click_helper import AssetCommand
from .click_helper import AssetInfo from .click_helper import AssetInfo
from .decorators import raise_on_error
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
......
...@@ -35,17 +35,17 @@ ...@@ -35,17 +35,17 @@
import logging import logging
import click import click
from beat.core import dataformat from beat.core import dataformat
from . import common
from . import commands from . import commands
from . import common
from .decorators import raise_on_error
from .click_helper import AliasedGroup from .click_helper import AliasedGroup
from .click_helper import AssetCommand from .click_helper import AssetCommand
from .click_helper import AssetInfo from .click_helper import AssetInfo
from .decorators import raise_on_error
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
......
...@@ -34,9 +34,11 @@ ...@@ -34,9 +34,11 @@
import logging import logging
import click
from functools import wraps from functools import wraps
import click
from .log import set_verbosity_level from .log import set_verbosity_level
# This needs to be beat so that logger is configured for all beat packages. # This needs to be beat so that logger is configured for all beat packages.
......
...@@ -34,17 +34,16 @@ ...@@ -34,17 +34,16 @@
################################################################################### ###################################################################################
import click
import logging import logging