gemseo / core / grammars

json_grammar module

A Grammar based on JSON schema.

class gemseo.core.grammars.json_grammar.JSONGrammar(name, schema_path=None, descriptions=None, **kwargs)[source]

Bases: BaseGrammar

A grammar based on a JSON schema.

Initialize self. See help(type(self)) for accurate signature.

Parameters:
  • name (str) – The name of the grammar.

  • schema_path (str | Path | None) – The path to a JSON schema file. If None, do not initialize the grammar from a JSON schema file.

  • descriptions (Mapping[str, str] | None) – The descriptions of the elements read from schema_path, in the form: {element_name: element_meaning}. If None, use the descriptions available in the JSON schema if any.

  • **kwargs (Any) – These arguments are not used.

Raises:

ValueError – If the name is empty.

add_namespace(name, namespace)

Add a namespace prefix to an existing grammar element.

The updated element name will be namespace``+:data:`~gemseo.core.namespaces.namespace_separator`+``name.

Parameters:
  • name (str) – The element name to rename.

  • namespace (str) – The name of the namespace.

Return type:

None

clear()[source]

Empty the grammar.

Return type:

None

convert_to_simple_grammar()[source]

Convert the grammar to a SimpleGrammar.

Returns:

A SimpleGrammar version of the current grammar.

Return type:

SimpleGrammar

get(k[, d]) D[k] if k in D, else d.  d defaults to None.
is_array(name, numeric_only=False)[source]

Check if an element is an array.

Parameters:
  • name (str) – The name of the element.

  • numeric_only (bool) –

    Whether to check if the array elements are numbers.

    By default it is set to False.

Returns:

Whether the element is an array. If check_items_number is set to True, then return whether the element is an array and its items are numbers.

Raises:

KeyError – If the element is not in the grammar.

Return type:

bool

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
rename_element(current_name, new_name)[source]

Rename an element.

Parameters:
  • current_name (str) – The current name of the element.

  • new_name (str) – The new name of the element.

Return type:

None

restrict_to(names)[source]

Restrict the grammar to the given names.

Parameters:

names (Sequence[str]) – The names of the elements to restrict the grammar to.

Raises:

KeyError – If a name is not in the grammar.

Return type:

None

set_descriptions(descriptions)[source]

Set the properties descriptions.

Parameters:

descriptions (Mapping[str, str]) – The descriptions, mapping properties names to the description.

Return type:

None

to_json(*args, **kwargs)[source]

Return the JSON representation of the grammar schema.

Parameters:
Returns:

The JSON representation of the schema.

Return type:

str

update(grammar, exclude_names=None)[source]

Update the elements from another grammar or names or a schema.

Parameters:
  • grammar (JSONGrammar | Iterable[str] | DictSchemaType) – The grammar or names to update from.

  • exclude_names (Iterable[str] | None) – The names of the elements that shall not be updated.

Return type:

None

update_from_data(data)[source]

Update the grammar from name-value pairs.

Parameters:

data (Mapping[str, Any]) – The data from which to get the names and types, typically {element_name: element_value}.

Raises:

TypeError – If a value has a bad type.

Return type:

None

Notes

The types of the values of the data will be converted to JSON Schema types and define the elements of the JSON Schema.

update_from_file(path)[source]

Update the grammar from a schema file.

Parameters:

path (str | Path) – The path to the schema file.

Raises:

FileNotFoundError – If the schema file does not exist.

Return type:

None

validate(data, raise_exception=True)[source]

Validate data against the grammar.

Parameters:
  • data (MutableMapping[str, Any]) – The data to be checked, with a dictionary-like format: {element_name: element_value}.

  • raise_exception (bool) –

    Whether to raise an exception when the validation fails.

    By default it is set to True.

Raises:

InvalidDataException – If the passed data is not a dictionary, or if the data is not consistent with the grammar.

Return type:

None

values() an object providing a view on D's values
write(path=None)[source]

Write the grammar schema to a file.

Parameters:

path (Path | str | None) – The file path. If None, then write to a file named after the grammar and with .json extension.

Return type:

None

from_namespaced: NamespacesMapping

The mapping from element names with namespace prefix to element names without namespace prefix.

name: str

The name of the grammar.

property names: KeysView[str]

The names of the elements.

property required_names: set[str]

The names of the required elements.

property schema: dict[str, typing.Mapping[str, typing.Union[str, float, bool, typing.Sequence[typing.Union[str, float, bool]], typing.List[typing.Union[str, float, bool, typing.Sequence[typing.Union[str, float, bool]]]], typing.Mapping[str, typing.Union[str, float, bool, typing.Sequence[typing.Union[str, float, bool]], typing.List[typing.Union[str, float, bool, typing.Sequence[typing.Union[str, float, bool]]]], ForwardRef('DictSchemaType')]]]]]

The dictionary representation of the schema.

to_namespaced: NamespacesMapping

The mapping from element names without namespace prefix to element names with namespace prefix.