|
23 | 23 |
|
24 | 24 | import IPython
|
25 | 25 | import traitlets.log
|
| 26 | +from IPython.core.profiledir import ProfileDir |
26 | 27 | from traitlets import Any
|
27 | 28 | from traitlets import Bool
|
28 | 29 | from traitlets import default
|
29 | 30 | from traitlets import Dict
|
30 | 31 | from traitlets import Float
|
31 | 32 | from traitlets import import_item
|
| 33 | +from traitlets import Instance |
32 | 34 | from traitlets import Integer
|
33 | 35 | from traitlets import List
|
34 | 36 | from traitlets import Unicode
|
| 37 | +from traitlets import validate |
| 38 | +from traitlets.config import Config |
35 | 39 | from traitlets.config import LoggingConfigurable
|
36 | 40 |
|
37 | 41 | from . import launcher
|
@@ -267,14 +271,71 @@ def _default_log(self):
|
267 | 271 | else:
|
268 | 272 | return traitlets.log.get_logger()
|
269 | 273 |
|
| 274 | + load_profile = Bool( |
| 275 | + True, |
| 276 | + config=True, |
| 277 | + help=""" |
| 278 | + If True (default) load ipcluster config from profile directory, if present. |
| 279 | + """, |
| 280 | + ) |
270 | 281 | # private state
|
271 | 282 | controller = Any()
|
272 | 283 | engines = Dict()
|
273 | 284 |
|
| 285 | + profile_config = Instance(Config, allow_none=False) |
| 286 | + |
| 287 | + @default("profile_config") |
| 288 | + def _profile_config_default(self): |
| 289 | + """Load config from our profile""" |
| 290 | + if not self.load_profile or not os.path.isdir(self.profile_dir): |
| 291 | + # no profile dir, nothing to load |
| 292 | + return Config() |
| 293 | + |
| 294 | + from .app import BaseParallelApplication, IPClusterStart |
| 295 | + |
| 296 | + if ( |
| 297 | + self.parent |
| 298 | + and isinstance(self.parent, BaseParallelApplication) |
| 299 | + and self.parent.name == 'ipcluster' |
| 300 | + and self.parent.profile_dir.location == self.profile_dir |
| 301 | + ): |
| 302 | + # profile config already loaded by parent, nothing new to load |
| 303 | + return Config() |
| 304 | + |
| 305 | + self.log.debug(f"Loading profile {self.profile_dir}") |
| 306 | + # set profile dir via config |
| 307 | + self.config.ProfileDir.location = self.profile_dir |
| 308 | + |
| 309 | + # load profile config via IPCluster |
| 310 | + app = IPClusterStart(parent=self, log_level=10) |
| 311 | + # adds profile dir to config_files_path |
| 312 | + app.init_profile_dir() |
| 313 | + # adds system to config_files_path |
| 314 | + app.init_config_files() |
| 315 | + # actually load the config |
| 316 | + app.load_config_file(suppress_errors=False) |
| 317 | + return app.config |
| 318 | + |
| 319 | + @validate("config") |
| 320 | + def _merge_profile_config(self, proposal): |
| 321 | + direct_config = proposal.value |
| 322 | + if not self.load_profile: |
| 323 | + return direct_config |
| 324 | + profile_config = self.profile_config |
| 325 | + if not profile_config: |
| 326 | + return direct_config |
| 327 | + # priority ?! direct > profile |
| 328 | + config = Config() |
| 329 | + if profile_config: |
| 330 | + config.merge(profile_config) |
| 331 | + config.merge(direct_config) |
| 332 | + return config |
| 333 | + |
274 | 334 | def __init__(self, **kwargs):
|
275 | 335 | """Construct a Cluster"""
|
276 | 336 | if 'parent' not in kwargs and 'config' not in kwargs:
|
277 | 337 | kwargs['parent'] = self._default_parent()
|
| 338 | + |
278 | 339 | super().__init__(**kwargs)
|
279 | 340 |
|
280 | 341 | def __del__(self):
|
|
0 commit comments