|
| 1 | +import re |
| 2 | + |
| 3 | +from xrlint.node import DataArrayNode, DatasetNode |
| 4 | +from xrlint.plugins.core.plugin import plugin |
| 5 | +from xrlint.rule import RuleContext, RuleOp |
| 6 | +from xrlint.util.schema import schema |
| 7 | + |
| 8 | +DEFAULT_GLOBAL_ATTRS = ["title", "history"] |
| 9 | +DEFAULT_VAR_ATTRS = ["institution", "source", "references", "comment"] |
| 10 | +DEFAULT_IGNORES = ["crs", "spatial_ref"] |
| 11 | + |
| 12 | + |
| 13 | +@plugin.define_rule( |
| 14 | + "dataset-description", |
| 15 | + version="1.0.0", |
| 16 | + type="suggestion", |
| 17 | + description=( |
| 18 | + "A dataset should provide information about where the data came" |
| 19 | + " from and what has been done to it." |
| 20 | + " This information is mainly for the benefit of human readers." |
| 21 | + " The rule accepts the following configuration parameters:\n\n" |
| 22 | + "- `global_attrs`: list of global attribute names." |
| 23 | + f" Defaults to `{DEFAULT_GLOBAL_ATTRS}`.\n" |
| 24 | + "- `var_attrs`: list of variable attribute names." |
| 25 | + f" Defaults to `{DEFAULT_VAR_ATTRS}`.\n" |
| 26 | + "- `ignored_vars`: list of ignored variables (regex patterns)." |
| 27 | + f" Defaults to `{DEFAULT_IGNORES}`.\n" |
| 28 | + "" |
| 29 | + ), |
| 30 | + docs_url=( |
| 31 | + "https://cfconventions.org/cf-conventions/cf-conventions.html" |
| 32 | + "#description-of-file-contents" |
| 33 | + ), |
| 34 | + schema=schema( |
| 35 | + "object", |
| 36 | + properties={ |
| 37 | + "global_attrs": schema( |
| 38 | + "array", |
| 39 | + items=schema("string"), |
| 40 | + default=DEFAULT_GLOBAL_ATTRS, |
| 41 | + title="Global dataset attribute names", |
| 42 | + ), |
| 43 | + "var_attrs": schema( |
| 44 | + "array", |
| 45 | + items=schema("string"), |
| 46 | + default=DEFAULT_VAR_ATTRS, |
| 47 | + title="Data variable attribute names", |
| 48 | + ), |
| 49 | + "ignored_vars": schema( |
| 50 | + "array", |
| 51 | + items=schema("string"), |
| 52 | + default=DEFAULT_IGNORES, |
| 53 | + title="Ignored variables (regex name patterns)", |
| 54 | + ), |
| 55 | + }, |
| 56 | + ), |
| 57 | +) |
| 58 | +class DatasetDescription(RuleOp): |
| 59 | + def __init__( |
| 60 | + self, global_attrs: list[str] | None = None, var_attrs: list[str] | None = None |
| 61 | + ): |
| 62 | + self.global_attrs = global_attrs if global_attrs else DEFAULT_GLOBAL_ATTRS |
| 63 | + self.var_attrs = var_attrs if var_attrs else DEFAULT_VAR_ATTRS |
| 64 | + self.ignored_vars = [ |
| 65 | + re.compile(p) for p in (var_attrs if var_attrs else DEFAULT_VAR_ATTRS) |
| 66 | + ] |
| 67 | + |
| 68 | + def dataset(self, ctx: RuleContext, node: DatasetNode): |
| 69 | + dataset = node.dataset |
| 70 | + for attr_name in self.global_attrs: |
| 71 | + if attr_name not in dataset.attrs: |
| 72 | + ctx.report(f"Missing dataset attribute {attr_name!r}.") |
| 73 | + |
| 74 | + def data_array(self, ctx: RuleContext, node: DataArrayNode): |
| 75 | + dataset = ctx.dataset |
| 76 | + if node.name not in dataset.data_vars: |
| 77 | + return |
| 78 | + |
| 79 | + for m in self.ignored_vars: |
| 80 | + if m.match(str(node.name)): |
| 81 | + return |
| 82 | + |
| 83 | + var = node.data_array |
| 84 | + for attr_name in self.var_attrs: |
| 85 | + if attr_name not in var.attrs and attr_name not in dataset.attrs: |
| 86 | + ctx.report(f"Missing data variable attribute {attr_name!r}.") |
0 commit comments