Functions
_strip_bom
Strip UTF-8 BOM from content if present.
_strip_bom
def _strip_bom(content: str, file_path: Path, encoding: str, caller: str | None = None) -> str
Strip UTF-8 BOM from content if present.
Parameters 4
| Name | Type | Default | Description |
|---|---|---|---|
content |
str |
— | File content |
file_path |
Path |
— | Path to file (for logging) |
encoding |
str |
— | Encoding used (for logging) |
caller |
str | None |
None |
Caller identifier for logging |
Returns
Content with BOM removed if present, otherwise unchangedstr
—
read_text_file
Read text file with robust error handling and encoding fallback.
Consolidates patterns from:
- ben…
read_text_file
def read_text_file(file_path: Path | str, encoding: str = 'utf-8', fallback_encoding: str | None = 'latin-1', on_error: str = 'raise', caller: str | None = None) -> str | None
Read text file with robust error handling and encoding fallback.
Consolidates patterns from:
- bengal/discovery/content_discovery.py:192 (UTF-8 with latin-1 fallback)
- bengal/rendering/template_functions/files.py:78 (file reading with logging)
- bengal/config/loader.py:137 (config file reading)
Parameters 5
| Name | Type | Default | Description |
|---|---|---|---|
file_path |
Path | str |
— | Path to file to read |
encoding |
str |
'utf-8' |
Primary encoding to try (default: 'utf-8') |
fallback_encoding |
str | None |
'latin-1' |
Fallback encoding if primary fails (default: 'latin-1') |
on_error |
str |
'raise' |
Error handling strategy: - 'raise': Raise exception on error - 'return_empty': Return empty string on error - 'return_none': Return None on error |
caller |
str | None |
None |
Caller identifier for logging context |
Returns
File contents as string, or None/empty string based on on_error. Encoding notes:str | None
—
utf-8-sigbefore the configured fallback.
load_json
Load JSON file with error handling.
Consolidates patterns from:
- bengal/rendering/template_functi…
load_json
def load_json(file_path: Path | str, on_error: str = 'return_empty', caller: str | None = None) -> Any
Load JSON file with error handling.
Consolidates patterns from:
- bengal/rendering/template_functions/data.py:80 (JSON loading)
Parameters 3
| Name | Type | Default | Description |
|---|---|---|---|
file_path |
Path | str |
— | Path to JSON file |
on_error |
str |
'return_empty' |
Error handling strategy ('raise', 'return_empty', 'return_none') |
caller |
str | None |
None |
Caller identifier for logging |
Returns
Parsed JSON data, or {} / None based on on_errorAny
—
load_yaml
Load YAML file with error handling.
Consolidates patterns from:
- bengal/config/loader.py:142 (YAM…
load_yaml
def load_yaml(file_path: Path | str, on_error: str = 'return_empty', caller: str | None = None) -> dict[str, Any] | None
Load YAML file with error handling.
Consolidates patterns from:
- bengal/config/loader.py:142 (YAML config loading)
- bengal/rendering/template_functions/data.py:94 (YAML data loading)
Parameters 3
| Name | Type | Default | Description |
|---|---|---|---|
file_path |
Path | str |
— | Path to YAML file |
on_error |
str |
'return_empty' |
Error handling strategy ('raise', 'return_empty', 'return_none') |
caller |
str | None |
None |
Caller identifier for logging |
Returns
Parsed YAML data, or {} / None based on on_errordict[str, Any] | None
—
load_toml
Load TOML file with error handling.
Consolidates patterns from:
- bengal/config/loader.py:137 (TOM…
load_toml
def load_toml(file_path: Path | str, on_error: str = 'return_empty', caller: str | None = None) -> dict[str, Any] | None
Load TOML file with error handling.
Consolidates patterns from:
- bengal/config/loader.py:137 (TOML config loading)
Parameters 3
| Name | Type | Default | Description |
|---|---|---|---|
file_path |
Path | str |
— | Path to TOML file |
on_error |
str |
'return_empty' |
Error handling strategy ('raise', 'return_empty', 'return_none') |
caller |
str | None |
None |
Caller identifier for logging |
Returns
Parsed TOML data, or {} / None based on on_errordict[str, Any] | None
—
load_data_file
Auto-detect and load JSON/YAML/TOML file.
Consolidates pattern from:
- bengal/rendering/template_f…
load_data_file
def load_data_file(file_path: Path | str, on_error: str = 'return_empty', caller: str | None = None) -> dict[str, Any] | None
Auto-detect and load JSON/YAML/TOML file.
Consolidates pattern from:
- bengal/rendering/template_functions/data.py:40 (get_data function)
Parameters 3
| Name | Type | Default | Description |
|---|---|---|---|
file_path |
Path | str |
— | Path to data file (.json, .yaml, .yml, .toml) |
on_error |
str |
'return_empty' |
Error handling strategy ('raise', 'return_empty', 'return_none') |
caller |
str | None |
None |
Caller identifier for logging |
Returns
Parsed data, or {} / None based on on_errordict[str, Any] | None
—
write_text_file
Write text to file with parent directory creation.
write_text_file
def write_text_file(file_path: Path | str, content: str, encoding: str = 'utf-8', create_parents: bool = True, caller: str | None = None) -> None
Write text to file with parent directory creation.
Parameters 5
| Name | Type | Default | Description |
|---|---|---|---|
file_path |
Path | str |
— | Path to file to write |
content |
str |
— | Text content to write |
encoding |
str |
'utf-8' |
Text encoding (default: 'utf-8') |
create_parents |
bool |
True |
Create parent directories if they don't exist |
caller |
str | None |
None |
Caller identifier for logging |
write_json
Write data as JSON file.
write_json
def write_json(file_path: Path | str, data: Any, indent: int | None = 2, create_parents: bool = True, caller: str | None = None) -> None
Write data as JSON file.
Parameters 5
| Name | Type | Default | Description |
|---|---|---|---|
file_path |
Path | str |
— | Path to JSON file |
data |
Any |
— | Data to serialize as JSON |
indent |
int | None |
2 |
JSON indentation (None for compact) |
create_parents |
bool |
True |
Create parent directories if needed |
caller |
str | None |
None |
Caller identifier for logging |
_remove_hidden_files
Remove macOS hidden files that may prevent directory deletion.
Targets .DS_Store, ._* files, and o…
_remove_hidden_files
def _remove_hidden_files(dir_path: Path) -> int
Remove macOS hidden files that may prevent directory deletion.
Targets .DS_Store, ._* files, and other dotfiles that macOS creates and can interfere with shutil.rmtree.
Parameters 1
| Name | Type | Default | Description |
|---|---|---|---|
dir_path |
Path |
— | Directory to clean hidden files from |
Returns
Number of hidden files removedint
—
rmtree_robust
Remove directory tree with robust error handling for filesystem quirks.
On macOS, shutil.rmtree ca…
rmtree_robust
def rmtree_robust(path: Path, max_retries: int = 3, caller: str | None = None) -> None
Remove directory tree with robust error handling for filesystem quirks.
On macOS, shutil.rmtree can fail with Errno 66 (Directory not empty) due to race conditions with Spotlight indexing, Finder metadata files (.DS_Store, ._*), or other processes briefly accessing the directory.
Strategy:
1. Try normal shutil.rmtree
2. On ENOTEMPTY, remove hidden files (.DS_Store, ._*) and retry
3. Fall back to subprocess `rm -rf` on macOS as last resort
Parameters 3
| Name | Type | Default | Description |
|---|---|---|---|
path |
Path |
— | Directory to remove |
max_retries |
int |
3 |
Number of retry attempts (default 3) |
caller |
str | None |
None |
Caller identifier for logging |