Skip to content

Controlled vocabulary

ControlledVocabularyExtract

Bases: ExtractionMethod

Compare properties to a controlled vocabulary defined by a pydantic.BaseModel.

Method name: controlled_vocabulary

Example Configuration

.. code-block:: yaml

- name: controlled_vocabulary
  inputs:
    model: my_cv.collections.CMIP5
    strict: False
Source code in extraction_methods/plugins/controlled_vocabulary.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class ControlledVocabularyExtract(ExtractionMethod):
    """
    Compare properties to a controlled vocabulary defined by a pydantic.BaseModel.

    **Method name:** ``controlled_vocabulary``

    Example Configuration:
        .. code-block:: yaml

            - name: controlled_vocabulary
              inputs:
                model: my_cv.collections.CMIP5
                strict: False
    """

    input_class = ControlledVocabularyInput

    @update_input
    def run(self, body: dict[str, Any]) -> dict[str, Any]:
        # Import data model
        scopes = self.input.model.split(".")
        module_str = ".".join(scopes[:-1])

        module = importlib.import_module(module_str)
        klass = getattr(module, scopes[-1])

        # Get metadata attributes
        properties = body

        # Instantiate data model
        try:
            cv = klass(**properties)
            body = cv.dict()

        except ValidationError as exc:
            LOGGER.warning(exc)

            if self.input.strict:
                raise exc

        return body

ControlledVocabularyInput

Bases: Input

Model for Contrilled Vocabulary Method Input.

Parameters:

Name Type Description Default
model str

pydantic.BaseModel subclass to be imported at run-time, e.g. package.module.class_name.

required
strict bool

If True, raise ValidationError, otherwise simply log ValidationError messages.

False
Source code in extraction_methods/plugins/controlled_vocabulary.py
25
26
27
28
29
30
31
32
33
34
35
36
class ControlledVocabularyInput(Input):
    """
    Model for Contrilled Vocabulary Method Input.
    """

    model: str = Field(
        description="pydantic.BaseModel subclass to be imported at run-time, e.g. `package.module.class_name`.",
    )
    strict: bool = Field(
        default=False,
        description="If True, raise ValidationError, otherwise simply log ValidationError messages.",
    )