Utilities¶

r2x_core.utils.filter_valid_kwargs(func, *, kwargs)[source]

Filter function kwargs, maintaining backward compatibility.

Parameters:
  • func (Callable[[...], Any])

  • kwargs (dict[str, Any])

Return type:

dict[str, Any]

r2x_core.utils.filter_kwargs_by_signatures(kwargs, *, callables)[source]

Filter kwargs to those accepted by the provided callables.

Parameters:
  • kwargs (dict[str, Any])

  • callables (list[Callable[[...], Any]])

Return type:

dict[str, Any]

r2x_core.utils.validate_file_extension(path, *, info)[source]

Validate that the file path has a supported extension.

This is a Pydantic validator that checks if the file extension from the provided path exists as a key in the module-level EXTENSION_MAPPING.

Parameters:
  • value (str) – The file path string to validate, provided by Pydantic.

  • info (pydantic.ValidationInfo) – Pydantic’s validation context. Required by the validator signature but not used in this function.

  • path (Path)

Returns:

The original file path string if its extension is valid.

Return type:

Path

Raises:
  • AssertionError – If the input value is not a string.

  • ValueError – If the file path has no extension.

  • KeyError – If the file’s extension is not found in EXTENSION_MAPPING.

Notes

This function is intended for use as a Pydantic model validator (e.g., with @field_validator or AfterValidator) and should not be called directly.

r2x_core.utils.validate_glob_pattern(pattern)[source]

Validate that a string is a valid glob pattern.

Parameters:

pattern (str | None) – The glob pattern to validate

Returns:

The validated pattern

Return type:

str | None

Raises:

ValueError – If the pattern is invalid (empty, only whitespace, or contains invalid characters)

r2x_core.utils.resolve_glob_pattern(pattern, *, search_dir)[source]

Resolve a glob pattern to a single file path.

Parameters:
  • pattern (str) – The glob pattern to resolve.

  • search_dir (Path) – The directory to search for matching files.

Return type:

Result[Path, ValueError | FileNotFoundError]

r2x_core.utils.override_dictionary(base, *, overrides)[source]

Merge dict overrides with a base dict, maintaining backward compatibility.

Parameters:
  • base (dict[str, Any])

  • overrides (dict[str, Any])

Return type:

dict[str, Any]

r2x_core.utils.audit_file(fpath)[source]

Check if a path exists and return it, or return an error.

Parameters:

fpath (Path)

Return type:

Result[Path, ValueError | FileNotFoundError]

r2x_core.utils.backup_folder(folder_path)[source]

Backup a folder by moving then copying back.

Parameters:

folder_path (Path | str)

Return type:

Result[None, str]