Save new config values to dask config file, return new config and path to file.
(
key: str,
value: Any,
config_file: pathlib.Path | None = None,
)
| 112 | |
| 113 | |
| 114 | def save_config( |
| 115 | key: str, |
| 116 | value: Any, |
| 117 | config_file: pathlib.Path | None = None, |
| 118 | ) -> tuple[dict, pathlib.Path]: |
| 119 | """ |
| 120 | Save new config values to dask config file, |
| 121 | return new config and path to file. |
| 122 | """ |
| 123 | # If explicit config file wasn't supplied, try finding the first existing |
| 124 | # config file w/ this key, falling back to the default config path |
| 125 | if config_file is None: |
| 126 | config_file = next( |
| 127 | dask.config.paths_containing_key(key), |
| 128 | pathlib.Path(dask.config.PATH) / "dask.yaml", |
| 129 | ) |
| 130 | config_file.parent.mkdir(exist_ok=True, parents=True) |
| 131 | |
| 132 | if config_file.exists(): |
| 133 | config = yaml.safe_load(config_file.read_text()) or {} |
| 134 | else: |
| 135 | config = {} |
| 136 | |
| 137 | dask.config.set({key: value}, config=config) |
| 138 | try: |
| 139 | config_file.write_text(yaml.dump(config)) |
| 140 | except OSError as e: |
| 141 | raise RuntimeError(f""" |
| 142 | |
| 143 | For some reason we couldn't write config to {config_file}. |
| 144 | Perhaps you don't have permissions here? |
| 145 | |
| 146 | You can change the directory where Dask writes config files |
| 147 | to somewhere else using the DASK_CONFIG environment variable. |
| 148 | |
| 149 | For example, you could set the following: |
| 150 | |
| 151 | export DASK_CONFIG=~/.dask |
| 152 | """) from e |
| 153 | |
| 154 | return config, config_file |
| 155 | |
| 156 | |
| 157 | @config.command(name="list") |