Skip to content

Commit be031c9

Browse files
authored
Merge pull request #13 from bcdev/forman-print_config_option
CLI `--print-config` option
2 parents 8845c39 + 1256cbf commit be031c9

30 files changed

+2063
-883
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# xrlint
22
/xrlint_config.*
3+
/notebooks/xrlint_config.*
34

45
# Logs
56
*.log

CHANGES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
## Version 0.1.0 (in development)
44

5+
- Added CLI option `--print-config PATH`, see same option in ESLint
56
- XRLint CLI now outputs single results immediately to console,
67
instead only after all results have been collected.
7-
8+
- Refactored and renamed `CliEngine` into `XRLint`. Documented the class.
9+
- `new_linter()` now uses a config name arg instead of a bool arg.
10+
- Split example notebook into two
811

912
## Early development snapshots
1013

docs/api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All described objects can be imported from the `xrlint.all` module.
44

5+
## Class `XRLint`
6+
7+
::: xrlint.cli.engine.XRLint
8+
59
## Function `new_linter()`
610

711
::: xrlint.linter.new_linter

docs/cli.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,33 @@ Usage: xrlint [OPTIONS] [FILES]...
88
99
Validate the given dataset FILES.
1010
11-
Reads configuration from `xrlint.config.*` if file exists and unless `--no-
12-
default-config` is set or `--config PATH` is provided. Then validates each
13-
dataset in FILES against the configuration. The validation result is dumped
14-
to standard output if not otherwise stated by `--output-file PATH`. The
15-
output format is `simple`. Other inbuilt formats are `json` and `html` which
16-
can by setting the `--format NAME` option.
11+
Reads configuration from `./xrlint_config.*` if such file exists and unless
12+
`--no_config_lookup` is set or `--config` is provided. Then validates each
13+
dataset in FILES against the configuration. The default dataset patters are
14+
`**/*.zarr` and `**/.nc`. FILES may comprise also directories. If a
15+
directory is not matched by any file pattern, it will be traversed
16+
recursively. The validation result is dumped to standard output if not
17+
otherwise stated by `--output-file`. The output format is `simple` by
18+
default. Other inbuilt formats are `json` and `html` which you can specify
19+
using the `--format` option.
1720
1821
Options:
19-
--no-default-config Disable use of default configuration from
22+
--no-config-lookup Disable use of default configuration from
2023
xrlint_config.*
21-
-c, --config PATH Use this configuration, overriding xrlint_config.*
24+
-c, --config FILE Use this configuration, overriding xrlint_config.*
2225
config options if present
26+
--print-config FILE Print the configuration for the given file
2327
--plugin MODULE Specify plugins. MODULE is the name of Python module
2428
that defines an 'export_plugin()' function.
2529
--rule SPEC Specify rules. SPEC must have format '<rule-name>:
2630
<rule-config>' (note the space character).
27-
-o, --output-file PATH Specify file to write report to
31+
-o, --output-file FILE Specify file to write report to
2832
-f, --format NAME Use a specific output format - default: simple
2933
--color / --no-color Force enabling/disabling of color
3034
--max-warnings COUNT Number of warnings to trigger nonzero exit code -
3135
default: 5
3236
--init Write initial configuration file and exit.
3337
--version Show the version and exit.
3438
--help Show this message and exit.
39+
3540
```

docs/start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,6 @@ import xrlint.all as xrl
105105
106106
test_ds = xr.Dataset(attrs={"title": "Test Dataset"})
107107
108-
linter = xrl.new_linter(recommended=True)
108+
linter = xrl.new_linter("recommended")
109109
linter.verify_dataset(test_ds)
110110
```

docs/todo.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@
1313
- project logo
1414
- if configuration for given FILE is empty,
1515
report an error, see TODO in CLI main tests
16-
- rename `xrlint.cli.CliEngine` into `xrlint.cli.XRLint`
17-
(with similar API as the `ESLint` class) and export it
18-
from `xrlint.all`. Value of `FILES` should be passed to
19-
`verify_datasets()` methods.
2016
- use `RuleMeta.docs_url` in formatters to create links
2117
- implement xarray backend for xcube 'levels' format
2218
so can validate them too
2319
- add some more tests so we reach 99% coverage
2420
- support rule op args/kwargs schema validation
25-
- support CLI option `--print-config FILE`, see ESLint
2621
- Support `RuleTest.expected`, it is currently unused
2722

2823
## Nice to have

notebooks/mkdataset.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import numpy as np
2+
import xarray as xr
3+
4+
nx = 2
5+
ny = 3
6+
nt = 4
7+
8+
9+
def make_dataset() -> xr.Dataset:
10+
"""Create a dataset that passes xrlint core rules."""
11+
12+
return xr.Dataset(
13+
attrs=dict(title="SST-Climatology Subset"),
14+
coords={
15+
"x": xr.DataArray(
16+
np.linspace(-180, 180, nx),
17+
dims="x",
18+
attrs={"units": "degrees"}
19+
),
20+
"y": xr.DataArray(
21+
np.linspace(-90, 90, ny),
22+
dims="y",
23+
attrs={"units": "degrees"}
24+
),
25+
"time": xr.DataArray(
26+
[2010 + y for y in range(nt)],
27+
dims="time",
28+
attrs={"units": "years"}
29+
),
30+
"spatial_ref": xr.DataArray(
31+
0,
32+
attrs={
33+
"grid_mapping_name": "latitude_longitude",
34+
"semi_major_axis": 6371000.0,
35+
"inverse_flattening": 0,
36+
},
37+
),
38+
},
39+
data_vars={
40+
"sst": xr.DataArray(
41+
np.random.random((nt, ny, nx)),
42+
dims=["time", "y", "x"],
43+
attrs={"units": "kelvin", "grid_mapping": "spatial_ref"}
44+
),
45+
"sst_anomaly": xr.DataArray(
46+
np.random.random((nt, ny, nx)),
47+
dims=["time", "y", "x"],
48+
attrs={"units": "kelvin", "grid_mapping": "spatial_ref"}
49+
)
50+
},
51+
)
52+
53+
54+
def make_dataset_with_issues() -> xr.Dataset:
55+
"""Create a dataset that produces issues with xrlint core rules."""
56+
invalid_ds = make_dataset()
57+
invalid_ds.attrs = {}
58+
invalid_ds.sst.attrs["units"] = 1
59+
invalid_ds["sst_avg"] = xr.DataArray(
60+
np.random.random((nx, ny)),
61+
dims=["x", "y"],
62+
attrs={"units": "kelvin"}
63+
)
64+
return invalid_ds

0 commit comments

Comments
 (0)