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

1 from .base import MakeConfig, DEFAULT_SERVER_CONFIG, SERVER_VALIDATION

2 from typing import Dict, Any

3

4 _global_config = None

5

6

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

8 global _global_config

9 _global_config = config

10

11

12 def 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

20 def 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

33 DEFAULT_CONFIG = MakeConfig(

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

35 validate={"server": validate_server_config},

36)