vyra_base.helper package¶
Submodules¶
vyra_base.helper.env_handler module¶
vyra_base.helper.error_handler module¶
vyra_base.helper.file_lock module¶
- async vyra_base.helper.file_lock.get_lock_for_file(file_path)[source]¶
Get (or create) a lock for the specified file.
- async vyra_base.helper.file_lock.release_lock_for_file(file_path)[source]¶
Remove the lock from the dictionary if no tasks are waiting for it.
- Parameters:
file_path (
Union[Path,AsyncPath]) – The path to the file.
vyra_base.helper.file_reader module¶
- class vyra_base.helper.file_reader.FileReader[source]¶
Bases:
objectFile reader.
Reads file content from locally stored files into the module.
- async classmethod open_json_file(config_file, config_default=PosixPath('.'))[source]¶
Reads a JSON file.
To ensure cross platform compatibility, JSON files are opened with utf8-sig encoding.
- Parameters:
- Returns:
Parsed JSON content.
- Return type:
- Raises:
IOError – If an unexpected IO error occurs.
UnicodeDecodeError – If a decoding error occurs.
json.decoder.JSONDecodeError – If a JSON decoding error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- async classmethod open_markdown_file(config_file, config_default='')[source]¶
Reads a markdown file.
- Parameters:
- Returns:
File content.
- Return type:
- Raises:
IOError – If an unexpected IO error occurs.
UnicodeDecodeError – If a decoding error occurs.
json.decoder.JSONDecodeError – If a JSON decoding error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- async classmethod open_env_file(env_path)[source]¶
Reads an environment (.env) file.
- Parameters:
env_path (
Path) – Path to the directory containing the .env file.- Returns:
Dictionary of environment variables.
- Return type:
- Raises:
IOError – If an unexpected IO error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- async classmethod open_toml_file(config_file)[source]¶
Reads a TOML file.
- Parameters:
config_file (
Path) – TOML formatted file.- Returns:
Parsed TOML content as a dictionary.
- Return type:
- Raises:
ImportError – If tomli is not installed for Python < 3.11.
IOError – If an unexpected IO error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- async classmethod open_ini_file(config_file)[source]¶
Reads an INI file.
- Parameters:
config_file (
Path) – INI formatted file.- Returns:
Parsed INI content as a dictionary.
- Return type:
- Raises:
ImportError – If configparser is not available.
IOError – If an unexpected IO error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- async classmethod open_yaml_file(config_file)[source]¶
Reads a YAML file.
- Parameters:
config_file (
Path) – YAML formatted file.- Returns:
Parsed YAML content as a dictionary.
- Return type:
- Raises:
ImportError – If PyYAML is not installed.
IOError – If an unexpected IO error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- classmethod open_json_file_sync(config_file, config_default=PosixPath('.'))[source]¶
Reads a JSON file (synchronous).
- Parameters:
- Returns:
Parsed JSON content.
- Return type:
- Raises:
IOError – If an unexpected IO error occurs.
UnicodeDecodeError – If a decoding error occurs.
json.decoder.JSONDecodeError – If a JSON decoding error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- classmethod open_markdown_file_sync(config_file, config_default='')[source]¶
Reads a markdown file (synchronous).
- Parameters:
- Returns:
File content.
- Return type:
- Raises:
IOError – If an unexpected IO error occurs.
UnicodeDecodeError – If a decoding error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- classmethod open_toml_file_sync(config_file)[source]¶
Reads a TOML file (synchronous).
- Parameters:
config_file (
Path) – TOML formatted file.- Returns:
Parsed TOML content as a dictionary.
- Return type:
- Raises:
ImportError – If tomli is not installed for Python < 3.11.
IOError – If an unexpected IO error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- classmethod open_ini_file_sync(config_file)[source]¶
Reads an INI file (synchronous).
- Parameters:
config_file (
Path) – INI formatted file.- Returns:
Parsed INI content as a dictionary.
- Return type:
- Raises:
ImportError – If configparser is not available.
IOError – If an unexpected IO error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
- classmethod open_yaml_file_sync(config_file)[source]¶
Reads a YAML file (synchronous).
- Parameters:
config_file (
Path) – YAML formatted file.- Returns:
Parsed YAML content as a dictionary.
- Return type:
- Raises:
ImportError – If PyYAML is not installed.
IOError – If an unexpected IO error occurs.
TypeError – If an unexpected type error occurs.
FileNotFoundError – If the file is not found.
vyra_base.helper.file_writer module¶
- class vyra_base.helper.file_writer.FileWriter[source]¶
Bases:
objectFile writer.
Writes Python objects to a file with both async and sync methods.
- async classmethod write_json_file(file, file_content)[source]¶
Writes a JSON file from a dictionary (asynchronous).
- async classmethod write_binary_file(file, content)[source]¶
Writes binary content to a file (asynchronous).
- async classmethod write_yaml_file(file, file_content)[source]¶
Writes a YAML file from a dictionary (asynchronous).
- classmethod write_json_file_sync(file, file_content)[source]¶
Writes a JSON file from a dictionary (synchronous).
- classmethod write_binary_file_sync(file, content)[source]¶
Writes binary content to a file (synchronous).
vyra_base.helper.log_filter module¶
Module contents¶
Helper Utilities for VYRA Base
Collection of utility functions and helpers for various tasks.
- vyra_base.helper.update_ament_prefix_path(workspace_install_dir)[source]¶
Add workspace install directory to AMENT_PREFIX_PATH.
This makes ROS2 packages in the workspace discoverable to rosidl_runtime_py utilities like get_message(), get_service().
- Parameters:
workspace_install_dir (
str|Path) – Path to workspace install directory (e.g., “/workspace/install”)- Return type:
- Returns:
True if path was added, False if already present
Examples
>>> update_ament_prefix_path("/workspace/install") True
- vyra_base.helper.update_python_path(package_site_packages_dir, prepend=True)[source]¶
Add package site-packages directory to sys.path for Python imports.
This enables importing generated Python modules (*_pb2.py, ROS2 .py files) from newly built packages.
- Parameters:
- Return type:
- Returns:
True if path was added, False if already present
Examples
>>> update_python_path("/workspace/install/mypackage/lib/python3.12/site-packages") True
- vyra_base.helper.detect_python_version()[source]¶
Detect current Python version (e.g., “3.12”).
- Return type:
- Returns:
Python version string in format “X.Y”
- vyra_base.helper.find_package_site_packages(workspace_install_dir, package_name, python_version=None)[source]¶
Find site-packages directory for a package in workspace.
Searches for typical ROS2 package structure: <workspace>/install/<package>/lib/python<version>/site-packages
- Parameters:
- Return type:
- Returns:
Path to site-packages if found, None otherwise
Examples
>>> find_package_site_packages("/workspace/install", "v2_modulemanager_interfaces") Path('/workspace/install/v2_modulemanager_interfaces/lib/python3.12/site-packages')
- vyra_base.helper.ensure_interface_package_discoverable(workspace_install_dir, package_name, python_version=None)[source]¶
Make an interface package discoverable to running Python process.
Updates both AMENT_PREFIX_PATH (for ROS2 introspection) and sys.path (for Python imports of generated modules).
- Parameters:
- Return type:
- Returns:
True if package is now discoverable, False on failure
Examples
>>> ensure_interface_package_discoverable( ... "/workspace/install", ... "v2_modulemanager_interfaces" ... ) True
- vyra_base.helper.ensure_workspace_discoverable(workspace_install_dir, python_version=None)[source]¶
Make all packages in a workspace install directory discoverable.
Scans workspace install directory and updates paths for all found packages.
- Parameters:
- Return type:
- Returns:
Number of packages made discoverable
Examples
>>> ensure_workspace_discoverable("/workspace/install") 5 # Made 5 packages discoverable
- vyra_base.helper.get_current_paths_info()[source]¶
Get current AMENT_PREFIX_PATH and sys.path for debugging.
- class vyra_base.helper.VyraLoggingConfig[source]¶
Bases:
objectCentralized logging configuration for vyra_base library.
This class manages the logging configuration for the entire vyra_base library, providing consistent logging across all modules and components.
- classmethod initialize(log_level=None, log_directory=None, log_config=None, enable_console=True, enable_file=True, enable_colors=True, enable_json=False, rate_limit=None)[source]¶
Initialize the logging configuration.
- Parameters:
log_level (
Optional[str]) – Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL). Can also be set via VYRA_LOG_LEVEL environment variable.log_directory (
Optional[Path]) – Directory where log files will be stored.log_config (
Optional[Dict[str,Any]]) – Optional custom logging configuration dict.enable_console (
bool) – Whether to enable console logging.enable_file (
bool) – Whether to enable file logging.enable_colors (
bool) – Whether to use colored output (console only).enable_json (
bool) – Whether to use JSON formatting (file only).rate_limit (
Optional[float]) – Minimum seconds between duplicate messages (None = disabled).
- Return type:
- classmethod get_logger(name)[source]¶
Get a logger instance.
Ensures logging is initialized before returning the logger.
- vyra_base.helper.get_logger(name)[source]¶
Convenience function to get a logger.
- Parameters:
name (
str) – Logger name (typically __name__).- Return type:
- Returns:
Logger instance.
Example
>>> logger = get_logger(__name__) >>> logger.info("Application started")
- vyra_base.helper.deep_merge(d1, d2)[source]¶
Recursively merges two dictionaries, combining nested dictionaries. If a key exists in both dictionaries and both values are dictionaries, they are merged recursively. Otherwise, the value from the second dictionary overwrites the value from the first dictionary. :type d1:
dict:param d1: The first dictionary. :type d2:dict:param d2: The second dictionary.
- vyra_base.helper.fuzzy_match(word, possibilities, n=3, cutoff=0.6)[source]¶
Find the closest matches to word from a list of possibilities.
Uses
difflib.get_close_matches()under the hood, providing a convenient wrapper with sensible defaults for industrial automation diagnostics (e.g. suggesting valid interface names when a lookup fails).- Parameters:
word (
str) – The string to match against the possibilities.possibilities (
Sequence[str]) – The pool of candidate strings to search in.n (
int) – Maximum number of close matches to return. Defaults to 3.cutoff (
float) – Minimum similarity score in the range[0, 1]. Strings with a ratio below this threshold are excluded. Defaults to 0.6.
- Returns:
A list of the best matches (may be empty if nothing is close enough), sorted by decreasing similarity.
- Return type:
Usage example:
from vyra_base.helper.func import fuzzy_match candidates = ["state_feeder", "news_feeder", "error_feeder"] suggestions = fuzzy_match("stae_feeder", candidates) # → ["state_feeder"]