1 | # Licensed under a 3-clause BSD style license - see LICENSE.rst |
||
2 | from __future__ import absolute_import, division, print_function, unicode_literals |
||
3 | import warnings |
||
4 | import logging |
||
5 | import click |
||
6 | import sys |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
7 | from .. import version |
||
8 | from ..extern.pathlib import Path |
||
9 | |||
10 | |||
11 | # We implement the --version following the example from here: |
||
12 | # http://click.pocoo.org/5/options/#callbacks-and-eager-options |
||
13 | def print_version(ctx, param, value): |
||
14 | if not value or ctx.resilient_parsing: |
||
15 | return |
||
16 | print("gammapy version {}".format(version.version)) |
||
17 | ctx.exit() |
||
18 | |||
19 | |||
20 | # http://click.pocoo.org/5/documentation/#help-parameter-customization |
||
21 | CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) |
||
22 | |||
23 | # https://click.palletsprojects.com/en/5.x/python3/#unicode-literals |
||
24 | click.disable_unicode_literals_warning = True |
||
25 | |||
26 | |||
27 | @click.group("gammapy", context_settings=CONTEXT_SETTINGS) |
||
28 | @click.option( |
||
29 | "--log-level", |
||
30 | default="info", |
||
31 | help="Logging verbosity level.", |
||
32 | type=click.Choice(["debug", "info", "warning", "error"]), |
||
33 | ) |
||
34 | @click.option("--ignore-warnings", is_flag=True, help="Ignore warnings?") |
||
35 | @click.option( |
||
36 | "--version", |
||
37 | is_flag=True, |
||
38 | callback=print_version, |
||
39 | expose_value=False, |
||
40 | is_eager=True, |
||
41 | help="Print version and exit.", |
||
42 | ) |
||
43 | def cli(log_level, ignore_warnings): |
||
44 | """Gammapy command line interface (CLI). |
||
45 | |||
46 | Gammapy is a Python package for gamma-ray astronomy. |
||
47 | |||
48 | Use ``--help`` to see available sub-commands, as well as the available |
||
49 | arguments and options for each sub-command. |
||
50 | |||
51 | For further information, see https://gammapy.org/ and https://docs.gammapy.org/ |
||
52 | |||
53 | \b |
||
54 | Examples |
||
55 | -------- |
||
56 | |||
57 | \b |
||
58 | $ gammapy --help |
||
59 | $ gammapy --version |
||
60 | $ gammapy info --help |
||
61 | $ gammapy info |
||
62 | """ |
||
63 | logging.basicConfig(level=log_level.upper()) |
||
64 | |||
65 | if ignore_warnings: |
||
66 | warnings.simplefilter("ignore") |
||
67 | |||
68 | |||
69 | @cli.group("image") |
||
70 | def cli_image(): |
||
71 | """Analysis - 2D images""" |
||
72 | |||
73 | |||
74 | @cli.group("download", short_help="Download datasets and notebooks") |
||
75 | def cli_download(): |
||
76 | """Download datasets and notebooks. |
||
77 | |||
78 | Download notebooks published as tutorials and the related datasets needed |
||
79 | to execute them. It is also possible to download individual notebooks |
||
80 | or datasets. The option `tutorials` will download by default the versioned |
||
81 | file-structure of the tutorials into a `gammapy-tutorials` folder created |
||
82 | at the current working directory. The option `datasets` will download by |
||
83 | default the datasets used by Gammapy into a `gammapy-datasets` folder |
||
84 | created at the current working directory. The option `notebooks` will |
||
85 | download by default the jupyter notebook files used in the tutorilas into |
||
86 | a `gammapy-notebooks` folder created at the current working directory. |
||
87 | |||
88 | \b |
||
89 | Examples |
||
90 | -------- |
||
91 | |||
92 | \b |
||
93 | $ gammapy download notebooks |
||
94 | $ gammapy download datasets |
||
95 | $ gammapy download tutorials --release 0.8 |
||
96 | $ gammapy download notebooks --src first_steps |
||
97 | $ gammapy download datasets --src fermi_3fhl --out localfolder/ |
||
98 | """ |
||
99 | |||
100 | |||
101 | @cli.group("jupyter", short_help="Perform actions on notebooks") |
||
102 | @click.option("--src", default=".", help="Local folder or Jupyter notebook filename.") |
||
103 | @click.pass_context |
||
104 | def cli_jupyter(ctx, src): |
||
105 | """ |
||
106 | Perform a series of actions on Jupyter notebooks. |
||
107 | |||
108 | The chosen action is applied by default for every Jupyter notebook present |
||
109 | in the current working directory. |
||
110 | |||
111 | \b |
||
112 | Examples |
||
113 | -------- |
||
114 | \b |
||
115 | $ gammapy jupyter strip |
||
116 | $ gammapy jupyter --src mynotebooks.ipynb run |
||
117 | $ gammapy jupyter --src myfolder/tutorials test |
||
118 | $ gammapy jupyter black |
||
119 | """ |
||
120 | log = logging.getLogger(__name__) |
||
121 | |||
122 | path = Path(src) |
||
123 | if not path.exists(): |
||
124 | log.error("File or folder {} not found.".format(src)) |
||
0 ignored issues
–
show
|
|||
125 | sys.exit() |
||
126 | |||
127 | if path.is_dir(): |
||
128 | paths = list(path.glob("*.ipynb")) |
||
129 | else: |
||
130 | paths = [path] |
||
131 | |||
132 | ctx.obj = {"paths": paths, "pathsrc": path} |
||
133 | |||
134 | |||
135 | def add_subcommands(): |
||
136 | from .info import cli_info |
||
137 | |||
138 | cli.add_command(cli_info) |
||
139 | |||
140 | from .check import cli_check |
||
141 | |||
142 | cli.add_command(cli_check) |
||
143 | |||
144 | from .image_bin import cli_image_bin |
||
145 | |||
146 | cli_image.add_command(cli_image_bin) |
||
147 | |||
148 | from .download import cli_download_notebooks |
||
149 | |||
150 | cli_download.add_command(cli_download_notebooks) |
||
151 | |||
152 | from .download import cli_download_datasets |
||
153 | |||
154 | cli_download.add_command(cli_download_datasets) |
||
155 | |||
156 | from .download import cli_download_tutorials |
||
157 | |||
158 | cli_download.add_command(cli_download_tutorials) |
||
159 | |||
160 | from .jupyter import cli_jupyter_black |
||
161 | |||
162 | cli_jupyter.add_command(cli_jupyter_black) |
||
163 | |||
164 | from .jupyter import cli_jupyter_strip |
||
165 | |||
166 | cli_jupyter.add_command(cli_jupyter_strip) |
||
167 | |||
168 | from .jupyter import cli_jupyter_run |
||
169 | |||
170 | cli_jupyter.add_command(cli_jupyter_run) |
||
171 | |||
172 | from .jupyter import cli_jupyter_test |
||
173 | |||
174 | cli_jupyter.add_command(cli_jupyter_test) |
||
175 | |||
176 | |||
177 | add_subcommands() |
||
178 |