bidsschematools.utils

Utility functions for the bids-specification schema.

Classes

class bidsschematools.utils.WarningsFilter(*filters: tuple[Literal['default', 'error', 'ignore', 'always', 'all', 'module', 'once']])

Bases: object

Context manager to apply warning filters.

Arguments are lists of positional arguments to warnings.filterwarnings().

Functions

bidsschematools.utils.configure_logger(lgr: Logger) None

Configuring formatting and stream handler for the logger.

Should not be used when bidsschematools is used as a library.

Parameters:

lgr (logging.Logger)

bidsschematools.utils.get_bundled_schema_path() str

Get the path to the schema directory.

Returns:

Absolute path to the directory containing schema-related files.

Return type:

str

bidsschematools.utils.get_logger(name: str | None = None, level: int | str | None = None) Logger

Return a logger to use.

Parameters:
  • name (None or str, optional) – Name of the logger. Defaults to None.

  • level (None or int, optional) – Level to set for the logger. Defaults to None. BIDS_SCHEMA_LOG_LEVEL environment variable can be used to set the level and will overwrite this value.

Returns:

logger object.

Return type:

logging.Logger

bidsschematools.utils.in_context(context_manager: AbstractContextManager) Callable[[Callable], Callable]

Convert a context manager into a function decorator.

Parameters:

context_manager (context manager) – The context manager to use.

Returns:

The function decorator.

Return type:

Callable

bidsschematools.utils.jsonschema_validator(schema: dict[str, Any], *, check_format: bool, default_cls: type[object] | None = None) object

Create a jsonschema validator appropriate for validating instances against a given JSON schema

Parameters:
  • schema (dict[str, Any]) – The JSON schema to validate against

  • check_format (bool) – Indicates whether to check the format against format specifications in the schema

  • default_cls (type[JsonschemaValidator] or None, optional) – The default JSON schema validator class to use to create the validator should the appropriate validator class cannot be determined based on the schema (by assessing the $schema property). If None, the class representing the latest JSON schema draft supported by the jsonschema package

Returns:

The JSON schema validator

Return type:

JsonschemaValidator

Raises:

jsonschema.exceptions.SchemaError – If the JSON schema is invalid

bidsschematools.utils.set_logger_level(lgr: Logger, level: int | str) None

Set the logger level.

Parameters:
  • lgr (logging.Logger) – logger object for which to change the level.

  • level (int or str) – The desired level for the logger.