Core Utilities
This section documents the utility functions and helpers used throughout the NDF Studio backend.
backend.core.utils
NDF Studio Core Utilities
This module provides utility functions used throughout the NDF Studio backend. These utilities handle common operations like file I/O, text processing, and data normalization.
Functions:
| Name | Description |
|---|---|
render_description_md |
Converts markdown text to safe HTML |
normalize_id |
Normalizes strings to valid identifiers |
save_json_file |
Saves data to JSON file |
load_json_file |
Loads data from JSON file |
load_text_file |
Loads text content from file |
Functions
render_description_md(text: Optional[str]) -> str
Converts markdown text to safe HTML for display.
This function takes markdown text and converts it to HTML, then sanitizes the HTML to prevent XSS attacks by allowing only safe tags and attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
Optional[str]
|
Markdown text to convert and sanitize |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Safe HTML string ready for display |
Source code in backend/core/utils.py
normalize_id(name: str) -> str
Normalizes a string to create a valid identifier.
This function converts a string to a valid identifier by: - Trimming whitespace - Preserving case (to avoid conflicts between 'Water' and 'water') - Replacing spaces with underscores
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The string to normalize |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Normalized identifier string |
Example
normalize_id(" My Node Name ") 'My_Node_Name' normalize_id(" Water ") 'Water' normalize_id(" water ") 'water'
Source code in backend/core/utils.py
save_json_file(path: Path, data: Dict[str, Any]) -> None
Saves data to a JSON file with proper formatting.
This function saves a dictionary to a JSON file with UTF-8 encoding and pretty formatting (2-space indentation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the JSON file to create/overwrite |
required |
data
|
Dict[str, Any]
|
Dictionary data to save |
required |
Raises:
| Type | Description |
|---|---|
IOError
|
If the file cannot be written |
TypeError
|
If the data cannot be serialized to JSON |
Example
save_json_file(Path("config.json"), {"key": "value"})
Creates config.json with: {"key": "value"}
Source code in backend/core/utils.py
load_json_file(path: Path) -> Dict[str, Any]
Loads data from a JSON file.
This function reads a JSON file and returns the parsed dictionary. The file is expected to be UTF-8 encoded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the JSON file to read |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Parsed JSON data as a dictionary |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file doesn't exist |
JSONDecodeError
|
If the file contains invalid JSON |
IOError
|
If the file cannot be read |
Example
data = load_json_file(Path("config.json")) print(data) {'key': 'value'}
Source code in backend/core/utils.py
load_text_file(path: Path) -> str
Loads text content from a file.
This function reads a text file and returns its content as a string.
The file is expected to be UTF-8 encoded.
Args:
path (Path): Path to the text file to read
Returns:
str: Content of the text file
Raises:
FileNotFoundError: If the file doesn't exist
IOError: If the file cannot be read
Example:
>>> content = load_text_file(Path("readme.md"))
>>> print(content[:50])
'# NDF Studio Documentation
This is the main...'