Core Utilities¶
Configuration, utilities, and helper functions.
config ¶
FHIRcraft Global Configuration
Classes:
| Name | Description |
|---|---|
FhircraftConfig |
Global configuration for FHIRcraft. |
Functions:
| Name | Description |
|---|---|
get_config |
Get the current FHIRcraft configuration. |
configure |
Configure FHIRcraft settings. |
override_config |
Context manager for temporary configuration changes. |
disable_constraint |
Disable specific validation constraints by their keys. |
enable_constraint |
Re-enable specific validation constraints by their keys. |
reset_config |
Reset configuration to default values. |
load_config_from_env |
Load configuration from environment variables. |
FhircraftConfig
Path: fhircraft.config.FhircraftConfig
dataclass
¶
FhircraftConfig(disable_validation_warnings: bool = False, disable_fhir_warnings: bool = False, disable_fhir_errors: bool = False, disabled_fhir_constraints: FrozenSet[str] = frozenset(), validation_mode: Literal['strict', 'lenient', 'skip'] = 'strict', terminology_service: TerminologyService | None = None)
Global configuration for FHIRcraft.
Attributes:
| Name | Type | Description |
|---|
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_validation_warnings
|
bool
|
|
False
|
disable_fhir_warnings
|
bool
|
|
False
|
disable_fhir_errors
|
bool
|
|
False
|
disabled_fhir_constraints
|
FrozenSet[str]
|
Build an immutable unordered collection of unique elements. |
<dynamic>
|
validation_mode
|
Literal['strict', 'lenient', 'skip']
|
|
'strict'
|
terminology_service
|
TerminologyService | None
|
|
None
|
get_config ¶
get_config() -> FhircraftConfig
Get the current FHIRcraft configuration.
Returns:
| Name | Type | Description |
|---|---|---|
FhircraftConfig |
FhircraftConfig
|
The current configuration instance. |
configure ¶
configure(*, disable_validation_warnings: bool | None = None, disable_fhir_warnings: bool | None = None, disable_fhir_errors: bool | None = None, disabled_fhir_constraints: Sequence[str] | None = None, validation_mode: Literal['strict', 'lenient', 'skip'] | None = None, terminology_service: TerminologyService | None | object = _UNSET) -> None
Configure FHIRcraft settings.
All parameters are optional; only the ones provided will be changed. Unspecified parameters retain their current values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_validation_warnings
|
bool | None
|
Disable all validation warnings globally. |
None
|
disable_fhir_warnings
|
bool | None
|
Disable only FHIR warning-severity issues, keep errors. |
None
|
disable_fhir_errors
|
bool | None
|
Disable error-level constraints (use with extreme caution). |
None
|
disabled_fhir_constraints
|
Sequence[str] | None
|
Set of constraint keys to disable (e.g., {'dom-6'}). |
None
|
validation_mode
|
Literal['strict', 'lenient', 'skip'] | None
|
Validation mode - 'strict', 'lenient', or 'skip'. |
None
|
override_config ¶
override_config(*, disable_validation_warnings: bool | None = None, disable_fhir_warnings: bool | None = None, disable_fhir_errors: bool | None = None, disabled_fhir_constraints: Sequence[str] | None = None, validation_mode: Literal['strict', 'lenient', 'skip'] | None = None, terminology_service: TerminologyService | None | object = _UNSET) -> Generator[FhircraftConfig, None, None]
Context manager for temporary configuration changes.
All parameters are optional; only the ones provided will be changed within the context block. Previous configuration is automatically restored on exit, even if an exception occurs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disable_validation_warnings
|
bool | None
|
Disable all validation warnings globally. |
None
|
disable_fhir_warnings
|
bool | None
|
Disable only FHIR warning-severity issues, keep errors. |
None
|
disable_fhir_errors
|
bool | None
|
Disable error-level constraints (use with extreme caution). |
None
|
disabled_fhir_constraints
|
Sequence[str] | None
|
Set of constraint keys to disable (e.g., {'dom-6'}). |
None
|
validation_mode
|
Literal['strict', 'lenient', 'skip'] | None
|
Validation mode - 'strict', 'lenient', or 'skip'. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
config |
FhircraftConfig
|
The temporary configuration. |
disable_constraint ¶
disable_constraint(*constraint_keys: str) -> None
Disable specific validation constraints by their keys.
Creates a new configuration with the given keys added to
disabled_fhir_constraints; the change is visible to the current context
only and does not escape a surrounding override_config block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*constraint_keys
|
str
|
One or more constraint keys to disable (e.g., 'dom-6'). |
()
|
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. |
()
|
reset_config ¶
Reset configuration to default values.
This is useful for testing or when you want to clear all configuration changes.
load_config_from_env ¶
Load configuration from environment variables.
Supported environment variables
- FHIRCRAFT_DISABLE_WARNINGS: 'true' to disable all validation warnings
- FHIRCRAFT_VALIDATION_MODE: 'strict', 'lenient', or 'skip'
- FHIRCRAFT_DISABLED_CONSTRAINTS: Comma-separated constraint keys
utils ¶
Functions:
| Name | Description |
|---|---|
is_url |
Check if the input string is a valid URL. |
capitalize |
Capitalize the first letter of a given string. |
load_env_variables |
Loads environment variables from a .env file into a dictionary without changing the global environment variables. |
ensure_list |
Ensure that the input variable is converted into a list if it is not already an iterable. |
load_file |
Load data from a file based on its extension. |
to_snake_case |
Convert a given string from CamelCase to snake_case. |
load_url |
Load content from a URL and parse it based on the content type (YAML or JSON). |
contains_only_none |
Check if the input contains only None values recursively. |
remove_none_dicts |
Remove any dictionaries with all values being None from the input dictionary recursively. |
get_dict_paths |
Get all paths in a nested dictionary with their corresponding values. |
replace_nth |
Replace the nth occurrence of a substring in a string. |
contains_list_type |
Recursively check if List is anywhere in the variable's typing. |
is_dict_subset |
Return True if all keys/values in subset are present in superset. |
get_module_name |
Returns the name of the module to which the given object belongs. |
is_list_field |
Determines if a given Pydantic field or FieldInfo is a list type, |
model_rebuild_all |
Call model_rebuild() on all Pydantic models defined in the module where this function is called. |
is_url ¶
capitalize ¶
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. |
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. |
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). |
to_snake_case ¶
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. |
contains_only_none ¶
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. |
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. |
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. |
contains_list_type ¶
Recursively check if List is anywhere in the variable's typing.
is_dict_subset ¶
Return True if all keys/values in subset are present in superset.
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. |
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], ...].
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.