Skip to content

Commit

Permalink
CLI: remove VERBOSE option and replace with VERBOSITY
Browse files Browse the repository at this point in the history
The `VERBOSE` option is obsolete since all commands now automatically
have the `VERBOSITY` option exposed. The `VERBOSE` option was mostly
used to configure a logger (most notably the `DELETE_LOGGER`) but this
is no longer necessary. Since those loggers are children of the main
`AIIDA_LOGGER`, whose level is controlled through the `VERBOSITY`
option, the child logger will inherit its value.

Some commands used the `VERBOSE` option more as a boolean switch. To
reproduce this behavior, a utiltiy function `is_verbose` is added to the
`cmdline` module, which will return `True` if the loglevel of the
`CMDLINE_LOGGER` is equal or smaller than the `INFO` level and `False`
otherwise.

The `verdi archive` commands used an ad-hoc implementation of the
verbosity option. This has been removed and they now also rely on the
logging level that is set on the `AIIDA_LOGGER` through the global
`VERBOSITY` option.
  • Loading branch information
sphuber committed Aug 24, 2021
1 parent 2b9725d commit ec81d0b
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 135 deletions.
14 changes: 7 additions & 7 deletions aiida/backends/sqlalchemy/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Simple wrapper around the alembic command line tool that first loads an AiiDA profile."""

import alembic
import click

from aiida.cmdline import is_verbose
from aiida.cmdline.params import options


Expand Down Expand Up @@ -51,18 +51,18 @@ def alembic_revision(message):


@alembic_cli.command('current')
@options.VERBOSE()
def alembic_current(verbose):
@options.VERBOSITY()
def alembic_current():
"""Show the current revision."""
execute_alembic_command('current', verbose=verbose)
execute_alembic_command('current', verbose=is_verbose())


@alembic_cli.command('history')
@click.option('-r', '--rev-range')
@options.VERBOSE()
def alembic_history(rev_range, verbose):
@options.VERBOSITY()
def alembic_history(rev_range):
"""Show the history for the given revision range."""
execute_alembic_command('history', rev_range=rev_range, verbose=verbose)
execute_alembic_command('history', rev_range=rev_range, verbose=is_verbose())


@alembic_cli.command('upgrade')
Expand Down
1 change: 1 addition & 0 deletions aiida/cmdline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'WorkflowParamType',
'dbenv',
'format_call_graph',
'is_verbose',
'only_if_daemon_running',
'with_dbenv',
)
Expand Down
56 changes: 15 additions & 41 deletions aiida/cmdline/commands/cmd_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# pylint: disable=too-many-arguments,import-error,too-many-locals,broad-except
"""`verdi archive` command."""
from enum import Enum
import logging
from typing import List, Tuple
import traceback
import urllib.request
Expand All @@ -22,6 +23,7 @@
from aiida.cmdline.params.types import GroupParamType, PathOrUrl
from aiida.cmdline.utils import decorators, echo
from aiida.common.links import GraphTraversalRules
from aiida.common.log import AIIDA_LOGGER

EXTRAS_MODE_EXISTING = ['keep_existing', 'update_existing', 'mirror', 'none', 'ask']
EXTRAS_MODE_NEW = ['import', 'none']
Expand Down Expand Up @@ -82,13 +84,6 @@ def inspect(archive, version, meta_data):
type=click.Choice(['zip', 'zip-uncompressed', 'zip-lowmemory', 'tar.gz', 'null']),
)
@options.FORCE(help='Overwrite output file if it already exists.')
@click.option(
'-v',
'--verbosity',
default='INFO',
type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'CRITICAL']),
help='Control the verbosity of console logging'
)
@options.graph_traversal_rules(GraphTraversalRules.EXPORT.value)
@click.option(
'--include-logs/--exclude-logs',
Expand All @@ -113,7 +108,7 @@ def inspect(archive, version, meta_data):
@decorators.with_dbenv()
def create(
output_file, codes, computers, groups, nodes, archive_format, force, input_calc_forward, input_work_forward,
create_backward, return_backward, call_calc_backward, call_work_backward, include_comments, include_logs, verbosity
create_backward, return_backward, call_calc_backward, call_work_backward, include_comments, include_logs
):
"""
Export subsets of the provenance graph to file for sharing.
Expand All @@ -127,7 +122,7 @@ def create(
# pylint: disable=too-many-branches
from aiida.common.log import override_log_formatter_context
from aiida.common.progress_reporter import set_progress_bar_tqdm, set_progress_reporter
from aiida.tools.importexport import export, ExportFileFormat, EXPORT_LOGGER
from aiida.tools.importexport import export, ExportFileFormat
from aiida.tools.importexport.common.exceptions import ArchiveExportError

entities = []
Expand Down Expand Up @@ -170,11 +165,10 @@ def create(
elif archive_format == 'null':
export_format = 'null'

if verbosity in ['DEBUG', 'INFO']:
set_progress_bar_tqdm(leave=(verbosity == 'DEBUG'))
if AIIDA_LOGGER.level <= logging.INFO:
set_progress_bar_tqdm(leave=(AIIDA_LOGGER.level == logging.DEBUG))
else:
set_progress_reporter(None)
EXPORT_LOGGER.setLevel(verbosity)

try:
with override_log_formatter_context('%(message)s'):
Expand Down Expand Up @@ -202,18 +196,12 @@ def create(
# version inside the function when needed.
help='Archive format version to migrate to (defaults to latest version).',
)
@click.option(
'--verbosity',
default='INFO',
type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'CRITICAL']),
help='Control the verbosity of console logging'
)
def migrate(input_file, output_file, force, in_place, archive_format, version, verbosity):
def migrate(input_file, output_file, force, in_place, archive_format, version):
"""Migrate an export archive to a more recent format version."""
from aiida.common.log import override_log_formatter_context
from aiida.common.progress_reporter import set_progress_bar_tqdm, set_progress_reporter
from aiida.tools.importexport import detect_archive_type, EXPORT_VERSION
from aiida.tools.importexport.archive.migrators import get_migrator, MIGRATE_LOGGER
from aiida.tools.importexport.archive.migrators import get_migrator

if in_place:
if output_file:
Expand All @@ -225,11 +213,10 @@ def migrate(input_file, output_file, force, in_place, archive_format, version, v
'no output file specified. Please add --in-place flag if you would like to migrate in place.'
)

if verbosity in ['DEBUG', 'INFO']:
set_progress_bar_tqdm(leave=(verbosity == 'DEBUG'))
if AIIDA_LOGGER.level <= logging.INFO:
set_progress_bar_tqdm(leave=(AIIDA_LOGGER.level == logging.DEBUG))
else:
set_progress_reporter(None)
MIGRATE_LOGGER.setLevel(verbosity)

if version is None:
version = EXPORT_VERSION
Expand All @@ -241,15 +228,14 @@ def migrate(input_file, output_file, force, in_place, archive_format, version, v
with override_log_formatter_context('%(message)s'):
migrator.migrate(version, output_file, force=force, out_compression=archive_format)
except Exception as error: # pylint: disable=broad-except
if verbosity == 'DEBUG':
if AIIDA_LOGGER.level <= logging.DEBUG:
raise
echo.echo_critical(
'failed to migrate the archive file (use `--verbosity DEBUG` to see traceback): '
f'{error.__class__.__name__}:{error}'
)

if verbosity in ['DEBUG', 'INFO']:
echo.echo_success(f'migrated the archive to version {version}')
echo.echo_success(f'migrated the archive to version {version}')


class ExtrasImportCode(Enum):
Expand Down Expand Up @@ -313,19 +299,11 @@ class ExtrasImportCode(Enum):
show_default=True,
help='Force migration of archive file archives, if needed.'
)
@click.option(
'-v',
'--verbosity',
default='INFO',
type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'CRITICAL']),
help='Control the verbosity of console logging'
)
@options.NON_INTERACTIVE()
@decorators.with_dbenv()
@click.pass_context
def import_archive(
ctx, archives, webpages, group, extras_mode_existing, extras_mode_new, comment_mode, migration, non_interactive,
verbosity
ctx, archives, webpages, group, extras_mode_existing, extras_mode_new, comment_mode, migration, non_interactive
):
"""Import data from an AiiDA archive file.
Expand All @@ -334,15 +312,11 @@ def import_archive(
# pylint: disable=unused-argument
from aiida.common.log import override_log_formatter_context
from aiida.common.progress_reporter import set_progress_bar_tqdm, set_progress_reporter
from aiida.tools.importexport.dbimport.utils import IMPORT_LOGGER
from aiida.tools.importexport.archive.migrators import MIGRATE_LOGGER

if verbosity in ['DEBUG', 'INFO']:
set_progress_bar_tqdm(leave=(verbosity == 'DEBUG'))
if AIIDA_LOGGER.level <= logging.INFO:
set_progress_bar_tqdm(leave=(AIIDA_LOGGER.level == logging.DEBUG))
else:
set_progress_reporter(None)
IMPORT_LOGGER.setLevel(verbosity)
MIGRATE_LOGGER.setLevel(verbosity)

all_archives = _gather_imports(archives, webpages)

Expand Down
15 changes: 5 additions & 10 deletions aiida/cmdline/commands/cmd_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
###########################################################################
"""`verdi code` command."""
from functools import partial
import logging

import click
import tabulate
Expand Down Expand Up @@ -144,11 +143,11 @@ def code_duplicate(ctx, code, non_interactive, **kwargs):

@verdi_code.command()
@arguments.CODE()
@options.VERBOSE()
@with_dbenv()
def show(code, verbose):
def show(code):
"""Display detailed information for a code."""
from aiida.repository import FileType
from aiida.cmdline import is_verbose

table = []
table.append(['PK', code.pk])
Expand All @@ -174,28 +173,24 @@ def show(code, verbose):
table.append(['Prepend text', code.get_prepend_text()])
table.append(['Append text', code.get_append_text()])

if verbose:
if is_verbose():
table.append(['Calculations', len(code.get_outgoing().all())])

echo.echo(tabulate.tabulate(table))


@verdi_code.command()
@arguments.CODES()
@options.VERBOSE()
@options.DRY_RUN()
@options.FORCE()
@with_dbenv()
def delete(codes, verbose, dry_run, force):
def delete(codes, dry_run, force):
"""Delete a code.
Note that codes are part of the data provenance, and deleting a code will delete all calculations using it.
"""
from aiida.common.log import override_log_formatter_context
from aiida.tools import delete_nodes, DELETE_LOGGER

verbosity = logging.DEBUG if verbose else logging.INFO
DELETE_LOGGER.setLevel(verbosity)
from aiida.tools import delete_nodes

node_pks_to_delete = [code.pk for code in codes]

Expand Down
12 changes: 6 additions & 6 deletions aiida/cmdline/commands/cmd_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,28 @@ def detect_invalid_nodes():


@verdi_database.command('summary')
@options.VERBOSE()
def database_summary(verbose):
def database_summary():
"""Summarise the entities in the database."""
from aiida.cmdline import is_verbose
from aiida.orm import QueryBuilder, Node, Group, Computer, Comment, Log, User
data = {}

# User
query_user = QueryBuilder().append(User, project=['email'])
data['Users'] = {'count': query_user.count()}
if verbose:
if is_verbose():
data['Users']['emails'] = query_user.distinct().all(flat=True)

# Computer
query_comp = QueryBuilder().append(Computer, project=['label'])
data['Computers'] = {'count': query_comp.count()}
if verbose:
if is_verbose():
data['Computers']['labels'] = query_comp.distinct().all(flat=True)

# Node
count = QueryBuilder().append(Node).count()
data['Nodes'] = {'count': count}
if verbose:
if is_verbose():
node_types = QueryBuilder().append(Node, project=['node_type']).distinct().all(flat=True)
data['Nodes']['node_types'] = node_types
process_types = QueryBuilder().append(Node, project=['process_type']).distinct().all(flat=True)
Expand All @@ -227,7 +227,7 @@ def database_summary(verbose):
# Group
query_group = QueryBuilder().append(Group, project=['type_string'])
data['Groups'] = {'count': query_group.count()}
if verbose:
if is_verbose():
data['Groups']['type_strings'] = query_group.distinct().all(flat=True)

# Comment
Expand Down
7 changes: 2 additions & 5 deletions aiida/cmdline/commands/cmd_devel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import sys

from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.cmdline.params import options
from aiida.cmdline.utils import decorators, echo


Expand All @@ -21,8 +20,7 @@ def verdi_devel():


@verdi_devel.command('check-load-time')
@options.VERBOSE()
def devel_check_load_time(verbose):
def devel_check_load_time():
"""Check for common indicators that slowdown `verdi`.
Check for environment properties that negatively affect the responsiveness of the `verdi` command line interface.
Expand All @@ -37,8 +35,7 @@ def devel_check_load_time(verbose):

loaded_aiida_modules = [key for key in sys.modules if key.startswith('aiida.')]
aiida_modules_str = '\n- '.join(sorted(loaded_aiida_modules))
if verbose:
echo.echo(f'aiida modules loaded:\n- {aiida_modules_str}')
echo.echo_info(f'aiida modules loaded:\n- {aiida_modules_str}')

manager = get_manager()

Expand Down
9 changes: 2 additions & 7 deletions aiida/cmdline/commands/cmd_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""`verdi group` commands"""
import logging
import click

from aiida.common.exceptions import UniquenessError
Expand Down Expand Up @@ -93,17 +92,13 @@ def group_remove_nodes(group, nodes, clear, force):
)
@options.graph_traversal_rules(GraphTraversalRules.DELETE.value)
@options.DRY_RUN()
@options.VERBOSE()
@with_dbenv()
def group_delete(group, delete_nodes, dry_run, force, verbose, **traversal_rules):
def group_delete(group, delete_nodes, dry_run, force, **traversal_rules):
"""Delete a group and (optionally) the nodes it contains."""
from aiida.common.log import override_log_formatter_context
from aiida.tools import delete_group_nodes, DELETE_LOGGER
from aiida.tools import delete_group_nodes
from aiida import orm

verbosity = logging.DEBUG if verbose else logging.INFO
DELETE_LOGGER.setLevel(verbosity)

if not (force or dry_run):
click.confirm(f'Are you sure you want to delete {group}?', abort=True)
elif dry_run:
Expand Down
9 changes: 2 additions & 7 deletions aiida/cmdline/commands/cmd_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""`verdi node` command."""
import logging
import shutil
import pathlib

Expand Down Expand Up @@ -280,12 +279,11 @@ def extras(nodes, keys, fmt, identifier, raw):

@verdi_node.command('delete')
@click.argument('identifier', nargs=-1, metavar='NODES')
@options.VERBOSE()
@options.DRY_RUN()
@options.FORCE()
@options.graph_traversal_rules(GraphTraversalRules.DELETE.value)
@with_dbenv()
def node_delete(identifier, dry_run, verbose, force, **traversal_rules):
def node_delete(identifier, dry_run, force, **traversal_rules):
"""Delete nodes from the provenance graph.
This will not only delete the nodes explicitly provided via the command line, but will also include
Expand All @@ -294,10 +292,7 @@ def node_delete(identifier, dry_run, verbose, force, **traversal_rules):
"""
from aiida.common.log import override_log_formatter_context
from aiida.orm.utils.loaders import NodeEntityLoader
from aiida.tools import delete_nodes, DELETE_LOGGER

verbosity = logging.DEBUG if verbose else logging.INFO
DELETE_LOGGER.setLevel(verbosity)
from aiida.tools import delete_nodes

pks = []

Expand Down
1 change: 0 additions & 1 deletion aiida/cmdline/params/options/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
'USER_FIRST_NAME',
'USER_INSTITUTION',
'USER_LAST_NAME',
'VERBOSE',
'VERBOSITY',
'VISUALIZATION_FORMAT',
'WAIT',
Expand Down
Loading

0 comments on commit ec81d0b

Please sign in to comment.