Skip to content

Commit 79f497e

Browse files
authored
Merge pull request #59 from bcdev/forman-docs_update
New examples doc page
2 parents 4c3b8fe + 8dd1372 commit 79f497e

File tree

8 files changed

+120
-19
lines changed

8 files changed

+120
-19
lines changed

CHANGES.md

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

33
## Version 0.5.1 (in development)
44

5+
- Enhanced documentation by a new page that compiles
6+
the code examples in the `examples` folder.
57

68
## Version 0.5.0 (from 2025-02-13)
79

docs/api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,26 @@ Note:
4646
the `xrlint.all` convenience module exports all of the above from a
4747
single module.
4848

49+
## CLI API
50+
4951
::: xrlint.cli.engine.XRLint
5052

53+
## Linter API
54+
5155
::: xrlint.linter.new_linter
5256

5357
::: xrlint.linter.Linter
5458

59+
## Plugin API
60+
5561
::: xrlint.plugin.new_plugin
5662

5763
::: xrlint.plugin.Plugin
5864

5965
::: xrlint.plugin.PluginMeta
6066

67+
## Configuration API
68+
6169
::: xrlint.config.Config
6270

6371
::: xrlint.config.ConfigObject
@@ -66,6 +74,8 @@ Note:
6674

6775
::: xrlint.config.ConfigObjectLike
6876

77+
## Rule API
78+
6979
::: xrlint.rule.define_rule
7080

7181
::: xrlint.rule.Rule
@@ -78,6 +88,8 @@ Note:
7888

7989
::: xrlint.rule.RuleExit
8090

91+
## Dataset Node API
92+
8193
::: xrlint.node.Node
8294

8395
::: xrlint.node.XarrayNode
@@ -92,6 +104,8 @@ Note:
92104

93105
::: xrlint.node.AttrNode
94106

107+
## Processor API
108+
95109
::: xrlint.processor.define_processor
96110

97111
::: xrlint.processor.Processor
@@ -100,12 +114,16 @@ Note:
100114

101115
::: xrlint.processor.ProcessorOp
102116

117+
## Result API
118+
103119
::: xrlint.result.Result
104120

105121
::: xrlint.result.Message
106122

107123
::: xrlint.result.Suggestion
108124

125+
## Testing API
126+
109127
::: xrlint.testing.RuleTester
110128

111129
::: xrlint.testing.RuleTest

docs/examples.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Examples
2+
3+
## Configuration
4+
5+
::: examples.plugin_config
6+
7+
options:
8+
members: false
9+
10+
Source code: [`examples/plugin_config.py`](https://github.com/bcdev/xrlint/blob/main/examples/plugin_config.py)
11+
12+
::: examples.virtual_plugin_config
13+
14+
options:
15+
members: false
16+
17+
Source code: [`examples/virtual_plugin_config.py`](https://github.com/bcdev/xrlint/blob/main/examples/virtual_plugin_config.py)
18+
19+
## Developing rules
20+
21+
::: examples.rule_testing
22+
23+
options:
24+
members: false
25+
26+
Source code: [`examples/rule_testing.py`](https://github.com/bcdev/xrlint/blob/main/examples/rule_testing.py)
27+
28+
## API usage
29+
30+
::: examples.check_s3_bucket
31+
32+
options:
33+
members: false
34+
35+
Source code: [`examples/check_s3_bucket.py`](https://github.com/bcdev/xrlint/blob/main/examples/check_s3_bucket.py)

examples/check_s3_bucket.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# This software is distributed under the terms and conditions of the
33
# MIT license (https://mit-license.org/).
44

5+
"""
6+
This code example shows how to use the high-level
7+
Python API to validate the contents of an S3 bucket.
8+
"""
9+
510
import xrlint.all as xrl
611

712
URL = "s3://xcube-test/"

examples/plugin_config.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
# Copyright © 2025 Brockmann Consult GmbH.
2+
# This software is distributed under the terms and conditions of the
3+
# MIT license (https://mit-license.org/).
4+
15
"""
26
This configuration example shows how to define and use a plugin
37
using the `Plugin` class and its `define_rule()` decorator method.
4-
"""
58
6-
# Copyright © 2025 Brockmann Consult GmbH.
7-
# This software is distributed under the terms and conditions of the
8-
# MIT license (https://mit-license.org/).
9+
You can use this example directly via the Python API by passing it's
10+
exported configuration to an instance of the `Linter` class or use
11+
the XRLint CLI:
12+
13+
```bash
14+
xrlint -c examples/plugin_config.py <OPTIONS> <FILES>
15+
```
16+
"""
917

1018
from xrlint.node import DatasetNode
1119
from xrlint.plugin import new_plugin

examples/rule_testing.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
"""
2-
This example demonstrates how to develop new rules.
3-
"""
4-
51
# Copyright © 2025 Brockmann Consult GmbH.
62
# This software is distributed under the terms and conditions of the
73
# MIT license (https://mit-license.org/).
84

5+
"""
6+
This example demonstrates how to develop new rules.
7+
"""
8+
99
import xarray as xr
1010

1111
from xrlint.node import DatasetNode
1212
from xrlint.rule import RuleContext, RuleOp, define_rule
1313
from xrlint.testing import RuleTest, RuleTester
1414

1515

16+
# ----------------------------------------------------
17+
# Place the rule implementation code in its own module
18+
# ----------------------------------------------------
19+
20+
1621
@define_rule("good-title")
1722
class GoodTitle(RuleOp):
1823
"""Dataset title should be 'Hello World!'."""
1924

25+
# We just validate the dataset instance here. You can also implement
26+
# the validation of other nodes in this class, e.g.,
27+
# validate_datatree(), validate_variable(), validate_attrs(),
28+
# and validate_attr().
29+
#
2030
def validate_dataset(self, ctx: RuleContext, node: DatasetNode):
2131
good_title = "Hello World!"
2232
if node.dataset.attrs.get("title") != good_title:
@@ -26,27 +36,34 @@ def validate_dataset(self, ctx: RuleContext, node: DatasetNode):
2636
)
2737

2838

29-
# -----------------
30-
# In another module
31-
# -----------------
39+
# ---------------------------------------------------
40+
# Place the following rule test code in a test module
41+
# ---------------------------------------------------
3242

3343
tester = RuleTester()
3444

3545
valid_dataset = xr.Dataset(attrs=dict(title="Hello World!"))
3646
invalid_dataset = xr.Dataset(attrs=dict(title="Hello Hamburg!"))
3747

38-
# Run test directly
48+
# You can use the tester to run a test directly
49+
#
3950
tester.run(
4051
"good-title",
4152
GoodTitle,
4253
valid=[RuleTest(dataset=valid_dataset)],
54+
# We expect one message to be emitted
4355
invalid=[RuleTest(dataset=invalid_dataset, expected=1)],
4456
)
4557

46-
# or define a test class derived from unitest.TestCase
58+
# ... or generate a test class that will be derived from `unitest.TestCase`.
59+
# This will provide you tooling support via your test runner, e.g., pytest,
60+
# as the tests in `valid` and `invalid` will be transformed into
61+
# test methods of the generated class.
62+
#
4763
GoodTitleTest = tester.define_test(
4864
"good-title",
4965
GoodTitle,
5066
valid=[RuleTest(dataset=valid_dataset)],
51-
invalid=[RuleTest(dataset=invalid_dataset)],
67+
# Note, here we expect a specific message to be emitted
68+
invalid=[RuleTest(dataset=invalid_dataset, expected=["Attribute 'title' wrong."])],
5269
)

examples/virtual_plugin_config.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
# Copyright © 2025 Brockmann Consult GmbH.
2+
# This software is distributed under the terms and conditions of the
3+
# MIT license (https://mit-license.org/).
4+
15
"""
26
This configuration example demonstrates how to
37
define and use "virtual" plugins. Such plugins
48
can be defined inside a configuration item.
5-
"""
69
7-
# Copyright © 2025 Brockmann Consult GmbH.
8-
# This software is distributed under the terms and conditions of the
9-
# MIT license (https://mit-license.org/).
10+
You can use this example directly via the Python API by passing it's
11+
exported configuration to an instance of the `Linter` class or use
12+
the XRLint CLI:
13+
14+
```bash
15+
xrlint -c examples/virtual_plugin_config.py <OPTIONS> <FILES>
16+
```
17+
"""
1018

1119
from xrlint.node import DatasetNode
1220
from xrlint.rule import RuleContext, RuleOp, define_rule

mkdocs.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ nav:
1111
- Rule Reference: rule-ref.md
1212
- CLI: cli.md
1313
- Python API: api.md
14+
- Examples: examples.md
1415
- About: about.md
1516

1617
theme:
@@ -55,9 +56,16 @@ plugins:
5556
python:
5657
options:
5758
docstring_style: google
59+
docstring_section_style: list
60+
show_object_full_path: true
5861
show_root_toc_entry: true
5962
show_root_heading: true
6063
show_source: true
61-
heading_level: 2
64+
show_category_heading: true
65+
show_symbol_type_heading: true
66+
show_symbol_type_toc: true
67+
heading_level: 3
6268
annotations_path: brief
6369
members_order: source
70+
extra:
71+
show_overloads: true

0 commit comments

Comments
 (0)