Get a setting value as either a :class:`dict` or a :class:`list`. If the setting is already a dict or a list, a copy of it will be returned. If it is a string it will be evaluated as JSON, or as a comma-separated list of strings as a fallback. For example,
(
self,
name: _SettingsKey,
default: dict[Any, Any] | list[Any] | tuple[Any] | None = None,
)
| 274 | return dict(value) |
| 275 | |
| 276 | def getdictorlist( |
| 277 | self, |
| 278 | name: _SettingsKey, |
| 279 | default: dict[Any, Any] | list[Any] | tuple[Any] | None = None, |
| 280 | ) -> dict[Any, Any] | list[Any]: |
| 281 | """Get a setting value as either a :class:`dict` or a :class:`list`. |
| 282 | |
| 283 | If the setting is already a dict or a list, a copy of it will be |
| 284 | returned. |
| 285 | |
| 286 | If it is a string it will be evaluated as JSON, or as a comma-separated |
| 287 | list of strings as a fallback. |
| 288 | |
| 289 | For example, settings populated from the command line will return: |
| 290 | |
| 291 | - ``{'key1': 'value1', 'key2': 'value2'}`` if set to |
| 292 | ``'{"key1": "value1", "key2": "value2"}'`` |
| 293 | |
| 294 | - ``['one', 'two']`` if set to ``'["one", "two"]'`` or ``'one,two'`` |
| 295 | |
| 296 | :param name: the setting name |
| 297 | :type name: string |
| 298 | |
| 299 | :param default: the value to return if no setting is found |
| 300 | :type default: any |
| 301 | """ |
| 302 | value = self.get(name, default) |
| 303 | if value is None: |
| 304 | return {} |
| 305 | if isinstance(value, str): |
| 306 | try: |
| 307 | value_loaded = json.loads(value) |
| 308 | if not isinstance(value_loaded, (dict, list)): |
| 309 | raise ValueError( |
| 310 | f"JSON string for setting '{name}' must evaluate to a dict or list, " |
| 311 | f"got {type(value_loaded).__name__}: {value_loaded!r}" |
| 312 | ) |
| 313 | return value_loaded |
| 314 | except ValueError: |
| 315 | return value.split(",") |
| 316 | if isinstance(value, tuple): |
| 317 | return list(value) |
| 318 | if not isinstance(value, (dict, list)): |
| 319 | raise ValueError( |
| 320 | f"Setting '{name}' must be a dict, list, tuple, or string, " |
| 321 | f"got {type(value).__name__}: {value!r}" |
| 322 | ) |
| 323 | return copy.deepcopy(value) |
| 324 | |
| 325 | def getwithbase(self, name: _SettingsKey) -> BaseSettings: |
| 326 | """Get a composition of a dictionary-like setting and its ``_BASE`` |
no test coverage detected