Skip to content

Code Generator

FHIR resource code generation utilities.

generator

Modules:

Name Description
core

Classes:

Name Description
CodeGenerator

CodeGenerator

Path: fhircraft.fhir.resources.generator.CodeGenerator

CodeGenerator()

Methods:

Name Description
generate_source

Generate source code for one or more pydantic model classes.

generate_files

Generate an importable package (as an in-memory mapping) for one or more resources.

generate

Generate a fully importable Python package on disk for one or more resources.

generate_source

generate_source(resources: Union[type[BaseModel], List[type[BaseModel]]]) -> str

Generate source code for one or more pydantic model classes.

Parameters:

Name Type Description Default
resources Union[type[BaseModel], List[type[BaseModel]]]

A single model class or a list of model classes to generate.

required

Returns:

Type Description
str

A string containing valid Python source code.

generate_files

generate_files(resources: Union[type[BaseModel], List[type[BaseModel]]], split: bool = True) -> Dict[str, str]

Generate an importable package (as an in-memory mapping) for one or more resources.

Parameters:

Name Type Description Default
resources Union[type[BaseModel], List[type[BaseModel]]]

A single model class or a list of model classes to generate.

required
split bool

If True, each generated model (including transitive dependencies, not just the input resources) is placed in its own module. If False, all models are placed in a single models.py module.

True

Returns:

Type Description
Dict[str, str]

A mapping of relative filename (including __init__.py) to file content.

generate

generate(resources: Union[type[BaseModel], List[type[BaseModel]]], output_dir: Union[str, PathLike], split: bool = True, exist_ok: bool = True) -> List[str]

Generate a fully importable Python package on disk for one or more resources.

Parameters:

Name Type Description Default
resources Union[type[BaseModel], List[type[BaseModel]]]

A single model class or a list of model classes to generate.

required
output_dir Union[str, PathLike]

Directory the package is written to (created if missing).

required
split bool

If True, each generated model gets its own module. If False, all models are written to a single models.py module.

True
exist_ok bool

If False, raise if output_dir already exists.

True

Returns:

Type Description
List[str]

The absolute paths of all files written.