|
1 | 1 | """Configure pytask.""" |
2 | 2 | from __future__ import annotations |
3 | 3 |
|
| 4 | +import enum |
4 | 5 | import os |
5 | 6 | from typing import Any |
6 | | -from typing import Callable |
7 | 7 |
|
8 | 8 | from pytask import hookimpl |
9 | | -from pytask_parallel.backends import PARALLEL_BACKENDS_DEFAULT |
10 | | -from pytask_parallel.callbacks import n_workers_callback |
11 | | -from pytask_parallel.callbacks import parallel_backend_callback |
| 9 | +from pytask_parallel.backends import ParallelBackendChoices |
12 | 10 |
|
13 | 11 |
|
14 | 12 | @hookimpl |
15 | | -def pytask_parse_config( |
16 | | - config: dict[str, Any], |
17 | | - config_from_cli: dict[str, Any], |
18 | | - config_from_file: dict[str, Any], |
19 | | -) -> None: |
| 13 | +def pytask_parse_config(config: dict[str, Any]) -> None: |
20 | 14 | """Parse the configuration.""" |
21 | | - config["n_workers"] = _get_first_non_none_value( |
22 | | - config_from_cli, |
23 | | - config_from_file, |
24 | | - key="n_workers", |
25 | | - default=1, |
26 | | - callback=n_workers_callback, |
27 | | - ) |
28 | 15 | if config["n_workers"] == "auto": |
29 | 16 | config["n_workers"] = max(os.cpu_count() - 1, 1) |
30 | 17 |
|
31 | | - config["delay"] = 0.1 |
| 18 | + if ( |
| 19 | + isinstance(config["parallel_backend"], str) |
| 20 | + and config["parallel_backend"] in ParallelBackendChoices._value2member_map_ |
| 21 | + ): |
| 22 | + config["parallel_backend"] = ParallelBackendChoices(config["parallel_backend"]) |
| 23 | + elif ( |
| 24 | + isinstance(config["parallel_backend"], enum.Enum) |
| 25 | + and config["parallel_backend"] in ParallelBackendChoices |
| 26 | + ): |
| 27 | + pass |
| 28 | + else: |
| 29 | + raise ValueError("Invalid value for 'parallel_backend'.") |
32 | 30 |
|
33 | | - config["parallel_backend"] = _get_first_non_none_value( |
34 | | - config_from_cli, |
35 | | - config_from_file, |
36 | | - key="parallel_backend", |
37 | | - default=PARALLEL_BACKENDS_DEFAULT, |
38 | | - callback=parallel_backend_callback, |
39 | | - ) |
| 31 | + config["delay"] = 0.1 |
40 | 32 |
|
41 | 33 |
|
42 | 34 | @hookimpl |
43 | 35 | def pytask_post_parse(config: dict[str, Any]) -> None: |
44 | 36 | """Disable parallelization if debugging is enabled.""" |
45 | 37 | if config["pdb"] or config["trace"]: |
46 | 38 | config["n_workers"] = 1 |
47 | | - |
48 | | - |
49 | | -def _get_first_non_none_value( |
50 | | - *configs: dict[str, Any], |
51 | | - key: str, |
52 | | - default: Any | None = None, |
53 | | - callback: Callable[..., Any] | None = None, |
54 | | -) -> Any: |
55 | | - """Get the first non-None value for a key from a list of dictionaries. |
56 | | -
|
57 | | - This function allows to prioritize information from many configurations by changing |
58 | | - the order of the inputs while also providing a default. |
59 | | -
|
60 | | - """ |
61 | | - callback = (lambda x: x) if callback is None else callback # noqa: E731 |
62 | | - processed_values = (callback(config.get(key)) for config in configs) |
63 | | - return next((value for value in processed_values if value is not None), default) |
0 commit comments