Core Utilities
Configuration, utilities, and helper functions.
FhircraftConfig
Path: fhircraft.config.FhircraftConfig
dataclass
FhircraftConfig(validation: ValidationConfig = ValidationConfig())
Global configuration for FHIRcraft.
This configuration class is designed to be easily extendable for future features beyond validation.
Attributes:
| Name | Type | Description |
|---|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validation
|
ValidationConfig
|
Configuration for FHIR validation behavior. Attributes: disable_warnings: Disable all validation warnings globally. disabled_constraints: Set of constraint keys to disable (e.g., 'dom-6'). disable_warning_severity: Disable only warnings, keep errors. disable_errors: Disable error-level constraints (use with extreme caution). mode: Validation mode - 'strict', 'lenient', or 'skip'. - strict: All validations enabled (default) - lenient: Convert errors to warnings - skip: Disable all validations |
<dynamic>
|
ValidationConfig
Path: fhircraft.config.ValidationConfig
dataclass
ValidationConfig(disable_warnings: bool = False, disabled_constraints: Set[str] = set(), disable_warning_severity: bool = False, disable_errors: bool = False, mode: Literal['strict', 'lenient', 'skip'] = 'strict')
Configuration for FHIR validation behavior.
Attributes:
| Name | Type | Description |
|---|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_warnings
|
bool
|
|
False
|
disabled_constraints
|
Set[str]
|
Build an unordered collection of unique elements. |
<dynamic>
|
disable_warning_severity
|
bool
|
|
False
|
disable_errors
|
bool
|
|
False
|
mode
|
Literal['strict', 'lenient', 'skip']
|
|
'strict'
|
configure
Configure FHIRcraft settings using keyword arguments.
This is a convenience function that allows setting configuration options without creating config objects explicitly.
**kwargs: Configuration options. Can include: - disable_validation_warnings (bool) - validation_mode (str: 'strict', 'lenient', 'skip') - disable_validation_errors (bool) - disabled_constraints (Set[str])
Source code in fhircraft/config.py
disable_constraint
disable_constraint(*constraint_keys: str) -> None
Disable specific validation constraints by their keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*constraint_keys
|
str
|
One or more constraint keys to disable (e.g., 'dom-6'). |
()
|
Source code in fhircraft/config.py
enable_constraint
enable_constraint(*constraint_keys: str) -> None
Re-enable specific validation constraints by their keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*constraint_keys
|
str
|
One or more constraint keys to re-enable. |
()
|
Source code in fhircraft/config.py
get_config
get_config() -> FhircraftConfig
Get the current FHIRcraft configuration.
Returns:
| Name | Type | Description |
|---|---|---|
FhircraftConfig |
FhircraftConfig
|
The current configuration instance. |
load_config_from_env
Load configuration from environment variables.
Supported environment variables
- FHIRCRAFT_DISABLE_WARNINGS: 'true' to disable validation warnings
- FHIRCRAFT_VALIDATION_MODE: 'strict', 'lenient', or 'skip'
- FHIRCRAFT_DISABLED_CONSTRAINTS: Comma-separated constraint keys
Source code in fhircraft/config.py
reset_config
Reset configuration to default values.
This is useful for testing or when you want to clear all configuration changes.
set_config
set_config(config: FhircraftConfig) -> None
Set the global FHIRcraft configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
FhircraftConfig
|
The new configuration to apply globally. |
required |
Warning
This sets the configuration globally and persists until changed.
Consider using with_config() for temporary changes.
Source code in fhircraft/config.py
with_config
Context manager for temporary configuration changes.
This allows you to temporarily modify configuration within a specific code block, with automatic restoration when exiting the block.
Yields:
| Type | Description |
|---|---|
FhircraftConfig
|
The temporary configuration. |
Source code in fhircraft/config.py
capitalize
contains_list_type
Recursively check if List is anywhere in the variable's typing.
Source code in fhircraft/utils.py
contains_only_none
Check if the input contains only None values recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Any
|
The input dictionary or list to check for only None values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
result |
bool
|
True if the input contains only None values, False otherwise. |
Source code in fhircraft/utils.py
ensure_list
Ensure that the input variable is converted into a list if it is not already an iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variable
|
any
|
The input variable that needs to be converted into a list if it is not already an iterable. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
variable |
list
|
The input variable converted into a list, or the input variable itself if it was already an iterable. |
Source code in fhircraft/utils.py
get_FHIR_release_from_version
Source code in fhircraft/utils.py
get_all_models_from_field
get_all_models_from_field(field: FieldInfo, issubclass_of: type[T_] = BaseModel) -> Generator[Type[T_], None, None]
Source code in fhircraft/utils.py
get_dict_paths
get_dict_paths(nested_dict: Union[Dict[str, Any], List[Dict[str, Any]]], prefix: str = '') -> Dict[str, Any]
Get all paths in a nested dictionary with their corresponding values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nested_dict
|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
The nested dictionary or list of dictionaries to extract paths from. |
required |
prefix
|
str
|
The prefix to be added to the paths (default is ''). |
''
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: A dictionary containing all paths in the nested dictionary with their corresponding values. |
Source code in fhircraft/utils.py
get_fhir_model_from_field
get_module_name
Returns the name of the module to which the given object belongs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object whose module name is to be retrieved. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The name of the module containing the object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the object does not belong to any module. |
Source code in fhircraft/utils.py
is_list_field
is_list_field(field) -> bool
Determines if a given Pydantic field or FieldInfo is a list type, including Optional[List[T]] and Union[List[T], ...].
Source code in fhircraft/utils.py
is_url
load_env_variables
Loads environment variables from a .env file into a dictionary without changing the global environment variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Optional[str]
|
Optional path to the .env file. If not provided, it looks for a .env file in the current directory. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
vars |
dict
|
A dictionary containing the environment variables from the .env file. |
Source code in fhircraft/utils.py
load_file
Load data from a file based on its extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The path to the file to load. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
data |
dict
|
The data loaded from the file as a dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file content is not a dictionary (for YAML files). |
Source code in fhircraft/utils.py
load_url
Load content from a URL and parse it based on the content type (YAML or JSON).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to load content from. |
required |
Returns:
| Type | Description |
|---|---|
Dict
|
Union[Dict, List, Any]: Parsed content from the URL. Can be a dictionary, list, or any other valid JSON/YAML data type. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the URL format is invalid or the content type is not supported. |
Source code in fhircraft/utils.py
merge_dicts
Merge two dictionaries recursively, merging lists element by element and dictionaries at the same index.
If a key exists in both dictionaries, the values are merged based on their types. If a key exists only in one dictionary, it is added to the merged dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dict1
|
dict
|
The first dictionary to merge. |
required |
dict2
|
dict
|
The second dictionary to merge. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The merged dictionary. |
Example
dict1 = {'a': 1, 'b': {'c': 2, 'd': [3, 4]}, 'e': [5, 6]} dict2 = {'b': {'c': 3, 'd': [4, 5]}, 'e': [6, 7], 'f': 8} merge_dicts(dict1, dict2) {'a': 1, 'b': {'c': 3, 'd': [3, 4, 5]}, 'e': [5, 6, 7], 'f': 8}
Source code in fhircraft/utils.py
model_rebuild_all
Call model_rebuild() on all Pydantic models defined in the module where this function is called. This is useful when models have forward references or need to be re-evaluated after all classes have been defined.
Source code in fhircraft/utils.py
remove_none_dicts
remove_none_dicts(d: Union[Dict[str, Any], List[Any], Any]) -> Union[Dict[str, Any], List[Any], Any]
Remove any dictionaries with all values being None from the input dictionary recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
Union[Dict[str, Any], List[Any], Any]
|
The input dictionary or list to remove None values from. |
required |
Returns:
| Type | Description |
|---|---|
Union[Dict[str, Any], List[Any], Any]
|
Union[Dict[str, Any], List[Any], Any]: The dictionary or list with None values removed. |
Source code in fhircraft/utils.py
replace_nth
Replace the nth occurrence of a substring in a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The original string. |
required |
sub
|
str
|
The substring to be replaced. |
required |
wanted
|
str
|
The new substring to replace with. |
required |
n
|
int
|
The occurrence number of the substring to replace. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
string |
str
|
The updated string after replacing the nth occurrence of the substring. |