__init__.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. """
  2. Trixy Configuration Management System
  3. This module provides comprehensive configuration management for the Trixy application
  4. across all deployment modes (server, client, standalone). It includes:
  5. - Type-safe configuration data structures
  6. - Environment variable and command line override support
  7. - Configuration validation with detailed error reporting
  8. - Hot-reloading of configuration files during runtime
  9. - Thread-safe configuration access for multi-satellite support
  10. - Configuration backup and restore functionality
  11. - Default configuration generation
  12. Public API:
  13. ConfigManager: Main configuration management class
  14. ServerConfig, ClientConfig, StandaloneConfig: Configuration data models
  15. ConfigValidator: Configuration validation utilities
  16. ValidationResult: Validation result container
  17. Example usage:
  18. from trixy_core.config import ConfigManager, DeploymentMode
  19. # Initialize configuration manager
  20. config_manager = ConfigManager(
  21. mode=DeploymentMode.SERVER,
  22. config_file="config/server_config.json",
  23. debug_mode=False
  24. )
  25. # Load configuration with validation
  26. config = config_manager.load_config()
  27. # Access configuration values
  28. command_port = config.network.command_port
  29. # Enable hot-reloading
  30. config_manager.enable_hot_reload()
  31. # Override configuration values
  32. config_manager.override_config({"network": {"command_port": 2105}})
  33. """
  34. from pprint import pprint as _pprint
  35. # Configuration data models
  36. from .config_models import (
  37. # Main configuration classes
  38. ServerConfig,
  39. ClientConfig,
  40. StandaloneConfig,
  41. ConfigType,
  42. # Component configuration classes
  43. NetworkConfig,
  44. AudioConfig,
  45. MLConfig,
  46. PluginConfig,
  47. SatelliteConfig,
  48. SecurityConfig,
  49. LoggingConfig,
  50. # Enums and types
  51. DeploymentMode,
  52. LogLevel,
  53. ConfigDict,
  54. # Utility functions
  55. get_config_class,
  56. create_default_config,
  57. merge_configs,
  58. CONFIG_CLASSES,
  59. )
  60. # Default configuration factories
  61. from .config_defaults import (
  62. # Factory functions
  63. create_default_server_config,
  64. create_default_client_config,
  65. create_default_standalone_config,
  66. create_default_config_for_mode,
  67. # Environment handling
  68. get_env_override,
  69. get_environment_overrides,
  70. apply_environment_overrides,
  71. get_env_var_documentation,
  72. # System utilities
  73. get_system_defaults,
  74. get_config_template,
  75. validate_environment,
  76. # Constants
  77. ENV_PREFIX,
  78. DEFAULT_PATHS,
  79. DEFAULT_PORTS,
  80. )
  81. # Configuration validation
  82. from .config_validator import (
  83. ConfigValidator,
  84. ValidationResult,
  85. validate_config_dict,
  86. quick_validate,
  87. )
  88. # Main configuration manager (import after other modules to avoid circular imports)
  89. from .config_manager import (
  90. ConfigManager,
  91. ConfigurationError,
  92. ConfigurationValidationError,
  93. ConfigurationLoadError,
  94. ConfigurationSaveError,
  95. )
  96. def pprint(message: str) -> None:
  97. """
  98. Print function that adapts based on application mode.
  99. In production mode: Uses logging system
  100. In debug mode: Uses print() statements
  101. This is the standard logging function referenced in CLAUDE.md.
  102. """
  103. # For now, use standard pprint - will be enhanced when logging system is implemented
  104. _pprint(f"[CONFIG] {message}")
  105. # Public API exports
  106. __all__ = [
  107. # Main classes
  108. "ConfigManager",
  109. "ConfigValidator",
  110. "ValidationResult",
  111. # Configuration models
  112. "ServerConfig",
  113. "ClientConfig",
  114. "StandaloneConfig",
  115. "ConfigType",
  116. "NetworkConfig",
  117. "AudioConfig",
  118. "MLConfig",
  119. "PluginConfig",
  120. "SatelliteConfig",
  121. "SecurityConfig",
  122. "LoggingConfig",
  123. # Enums and types
  124. "DeploymentMode",
  125. "LogLevel",
  126. "ConfigDict",
  127. # Exceptions
  128. "ConfigurationError",
  129. "ConfigurationValidationError",
  130. "ConfigurationLoadError",
  131. "ConfigurationSaveError",
  132. # Utility functions
  133. "get_config_class",
  134. "create_default_config",
  135. "merge_configs",
  136. "create_default_config_for_mode",
  137. "get_env_override",
  138. "get_environment_overrides",
  139. "apply_environment_overrides",
  140. "get_system_defaults",
  141. "get_config_template",
  142. "validate_environment",
  143. "validate_config_dict",
  144. "quick_validate",
  145. "get_env_var_documentation",
  146. "pprint",
  147. # Constants
  148. "CONFIG_CLASSES",
  149. "ENV_PREFIX",
  150. "DEFAULT_PATHS",
  151. "DEFAULT_PORTS",
  152. ]
  153. # Version information
  154. __version__ = "1.0.0"
  155. __author__ = "Trixy Development Team"
  156. __description__ = "Professional configuration management system for Trixy voice assistant"
  157. def get_version() -> str:
  158. """Get the configuration module version."""
  159. return __version__
  160. def create_config_manager(
  161. mode: DeploymentMode,
  162. config_file: str = None,
  163. debug_mode: bool = False,
  164. **kwargs
  165. ) -> ConfigManager:
  166. """
  167. Convenience function to create a ConfigManager instance.
  168. Args:
  169. mode: Deployment mode (server, client, standalone)
  170. config_file: Path to configuration file (optional)
  171. debug_mode: Enable debug mode
  172. **kwargs: Additional arguments passed to ConfigManager
  173. Returns:
  174. Configured ConfigManager instance
  175. Example:
  176. config_manager = create_config_manager(
  177. mode=DeploymentMode.SERVER,
  178. config_file="config/server_config.json",
  179. debug_mode=True
  180. )
  181. """
  182. return ConfigManager(
  183. mode=mode,
  184. config_file=config_file,
  185. debug_mode=debug_mode,
  186. **kwargs
  187. )
  188. def load_config_from_file(
  189. config_file: str,
  190. validate: bool = True,
  191. strict_validation: bool = False
  192. ) -> ConfigType:
  193. """
  194. Convenience function to load configuration from a file.
  195. Args:
  196. config_file: Path to configuration file
  197. validate: Whether to validate the configuration
  198. strict_validation: Whether to treat warnings as errors
  199. Returns:
  200. Loaded and validated configuration object
  201. Raises:
  202. ConfigurationError: If loading or validation fails
  203. Example:
  204. config = load_config_from_file("config/server_config.json")
  205. """
  206. config_manager = ConfigManager.from_file(config_file)
  207. return config_manager.load_config(validate=validate, strict_validation=strict_validation)
  208. def validate_config_file(config_file: str, strict: bool = False) -> ValidationResult:
  209. """
  210. Convenience function to validate a configuration file.
  211. Args:
  212. config_file: Path to configuration file
  213. strict: Whether to treat warnings as errors
  214. Returns:
  215. ValidationResult with detailed feedback
  216. Example:
  217. result = validate_config_file("config/server_config.json")
  218. if not result.is_valid:
  219. print("Configuration errors:")
  220. for error in result.errors:
  221. print(f" - {error}")
  222. """
  223. validator = ConfigValidator(strict_mode=strict)
  224. return quick_validate(config_file)
  225. def create_default_config_file(
  226. mode: DeploymentMode,
  227. output_file: str,
  228. debug_mode: bool = False,
  229. overrides: dict = None
  230. ) -> None:
  231. """
  232. Create a default configuration file for the specified mode.
  233. Args:
  234. mode: Deployment mode
  235. output_file: Path where to save the configuration file
  236. debug_mode: Whether to enable debug mode in the default config
  237. overrides: Optional configuration overrides to apply
  238. Example:
  239. create_default_config_file(
  240. mode=DeploymentMode.SERVER,
  241. output_file="config/server_config.json",
  242. debug_mode=False
  243. )
  244. """
  245. config_manager = ConfigManager(mode=mode, debug_mode=debug_mode)
  246. default_config = config_manager.get_default_config()
  247. if overrides:
  248. default_config = merge_configs(default_config, overrides)
  249. config_manager.save_config_to_file(default_config, output_file)
  250. pprint(f"Created default {mode.value} configuration: {output_file}")