Coverage for nexios\config\__init__.py: 82%

17 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-21 20:31 +0100

1from .base import MakeConfig, DEFAULT_SERVER_CONFIG, SERVER_VALIDATION 

2from typing import Dict, Any 

3 

4_global_config = None 

5 

6 

7def set_config(config: MakeConfig) -> None: 

8 global _global_config 

9 _global_config = config 

10 

11 

12def get_config() -> MakeConfig: 

13 if _global_config is None: 

14 raise RuntimeError("Configuration has not been initialized.") 

15 # return {} 

16 return _global_config 

17 

18 

19# Apply server validation to nested server configuration 

20def validate_server_config(config: Dict[str, Any]) -> bool: 

21 """Validate server configuration structure and values.""" 

22 if not isinstance(config, dict): 

23 return False 

24 

25 # Check each key in the server config 

26 for key, value in config.items(): 

27 if key in SERVER_VALIDATION and not SERVER_VALIDATION[key](value): 

28 return False 

29 

30 return True 

31 

32 

33DEFAULT_CONFIG = MakeConfig( 

34 {"debug": True, "server": DEFAULT_SERVER_CONFIG}, 

35 validate={"server": validate_server_config}, 

36)