aind_behavior_services.schema package

Module contents

class aind_behavior_services.schema.BonsaiSgenSerializers(*values)[source]

Bases: Enum

JSON = 'json'[source]
NONE = 'None'[source]
YAML = 'yaml'[source]
class aind_behavior_services.schema.CustomGenerateJsonSchema(*args, **kwargs)[source]

Bases: GenerateJsonSchema

Custom JSON Schema generator to modify the way certain schemas are generated.

enum_schema(schema)[source]

Generates a JSON schema that matches an Enum value.

Parameters:

schema (EnumSchema) – The core schema.

Returns:

The generated JSON schema.

Return type:

dict[str, Any]

generate_inner(schema)[source]

Generates a JSON schema for a given core schema.

Parameters:

schema (InvalidSchema | AnySchema | NoneSchema | BoolSchema | IntSchema | FloatSchema | DecimalSchema | StringSchema | BytesSchema | DateSchema | TimeSchema | DatetimeSchema | TimedeltaSchema | LiteralSchema | MissingSentinelSchema | EnumSchema | IsInstanceSchema | IsSubclassSchema | CallableSchema | ListSchema | TupleSchema | SetSchema | FrozenSetSchema | GeneratorSchema | DictSchema | AfterValidatorFunctionSchema | BeforeValidatorFunctionSchema | WrapValidatorFunctionSchema | PlainValidatorFunctionSchema | WithDefaultSchema | NullableSchema | UnionSchema | TaggedUnionSchema | ChainSchema | LaxOrStrictSchema | JsonOrPythonSchema | TypedDictSchema | ModelFieldsSchema | ModelSchema | DataclassArgsSchema | DataclassSchema | ArgumentsSchema | ArgumentsV3Schema | CallSchema | CustomErrorSchema | JsonSchema | UrlSchema | MultiHostUrlSchema | DefinitionsSchema | DefinitionReferenceSchema | UuidSchema | ComplexSchema) – The given core schema.

Returns:

The generated JSON schema.

Return type:

dict[str, Any]

TODO: the nested function definitions here seem like bad practice, I’d like to unpack these in a future PR. It’d be great if we could shorten the call stack a bit for JSON schema generation, and I think there’s potential for that here.

get_flattened_oneof(schemas)[source]
Parameters:

schemas (list[dict[str, Any]])

Return type:

dict[str, Any]

literal_schema(schema)[source]

Generates a JSON schema that matches a literal value.

Parameters:

schema (LiteralSchema) – The core schema.

Returns:

The generated JSON schema.

Return type:

dict[str, Any]

nullable_schema(schema)[source]

Generates a JSON schema that matches a schema that allows null values.

Parameters:

schema (NullableSchema) – The core schema.

Returns:

The generated JSON schema.

Return type:

dict[str, Any]

union_schema(schema)[source]

Generates a JSON schema that matches a schema that allows values matching any of the given schemas.

Parameters:

schema (UnionSchema) – The core schema.

Returns:

The generated JSON schema.

Return type:

dict[str, Any]

class aind_behavior_services.schema.SgenNamespace(namespace)[source]

Bases: object

Parameters:

namespace (str)

property namespace: str[source]
sgen_typename(*, typename=None)[source]
Parameters:

typename (str | None)

Return type:

Callable[[Type[T]], Type[T]]

aind_behavior_services.schema.bonsai_sgen(schema_path, output_path, namespace=None, root_element=None, serializer=None)[source]

Runs Bonsai.SGen to generate a Bonsai-compatible schema from a json-schema model For more information run bonsai.sgen –help in the command line.

Returns:

The result of running the command.

Return type:

CompletedProcess

Parameters:
  • schema_path (PathLike) – Target Json Schema file

  • output_path (PathLike) – Specifies the name of the file containing the generated code.

  • namespace (Optional[str], optional) – Specifies the namespace to use for all generated serialization classes. Defaults to DataSchema.

  • root_element (Optional[str], optional) – Specifies the name of the class used to represent the schema root element. If None, it will use the json schema root element. Defaults to None.

  • serializer (Optional[List[BonsaiSgenSerializers]], optional) – Specifies the serializer data annotations to include in the generated classes. Defaults to None.

aind_behavior_services.schema.convert_pydantic_to_bonsai(model, *, model_name=None, json_schema_output_dir=PosixPath('src/DataSchemas'), cs_output_dir=PosixPath('src/Extensions'), cs_namespace='DataSchema', cs_serializer=None, json_schema_export_kwargs=None, root_element=None)[source]
Parameters:
  • model (Type[BaseModel])

  • model_name (str | None)

  • json_schema_output_dir (PathLike)

  • cs_output_dir (PathLike | None)

  • cs_namespace (str)

  • cs_serializer (List[BonsaiSgenSerializers] | None)

  • json_schema_export_kwargs (Dict[str, Any] | None)

  • root_element (str | None)

Return type:

CompletedProcess | None

aind_behavior_services.schema.export_schema(model, schema_generator=<class 'aind_behavior_services.schema.CustomGenerateJsonSchema'>, mode='serialization', remove_root=True)[source]

Export the schema of a model to a json file

Parameters:
  • model (Type[BaseModel])

  • schema_generator (Type[GenerateJsonSchema])

  • mode (Literal['validation', 'serialization'])

  • remove_root (bool)

aind_behavior_services.schema.sgen_typename(*, typename=None, namespace=None)[source]

Class decorator to add an x-sgen-typename property to the model’s JSON schema, which Bonsai.SGen uses to determine the typename for the generated class.

Parameters:
  • typename (str | None)

  • namespace (str | None)

Return type:

Callable[[Type[T]], Type[T]]