ATLAS Offline Software
Loading...
Searching...
No Matches
python.ConfigText Namespace Reference

Classes

class  TextConfig
class  TextConfigWarning

Functions

 readYaml (yamlPath)
 printYaml (d, sort=False, jsonFormat=False)
 makeSequence (configPath, *, flags=None, algSeq=None, noSystematics=None, dataType=None, geometry=None, autoconfigFromFlags=None, isPhyslite=None, noPhysliteBroken=False)
 combineConfigFiles (local, config_path, fragment_key="include")
 _load_fragment (pathlib.Path fragment_path)
 _find_fragment (fragment_path, config_path)
 _merge_dicts (local, fragment)

Variables

 logCPAlgTextCfg = logging.getLogger('CPAlgTextCfg')

Function Documentation

◆ _find_fragment()

python.ConfigText._find_fragment ( fragment_path,
config_path )
protected

Definition at line 467 of file ConfigText.py.

467def _find_fragment(fragment_path, config_path):
468 paths_to_check = [
469 fragment_path,
470 config_path / fragment_path,
471 *[x / fragment_path for x in os.environ["DATAPATH"].split(":")]
472 ]
473 for path in paths_to_check:
474 if path.exists():
475 return path
476
477 raise FileNotFoundError(fragment_path)
478
479
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177

◆ _load_fragment()

python.ConfigText._load_fragment ( pathlib.Path fragment_path)
protected
Load a YAML or JSON fragment

This function is superfluous as of the yaml 1.2 spec (which
has not been implemented in ATLAS Yaml dependencies).
Once https://github.com/yaml/pyyaml/issues/173 is resolved
pyyaml will support yaml 1.2, which is compatable with json. 
Until then yaml and json behave differently in some scientific
notation edge cases.

Definition at line 450 of file ConfigText.py.

450def _load_fragment(fragment_path: pathlib.Path):
451 """Load a YAML or JSON fragment
452
453 This function is superfluous as of the yaml 1.2 spec (which
454 has not been implemented in ATLAS Yaml dependencies).
455 Once https://github.com/yaml/pyyaml/issues/173 is resolved
456 pyyaml will support yaml 1.2, which is compatable with json.
457 Until then yaml and json behave differently in some scientific
458 notation edge cases.
459 """
460
461 with open(fragment_path, 'r') as fragment_file:
462 if fragment_path.suffix.lower() == '.json':
463 return json.load(fragment_file)
464 else:
465 return yaml.safe_load(fragment_file)
466

◆ _merge_dicts()

python.ConfigText._merge_dicts ( local,
fragment )
protected

Definition at line 480 of file ConfigText.py.

480def _merge_dicts(local, fragment):
481 # In the list case append the fragment to the local list
482 if isinstance(local, list):
483 local += fragment
484 return
485 # In the dict case, append only missing values to local: the local
486 # values take precedence over the fragment ones.
487 if isinstance(local, dict):
488 for key, value in fragment.items():
489 if key in local:
490 _merge_dicts(local[key], value)
491 else:
492 local[key] = value
493 return

◆ combineConfigFiles()

python.ConfigText.combineConfigFiles ( local,
config_path,
fragment_key = "include" )
Recursively combine configuration fragments into `local`.

- Looks for `fragment_key` at any dict node.
- If value is a string/path: merge that fragment.
- If value is a list: merge all fragments in order.
  For conflicts between fragments, the **earlier** file in the list wins. 
  Local keys still override the merged fragments.

Returns True if any merging happened below this node.

Definition at line 381 of file ConfigText.py.

381def combineConfigFiles(local, config_path, fragment_key="include"):
382 """
383 Recursively combine configuration fragments into `local`.
384
385 - Looks for `fragment_key` at any dict node.
386 - If value is a string/path: merge that fragment.
387 - If value is a list: merge all fragments in order.
388 For conflicts between fragments, the **earlier** file in the list wins.
389 Local keys still override the merged fragments.
390
391 Returns True if any merging happened below this node.
392 """
393 combined = False
394
395 # If this isn't an iterable there's nothing to combine
396 if isinstance(local, dict):
397 to_combine = local.values()
398 elif isinstance(local, list):
399 to_combine = local
400 else:
401 return combined
402
403 # Recurse first so that nested nodes are resolved
404 for sub in to_combine:
405 combined = combineConfigFiles(sub, config_path, fragment_key=fragment_key) or combined
406
407 # if there are no fragments to include we're done
408 if fragment_key not in local:
409 return combined
410
411 # Only dict nodes can have include keys
412 if not isinstance(local, dict):
413 return combined
414
415 # Normalize to a list of paths
416 value = local[fragment_key]
417 if isinstance(value, (str, pathlib.Path)):
418 warnings.warn(
419 f"{fragment_key} should be followed with a list of files",
420 TextConfigWarning,
421 stacklevel=2,
422 )
423 paths = [value]
424 elif isinstance(value, list):
425 paths = value
426 else:
427 raise TypeError(f"'{fragment_key}' must be a string path or a list of paths, got {type(value).__name__}")
428
429 # Build an accumulator of all fragments, earlier paths win on conflicts
430 fragments_acc = {}
431 for entry in paths:
432 fragment_path = _find_fragment(pathlib.Path(entry), config_path)
433 fragment = _load_fragment(fragment_path)
434
435 # Allow recursion inside each fragment, using the fragment's directory as base
436 combineConfigFiles(fragment, fragment_path.parent, fragment_key=fragment_key)
437
438 # Merge this fragment into the accumulator; earlier entries win
439 _merge_dicts(fragments_acc, fragment)
440
441 # Remove the key before merging to avoid re-processing it
442 del local[fragment_key]
443
444 # Merge fragments into local; local values take precedence
445 _merge_dicts(local, fragments_acc)
446
447 return True
448
449

◆ makeSequence()

python.ConfigText.makeSequence ( configPath,
* ,
flags = None,
algSeq = None,
noSystematics = None,
dataType = None,
geometry = None,
autoconfigFromFlags = None,
isPhyslite = None,
noPhysliteBroken = False )
 

Definition at line 333 of file ConfigText.py.

333def makeSequence(configPath, *, flags=None, algSeq=None, noSystematics=None, dataType=None, geometry=None, autoconfigFromFlags=None, isPhyslite=None, noPhysliteBroken=False):
334 """
335 """
336
337 # Historically we have used the identifier
338 # `autoconfigFromFlags`, but in the rest of the code base
339 # `flags` is used. So for now we allow either, and can hopefully
340 # at some point remove the former (21 Aug 25).
341 if autoconfigFromFlags is not None:
342 if flags is not None:
343 raise ValueError("Cannot pass both flags and autoconfigFromFlags arguments")
344 flags = autoconfigFromFlags
345 warnings.warn ('Using autoconfigFromFlags parameter is deprecated, use flags instead', category=deprecationWarningCategory, stacklevel=2)
346 elif flags is None:
347 warnings.warn ('it is deprecated to configure meta-data for analysis configuration manually, please read the configuration flags via the meta-data reader', category=deprecationWarningCategory, stacklevel=2)
348
349 from AnalysisAlgorithmsConfig.ConfigAccumulator import ConfigAccumulator
350
351 config = TextConfig(configPath)
352
353 logCPAlgTextCfg.info("Configuration file read in:")
354 config.printConfig()
355
356 logCPAlgTextCfg.info("Default algorithms:")
357 config.printAlgs(printOpts=True)
358
359 logCPAlgTextCfg.info("Configuring algorithms based on YAML file:")
360 configSeq = config.configure()
361
362 # defaults are added to config as algs are configured
363 logCPAlgTextCfg.info("Configuration used:")
364 config.printConfig()
365
366 # compile
367 configAccumulator = ConfigAccumulator(algSeq=algSeq, dataType=dataType, isPhyslite=isPhyslite, geometry=geometry, autoconfigFromFlags=autoconfigFromFlags, flags=flags, noSystematics=noSystematics)
368 configSeq.fullConfigure(configAccumulator)
369
370 # blocks can be reordered during configSeq.fullConfigure
371 logCPAlgTextCfg.info("ConfigBlocks and their configuration:")
372 configSeq.printOptions()
373
374 return configAccumulator.CA if isAthena else None
375
376
377# Combine configuration files
378#
379# See the README for more info on how this works
380#

◆ printYaml()

python.ConfigText.printYaml ( d,
sort = False,
jsonFormat = False )
Prints a dictionary as YAML

Definition at line 31 of file ConfigText.py.

31def printYaml(d, sort=False, jsonFormat=False):
32 """Prints a dictionary as YAML"""
33 print(yaml.dump(d, default_flow_style=jsonFormat, sort_keys=sort))
34
void print(char *figname, TCanvas *c1)

◆ readYaml()

python.ConfigText.readYaml ( yamlPath)
Loads YAML file into a dictionary

Definition at line 22 of file ConfigText.py.

22def readYaml(yamlPath):
23 """Loads YAML file into a dictionary"""
24 if not os.path.isfile(yamlPath):
25 raise ValueError(f"{yamlPath} is not a file.")
26 with open(yamlPath, 'r') as f:
27 textConfig = yaml.safe_load(f)
28 return textConfig
29
30

Variable Documentation

◆ logCPAlgTextCfg

python.ConfigText.logCPAlgTextCfg = logging.getLogger('CPAlgTextCfg')

Definition at line 19 of file ConfigText.py.