Coverage for src/chrisjen/options.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-14 09:29 +0000

1"""Configuration settings and convenience functions for changing them. 

2 

3Contents: 

4 add_special_setting: adds a section of settings that is not a worker. 

5 is_special_setting: returns whether a section of settings is not a 

6 worker. 

7 set_compatibility_rule: sets the global compatibility rule. 

8 set_keyer: sets the global default function used to name dict keys. 

9 set_method_namer: sets the global default function used to name factory 

10 creation methods. 

11 set_overwrite_rule: sets the global attribute overwrite rule. 

12 set_verbose_rule: sets the global attribute message verbosity rule. 

13 

14""" 

15 

16from __future__ import annotations 

17 

18import pathlib 

19from collections.abc import Callable 

20from typing import Any 

21 

22import bobbie 

23import nagata 

24 

25from . import utilities 

26 

27_DEFAULT_CLERK: type[nagata.FileManager] = nagata.FileManager 

28_DEFAULT_DESIGN: str = 'flow' 

29_DEFAULT_IDEA: type[bobbie.Settings] = bobbie.Settings 

30_DEFAULT_OUTLINE: str = 'blueprint' 

31_DEFAULT_REPORT: str = 'summary' 

32_DEFAULT_ROOT: pathlib.Path = pathlib.Path('../project') 

33_DEFAULT_SETTINGS: type[bobbie.Settings] = bobbie.Settings 

34# Default naming function for non-str objects. 

35_KEY_NAMER: Callable[[Any], str | None] = utilities._namify 

36# Settings in the "general" section that reach every node with a field of the 

37# same name, unless the node has its own setting (see `Vertex.build`). Only 

38# these are passed on, so that a general setting meant for something else 

39# does not reach a class that happens to have a field with its name. A 

40# package built on `chrisjen` can add its own. 

41_GENERAL_SETTINGS: list[str] = ['conserve_memory'] 

42# Sections of settings that are not workers. 

43_SPECIAL_SETTINGS: list[str] = ['general', 'files'] 

44# Whether to return more elaborate error messages and feedback. 

45_VERBOSE: bool = False 

46 

47 

48def add_special_setting(name: str) -> None: 

49 """Adds a section of settings that is not a worker. 

50 

51 A package built on `chrisjen` that reads a section of its own (such as 

52 "feature_groups") adds it, so that the section is not built as a worker 

53 or taken as the project's section. Adding a section twice does nothing. 

54 

55 Args: 

56 name: name of the section. 

57 

58 Raises: 

59 TypeError: if `name` is not a `str`. 

60 

61 """ 

62 if not isinstance(name, str): 

63 message = f'the name of a section must be a str, not {name!r}' 

64 raise TypeError(message) 

65 if name not in _SPECIAL_SETTINGS: 

66 _SPECIAL_SETTINGS.append(name) 

67 

68 

69def is_special_setting(name: str) -> bool: 

70 """Returns whether a section of settings is not a worker. 

71 

72 Args: 

73 name: name of the section. 

74 

75 Returns: 

76 Whether `name` is "general", "files", or a section added with 

77 `add_special_setting`. 

78 

79 """ 

80 return name in _SPECIAL_SETTINGS 

81 

82 

83def set_compatibility_rule(compatibility: bool) -> None: 

84 """Sets the global attribute compatibility rule. 

85 

86 Args: 

87 compatibility: whether to use strict (`True`) or relaxed (`False`) 

88 validation. 

89 

90 Raises: 

91 TypeError: if `compatibility` is not `bool`. 

92 

93 """ 

94 if isinstance(compatibility, bool): 

95 globals()['_STRICT_COMPATIBILITY'] = compatibility 

96 else: 

97 raise TypeError('compatibility argument must be boolean') 

98 

99 

100def set_keyer(keyer: Callable[[object | type[Any]], str]) -> None: 

101 """Sets the global default function used to name `dict` keys. 

102 

103 Args: 

104 keyer: function that returns a `str` name of any item passed. 

105 

106 Raises: 

107 TypeError: if `keyer` is not callable. 

108 

109 """ 

110 if callable(keyer): 

111 globals()['_KEY_NAMER'] = keyer 

112 else: 

113 raise TypeError('keyer argument must be a callable') 

114 

115 

116def set_method_namer(namer: Callable[[object | type[Any]], str]) -> None: 

117 """Sets the global default function used to name factory creation methods. 

118 

119 Args: 

120 namer: function that returns a `str` name of any item passed. 

121 

122 Raises: 

123 TypeError: if `namer` is not callable. 

124 

125 """ 

126 if callable(namer): 

127 globals()['_METHOD_NAMER'] = namer 

128 else: 

129 raise TypeError('namer argument must be a callable') 

130 

131 

132def set_overwrite_rule(overwrite: bool) -> None: 

133 """Sets the global attribute overwrite rule. 

134 

135 Args: 

136 overwrite: whether to set the default rule to overwrite existing 

137 attributes. 

138 

139 Raises: 

140 TypeError: if `overwrite` is not bool. 

141 

142 """ 

143 if isinstance(overwrite, bool): 

144 globals()['_OVERWRITE'] = overwrite 

145 else: 

146 raise TypeError('overwrite argument must be boolean') 

147 

148 

149def set_verbose_rule(verbose: bool) -> None: 

150 """Sets the global attribute message verbosity rule. 

151 

152 Args: 

153 verbose: whether to set the default rule to verbosity in logging and 

154 messaging. 

155 

156 Raises: 

157 TypeError: if `verbose` is not bool. 

158 

159 """ 

160 if isinstance(verbose, bool): 

161 globals()['_VERBOSE'] = verbose 

162 else: 

163 raise TypeError('verbose argument must be boolean') 

164 

165 

166# @dataclasses.dataclass 

167# class _MISSING_VALUE(object): 

168# """Sentinel object for a missing data or parameter. 

169 

170# This follows the same pattern as the '__MISSING_TYPE` class in the builtin 

171# dataclasses library. 

172# https://github.com/python/cpython/blob/3.10/Lib/dataclasses.py#L182-L186 

173 

174# Because None is sometimes a valid argument or data option, this class 

175# provides an alternative that does not create the confusion that a 

176# default of None can sometimes lead to. 

177 

178# """ 

179# pass 

180 

181 

182# # _MISSING, instance of _MISSING_VALUE, should be used for missing values as 

183# # an alternative to None when None is a valid value for an argument. This 

184# # provides a fuller repr and traceback. 

185# _MISSING = _MISSING_VALUE()