Coverage for .nox/test-3-12/lib/python3.12/site-packages/nskit/common/configuration/__init__.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.3.3, created at 2023-12-19 17:42 +0000

1"""Base configuration class. 

2 

3Includes: 

4 - properties in model dumps 

5 - file based config loading (json/toml/yaml) 

6 - model dumping to toml & yaml 

7""" 

8from __future__ import annotations 

9 

10from typing import Any 

11 

12from pydantic_settings import BaseSettings as _BaseSettings 

13from pydantic_settings import PydanticBaseSettingsSource as _PydanticBaseSettingsSource 

14from pydantic_settings import SettingsConfigDict as _SettingsConfigDict 

15 

16from nskit.common.configuration.mixins import PropertyDumpMixin 

17from nskit.common.configuration.sources import FileConfigSettingsSource 

18from nskit.common.io import json, toml, yaml 

19 

20 

21class BaseConfiguration(PropertyDumpMixin, _BaseSettings): 

22 """A Pydantic BaseSettings type object with Properties included in model dump, and yaml and toml integrations.""" 

23 

24 model_config = _SettingsConfigDict(env_file_encoding='utf-8') 

25 

26 @classmethod 

27 def settings_customise_sources( 

28 cls, 

29 settings_cls: type[_BaseSettings], 

30 init_settings: _PydanticBaseSettingsSource, 

31 env_settings: _PydanticBaseSettingsSource, 

32 dotenv_settings: _PydanticBaseSettingsSource, 

33 file_secret_settings: _PydanticBaseSettingsSource, 

34 ) -> tuple[_PydanticBaseSettingsSource, ...]: 

35 """Create settings loading, including the FileConfigSettingsSource.""" 

36 # TODO This probably needs a tweak to handle complex structures. 

37 return ( 

38 FileConfigSettingsSource(settings_cls), 

39 init_settings, 

40 env_settings, 

41 dotenv_settings, 

42 file_secret_settings, 

43 ) 

44 

45 def model_dump_toml( 

46 self, 

47 *, 

48 indent: int | None = None, 

49 include: Any = None, 

50 exclude: Any = None, 

51 by_alias: bool = False, 

52 exclude_unset: bool = False, 

53 exclude_defaults: bool = False, 

54 exclude_none: bool = False, 

55 round_trip: bool = False, 

56 warnings: bool = True): 

57 """Dump model to TOML.""" 

58 # We go via JSON to include indent etc. 

59 return toml.dumps(json.loads(self.model_dump_json( 

60 indent=indent, 

61 include=include, 

62 exclude=exclude, 

63 by_alias=by_alias, 

64 exclude_unset=exclude_unset, 

65 exclude_defaults=exclude_defaults, 

66 exclude_none=exclude_none, 

67 round_trip=round_trip, 

68 warnings=warnings 

69 ))) 

70 

71 def model_dump_yaml( 

72 self, 

73 *, 

74 indent: int | None = None, 

75 include: Any = None, 

76 exclude: Any = None, 

77 by_alias: bool = False, 

78 exclude_unset: bool = False, 

79 exclude_defaults: bool = False, 

80 exclude_none: bool = False, 

81 round_trip: bool = False, 

82 warnings: bool = True): 

83 """Dump model to YAML.""" 

84 # We go via JSON to include indent etc. 

85 return yaml.dumps(json.loads(self.model_dump_json( 

86 indent=indent, 

87 include=include, 

88 exclude=exclude, 

89 by_alias=by_alias, 

90 exclude_unset=exclude_unset, 

91 exclude_defaults=exclude_defaults, 

92 exclude_none=exclude_none, 

93 round_trip=round_trip, 

94 warnings=warnings 

95 )))