Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use click for main, info and checks cli
- Loading branch information
Showing
9 changed files
with
136 additions
and
204 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
import sys | ||
"""Top-level script environment for Gammapy. | ||
This is what's executed when you run: | ||
from .scripts.cli import main | ||
python -m gammapy | ||
See https://docs.python.org/3/library/__main__.html | ||
""" | ||
import sys | ||
from .scripts.main import cli | ||
|
||
sys.exit(main()) | ||
sys.exit(cli()) |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
import warnings | ||
import logging | ||
import click | ||
from .. import version | ||
|
||
|
||
# We implement the --version following the example from here: | ||
# http://click.pocoo.org/5/options/#callbacks-and-eager-options | ||
def print_version(ctx, param, value): | ||
if not value or ctx.resilient_parsing: | ||
return | ||
print('gammapy version {}'.format(version.version)) | ||
ctx.exit() | ||
|
||
|
||
# http://click.pocoo.org/5/documentation/#help-parameter-customization | ||
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) | ||
|
||
|
||
@click.group(context_settings=CONTEXT_SETTINGS) | ||
@click.option('--log-level', default='info', help='Logging verbosity level', | ||
type=click.Choice(['debug', 'info', 'warning', 'error'])) | ||
@click.option('--ignore-warnings', is_flag=True, help='Ignore warnings?') | ||
@click.option('--version', is_flag=True, callback=print_version, | ||
expose_value=False, is_eager=True, help='Print version and exit') | ||
def cli(log_level, ignore_warnings): | ||
"""Gammapy command line interface. | ||
Gammapy is a Python package for gamma-ray astronomy. | ||
For further information, see http://gammapy.org/ | ||
""" | ||
logging.basicConfig(level=log_level.upper()) | ||
|
||
if ignore_warnings: | ||
warnings.simplefilter('ignore') | ||
|
||
|
||
from .info import cli_info | ||
cli.add_command(cli_info) | ||
|
||
from .check import cli_check | ||
cli.add_command(cli_check) | ||
|
||
if __name__ == '__main__': | ||
cli() |
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,12 +1,19 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
from click.testing import CliRunner | ||
from ..main import cli | ||
|
||
from ..cli import main | ||
|
||
def test_cli_info_help(): | ||
result = CliRunner().invoke(cli, ['info', '--help']) | ||
assert result.exit_code == 0 | ||
assert 'Usage' in result.output | ||
|
||
def test_cmd_info(capsys): | ||
main(['info', '--all']) | ||
out, err = capsys.readouterr() | ||
assert "Gammapy current install" in out | ||
assert "Gammapy dependencies" in out | ||
assert "Gammapy environment variables" in out | ||
|
||
def test_cli_info_no_args(): | ||
# No arguments should print all infos | ||
result = CliRunner().invoke(cli, ['info']) | ||
assert result.exit_code == 0 | ||
assert "Gammapy current install" in result.output | ||
assert "Gammapy dependencies" in result.output | ||
assert "Gammapy environment variables" in result.output |
Oops, something went wrong.