Compose a component list from a :ref:`component priority dictionary <component-priority-dictionaries>`.
(
compdict: MutableMapping[Any, Any],
*,
convert: Callable[[Any], Any] = update_classpath,
)
| 18 | |
| 19 | |
| 20 | def build_component_list( |
| 21 | compdict: MutableMapping[Any, Any], |
| 22 | *, |
| 23 | convert: Callable[[Any], Any] = update_classpath, |
| 24 | ) -> list[Any]: |
| 25 | """Compose a component list from a :ref:`component priority dictionary |
| 26 | <component-priority-dictionaries>`.""" |
| 27 | |
| 28 | def _check_components(complist: Collection[Any]) -> None: |
| 29 | if len({convert(c) for c in complist}) != len(complist): |
| 30 | raise ValueError( |
| 31 | f"Some paths in {complist!r} convert to the same object, " |
| 32 | "please update your settings" |
| 33 | ) |
| 34 | |
| 35 | def _map_keys(compdict: Mapping[Any, Any]) -> BaseSettings | dict[Any, Any]: |
| 36 | if isinstance(compdict, BaseSettings): |
| 37 | compbs = BaseSettings() |
| 38 | for k, v in compdict.items(): |
| 39 | prio = compdict.getpriority(k) |
| 40 | assert prio is not None |
| 41 | if compbs.getpriority(convert(k)) == prio: |
| 42 | raise ValueError( |
| 43 | f"Some paths in {list(compdict.keys())!r} " |
| 44 | "convert to the same " |
| 45 | "object, please update your settings" |
| 46 | ) |
| 47 | compbs.set(convert(k), v, priority=prio) |
| 48 | return compbs |
| 49 | _check_components(compdict) |
| 50 | return {convert(k): v for k, v in compdict.items()} |
| 51 | |
| 52 | def _validate_values(compdict: Mapping[Any, Any]) -> None: |
| 53 | """Fail if a value in the components dict is not a real number or None.""" |
| 54 | for name, value in compdict.items(): |
| 55 | if value is not None and not isinstance(value, numbers.Real): |
| 56 | raise ValueError( |
| 57 | f"Invalid value {value} for component {name}, " |
| 58 | "please provide a real number or None instead" |
| 59 | ) |
| 60 | |
| 61 | _validate_values(compdict) |
| 62 | compdict = without_none_values(_map_keys(compdict)) |
| 63 | return [k for k, v in sorted(compdict.items(), key=itemgetter(1))] |
| 64 | |
| 65 | |
| 66 | def arglist_to_dict(arglist: list[str]) -> dict[str, str]: |