Skip to content

Commit 8c0f848

Browse files
authored
Merge pull request #61 from bcdev/forman-60-allow_hyphen_in_config_names
Allow hyphen in config names
2 parents 79f497e + 84ee85d commit 8c0f848

File tree

9 files changed

+89
-46
lines changed

9 files changed

+89
-46
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# xrlint
2+
/xrlint-config.*
23
/xrlint_config.*
4+
/notebooks/xrlint-config.*
35
/notebooks/xrlint_config.*
46

57
# Logs

CHANGES.md

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

33
## Version 0.5.1 (in development)
44

5+
- XRLint now also loads default configuration from files named
6+
`xrlint-config.yaml` and `xrlint-config.json`. (#60)
7+
8+
- The command `xrlint --init` now writes `xrlint-config.yaml`
9+
instead of `xrlint_config.yaml`.
10+
511
- Enhanced documentation by a new page that compiles
612
the code examples in the `examples` folder.
713

docs/cli.md

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
# Command Line Interface
22

33
After installation, the `xrlint` command can be used from the terminal.
4-
The following are the command's options and arguments:
4+
The following are the command's usage help including a short description
5+
of its options and arguments:
56

67
```
78
Usage: xrlint [OPTIONS] [FILES]...
89
910
Validate the given dataset FILES.
1011
11-
Reads configuration from './xrlint_config.*' if such file exists and unless
12-
'--no_config_lookup' is set or '--config' is provided. It then validates
13-
each dataset in FILES against the configuration. The default dataset patters
14-
are '**/*.zarr' and '**/.nc'. FILES may comprise also directories or URLs.
15-
The supported URL protocols are the ones supported by xarray. Using remote
16-
protocols may require installing additional packages such as S3Fs
17-
(https://s3fs.readthedocs.io/) for the 's3' protocol.
12+
When executed, XRLint does the following three things:
1813
19-
If a directory is provided that not matched by any file pattern, it will be
20-
traversed recursively. The validation result is dumped to standard output if
21-
not otherwise stated by '--output-file'. The output format is 'simple' by
22-
default. Other inbuilt formats are 'json' and 'html' which you can specify
23-
using the '--format' option.
14+
(1) Unless options '--no-config-lookup' or '--config' are used it searches
15+
for a default configuration file in the current working directory. Default
16+
configuration files are determined by their filename, namely
17+
'xrlint_config.py' or 'xrlint-config.<format>', where <format> refers to the
18+
filename extensions 'json', 'yaml', and 'yml'. A Python configuration file
19+
('*.py'), is expected to provide XRLInt configuration from a function
20+
'export_config()', which may include custom plugins and rules.
21+
22+
(2) It then validates each dataset in FILES against the configuration. The
23+
default dataset patters are '**/*.zarr' and '**/.nc'. FILES may comprise
24+
also directories or URLs. The supported URL protocols are the ones supported
25+
by xarray. Using remote protocols may require installing additional packages
26+
such as S3Fs (https://s3fs.readthedocs.io/) for the 's3' protocol. If a
27+
directory is provided that not matched by any file pattern, it will be
28+
traversed recursively.
29+
30+
(3) The validation result is dumped to standard output if not otherwise
31+
stated by '--output-file'. The output format is 'simple' by default. Other
32+
inbuilt formats are 'json' and 'html' which you can specify using the '--
33+
format' option.
34+
35+
Please refer to the documentation (https://bcdev.github.io/xrlint/) for more
36+
information.
2437
2538
Options:
26-
--no-config-lookup Disable use of default configuration from
27-
xrlint_config.*
28-
-c, --config FILE Use this configuration, overriding xrlint_config.*
29-
config options if present
39+
--no-config-lookup Disable use of default configuration files
40+
-c, --config FILE Use this configuration instead of looking for a
41+
default configuration file
3042
--print-config FILE Print the configuration for the given file
3143
--plugin MODULE Specify plugins. MODULE is the name of Python module
3244
that defines an 'export_plugin()' function.
@@ -37,7 +49,9 @@ Options:
3749
--color / --no-color Force enabling/disabling of color
3850
--max-warnings COUNT Number of warnings to trigger nonzero exit code -
3951
default: 5
40-
--init Write initial configuration file and exit.
52+
--init Write initial configuration file 'xrlint-
53+
config.yaml' and exit.
4154
--version Show the version and exit.
4255
--help Show this message and exit.
56+
4357
```

docs/config.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Many parts have been copied and adjusted as it applies in many similar ways to X
77

88
The XRLint configuration file may be named any of the following:
99

10-
* `xrlint_config.yaml`
11-
* `xrlint_config.json`
12-
* `xrlint_config.py`
10+
* YAML format: `xrlint-config.yaml` (or use extension `.yml`)
11+
* JSON format: `xrlint-config.json`
12+
* Python module: `xrlint_config.py` (note the underscore)
1313

1414
It should be placed in the root directory of your project and export
1515
an array of [configuration objects](#configuration-objects) or
@@ -40,7 +40,7 @@ Same using JSON:
4040
]
4141
```
4242

43-
And as Python script:
43+
And as Python module:
4444

4545
```python
4646
def export_config():

docs/start.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Initializing a new project with
2727
xrlint --init
2828
```
2929

30-
writes a configuration file `xrlint_config.yaml`
30+
writes a configuration file `xrlint-config.yaml`
3131
into the current working directory:
3232

3333
```yaml

notebooks/xrlint-linter.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@
19241924
"name": "python",
19251925
"nbconvert_exporter": "python",
19261926
"pygments_lexer": "ipython3",
1927-
"version": "3.13.1"
1927+
"version": "3.12.8"
19281928
}
19291929
},
19301930
"nbformat": 4,

tests/cli/test_main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ def test_color_no_color(self):
160160
def test_files_with_invalid_config(self):
161161
with text_file(DEFAULT_CONFIG_FILE_YAML, self.invalid_config_yaml):
162162
result = self.xrlint("--no-color", *self.files)
163+
# noinspection SpellCheckingInspection
163164
self.assertEqual(
164-
"Error: xrlint_config.yaml: configuration 'recommentet' not found\n",
165+
f"Error: {DEFAULT_CONFIG_FILE_YAML}:"
166+
" configuration 'recommentet' not found\n",
165167
result.output,
166168
)
167169
self.assertEqual(1, result.exit_code)

xrlint/cli/constants.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,33 @@
44

55
from typing import Final
66

7-
DEFAULT_CONFIG_BASENAME: Final = "xrlint_config"
87

9-
DEFAULT_CONFIG_FILE_YAML: Final = f"{DEFAULT_CONFIG_BASENAME}.yaml"
10-
DEFAULT_CONFIG_FILE_JSON: Final = f"{DEFAULT_CONFIG_BASENAME}.json"
11-
DEFAULT_CONFIG_FILE_PY: Final = f"{DEFAULT_CONFIG_BASENAME}.py"
8+
_MODULE_BASENAME: Final = "xrlint_config"
9+
_REGULAR_BASENAME: Final = "xrlint-config"
10+
1211

1312
DEFAULT_CONFIG_FILES: Final = [
14-
DEFAULT_CONFIG_FILE_YAML,
15-
DEFAULT_CONFIG_FILE_JSON,
16-
DEFAULT_CONFIG_FILE_PY,
13+
# Added in 0.5.1:
14+
f"{_REGULAR_BASENAME}.yaml",
15+
f"{_REGULAR_BASENAME}.yml",
16+
f"{_REGULAR_BASENAME}.json",
17+
# Until 0.5.0:
18+
f"{_MODULE_BASENAME}.yaml",
19+
f"{_MODULE_BASENAME}.yml",
20+
f"{_MODULE_BASENAME}.json",
21+
f"{_MODULE_BASENAME}.py",
1722
]
1823

24+
DEFAULT_CONFIG_FILE_YAML: Final = f"{_REGULAR_BASENAME}.yaml"
1925
DEFAULT_OUTPUT_FORMAT: Final = "simple"
2026
DEFAULT_MAX_WARNINGS: Final = 5
2127

22-
INIT_CONFIG_YAML: Final = "- recommended\n"
28+
INIT_CONFIG_YAML: Final = (
29+
"# XRLint configuration file\n"
30+
"# See https://bcdev.github.io/xrlint/config/\n"
31+
"\n"
32+
"- recommended\n"
33+
)
2334

2435
DEFAULT_GLOBAL_FILES: Final = ["**/*.zarr", "**/*.nc"]
2536
DEFAULT_GLOBAL_IGNORES: Final = [".git", "node_modules"]

xrlint/cli/main.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
# Warning: do not import heavy stuff here, it can
1010
# slow down commands like "xrlint --help" otherwise.
1111
from xrlint.cli.constants import (
12-
DEFAULT_CONFIG_BASENAME,
1312
DEFAULT_MAX_WARNINGS,
1413
DEFAULT_OUTPUT_FORMAT,
14+
DEFAULT_CONFIG_FILE_YAML,
1515
)
1616
from xrlint.version import version
1717

@@ -20,17 +20,14 @@
2020
@click.option(
2121
"--no-config-lookup",
2222
"no_config_lookup",
23-
help=f"Disable use of default configuration from {DEFAULT_CONFIG_BASENAME}.*",
23+
help="Disable use of default configuration files",
2424
is_flag=True,
2525
)
2626
@click.option(
2727
"--config",
2828
"-c",
2929
"config_path",
30-
help=(
31-
f"Use this configuration, overriding {DEFAULT_CONFIG_BASENAME}.*"
32-
f" config options if present"
33-
),
30+
help="Use this configuration instead of looking for a default configuration file",
3431
metavar="FILE",
3532
)
3633
@click.option(
@@ -94,7 +91,7 @@
9491
@click.option(
9592
"--init",
9693
"init_mode",
97-
help="Write initial configuration file and exit.",
94+
help=f"Write initial configuration file '{DEFAULT_CONFIG_FILE_YAML}' and exit.",
9895
is_flag=True,
9996
)
10097
@click.argument("files", nargs=-1)
@@ -115,22 +112,33 @@ def main(
115112
):
116113
"""Validate the given dataset FILES.
117114
118-
Reads configuration from './xrlint_config.*' if such file
119-
exists and unless '--no-config-lookup' is set or '--config' is
120-
provided.
121-
It then validates each dataset in FILES against the configuration.
115+
When executed, XRLint does the following three things:
116+
117+
(1) Unless options '--no-config-lookup' or '--config' are used
118+
it searches for a default configuration file in the current working
119+
directory. Default configuration files are determined by their
120+
filename, namely 'xrlint_config.py' or 'xrlint-config.<format>',
121+
where <format> refers to the filename extensions
122+
'json', 'yaml', and 'yml'. A Python configuration file ('*.py'),
123+
is expected to provide XRLInt configuration from a function
124+
'export_config()', which may include custom plugins and rules.
125+
126+
(2) It then validates each dataset in FILES against the configuration.
122127
The default dataset patters are '**/*.zarr' and '**/.nc'.
123128
FILES may comprise also directories or URLs. The supported URL
124129
protocols are the ones supported by xarray. Using remote
125130
protocols may require installing additional packages such as
126131
S3Fs (https://s3fs.readthedocs.io/) for the 's3' protocol.
127-
128132
If a directory is provided that not matched by any file pattern,
129133
it will be traversed recursively.
130-
The validation result is dumped to standard output if not otherwise
134+
135+
(3) The validation result is dumped to standard output if not otherwise
131136
stated by '--output-file'. The output format is 'simple' by default.
132137
Other inbuilt formats are 'json' and 'html' which you can specify
133138
using the '--format' option.
139+
140+
Please refer to the documentation (https://bcdev.github.io/xrlint/)
141+
for more information.
134142
"""
135143
from xrlint.cli.engine import XRLint
136144

0 commit comments

Comments
 (0)