Skip to content

Commit e57527d

Browse files
authored
Merge pull request #64 from bcdev/forman-57-cli_with_traceback
Added CLI option `--traceback`
2 parents 3a46dd0 + 292d7cb commit e57527d

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Version 0.5.0 (in development)
22

3+
### Enhancements
4+
5+
* Added CLI option `--traceback`. [#57]
6+
7+
38
## Version 0.4.1 (from 2024-02-13)
49

510
### Fixes

docs/cli.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Usage: zappend [OPTIONS] [SLICES]...
99
Create or update a Zarr datacube TARGET from slice datasets SLICES.
1010
1111
The zappend command concatenates the dataset SLICES along a given append
12-
dimension, e.g., `"time"` (the default) for geospatial satellite
13-
observations. Each append step is atomic, that is, the append operation is a
14-
transaction that can be rolled back, in case the append operation fails.
15-
This ensures integrity of the target data cube given by TARGET or in CONFIG.
12+
dimension, e.g., "time" (the default) for geospatial satellite observations.
13+
Each append step is atomic, that is, the append operation is a transaction
14+
that can be rolled back, in case the append operation fails. This ensures
15+
integrity of the target data cube given by TARGET or in CONFIG.
1616
1717
Options:
1818
-c, --config CONFIG Configuration JSON or YAML file. If multiple are
@@ -22,6 +22,7 @@ Options:
2222
'target_dir' configuration field.
2323
--dry-run Run the tool without creating, changing, or deleting
2424
any files.
25+
--traceback Show Python traceback on error.
2526
--version Show version and exit.
2627
--help-config json|md Show configuration help and exit.
2728
--help Show this message and exit.

tests/test_cli.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,25 @@ def test_some_slices_and_no_target(self):
112112
"Error: Missing 'target_dir' in configuration\n", result.output
113113
)
114114
self.assertFalse(FileObj("memory://target.zarr").exists())
115+
116+
def test_some_slices_and_no_target_with_traceback(self):
117+
make_test_dataset(uri="memory://slice-1.zarr")
118+
make_test_dataset(uri="memory://slice-2.zarr")
119+
make_test_dataset(uri="memory://slice-3.zarr")
120+
121+
runner = CliRunner()
122+
# noinspection PyTypeChecker
123+
result = runner.invoke(
124+
zappend,
125+
[
126+
"--traceback",
127+
"memory://slice-1.zarr",
128+
"memory://slice-2.zarr",
129+
"memory://slice-3.zarr",
130+
],
131+
)
132+
print(result.output)
133+
self.assertEqual(1, result.exit_code)
134+
self.assertIn("Traceback (most recent call last):\n", result.output)
135+
self.assertIn("Error: Missing 'target_dir' in configuration\n", result.output)
136+
self.assertFalse(FileObj("memory://target.zarr").exists())

zappend/cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright © 2024 Norman Fomferra
22
# Permissions are hereby granted under the terms of the MIT License:
33
# https://opensource.org/licenses/MIT.
4+
import sys
45

56
import click
67

@@ -28,6 +29,11 @@
2829
is_flag=True,
2930
help="Run the tool without creating, changing," " or deleting any files.",
3031
)
32+
@click.option(
33+
"--traceback",
34+
is_flag=True,
35+
help="Show Python traceback on error.",
36+
)
3137
@click.option(
3238
"--version",
3339
is_flag=True,
@@ -44,6 +50,7 @@ def zappend(
4450
config: tuple[str, ...],
4551
target: str | None,
4652
dry_run: bool,
53+
traceback: bool,
4754
version: bool,
4855
help_config: str | None,
4956
):
@@ -73,6 +80,10 @@ def zappend(
7380
try:
7481
zappend(slices, config=config, target_dir=target, dry_run=dry_run)
7582
except BaseException as e:
83+
if traceback:
84+
import traceback as tb
85+
86+
tb.print_exc(file=sys.stderr)
7687
raise click.ClickException(f"{e}") from e
7788

7889

0 commit comments

Comments
 (0)