ATLAS Offline Software
Classes | Functions | Variables
python.ConfigText Namespace Reference

Classes

class  TextConfig
 

Functions

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

Variables

 logCPAlgTextCfg = logging.getLogger('CPAlgTextCfg')
 

Function Documentation

◆ _find_fragment()

def python.ConfigText._find_fragment (   fragment_path,
  config_path 
)
private

Definition at line 397 of file ConfigText.py.

397 def _find_fragment(fragment_path, config_path):
398  paths_to_check = [
399  fragment_path,
400  config_path / fragment_path,
401  *[x / fragment_path for x in os.environ["DATAPATH"].split(":")]
402  ]
403  for path in paths_to_check:
404  if path.exists():
405  return path
406 
407  raise FileNotFoundError(fragment_path)
408 
409 

◆ _merge_dicts()

def python.ConfigText._merge_dicts (   local,
  fragment 
)
private

Definition at line 410 of file ConfigText.py.

410 def _merge_dicts(local, fragment):
411  # in the list case append the fragment to the local list
412  if isinstance(local, list):
413  local += fragment
414  return
415  # In the dict case, append only missing values to local: the local
416  # values take precidence over the fragment ones.
417  if isinstance(local, dict):
418  for key, value in fragment.items():
419  if key in local:
420  _merge_dicts(local[key], value)
421  else:
422  local[key] = value
423  return

◆ combineConfigFiles()

def python.ConfigText.combineConfigFiles (   local,
  config_path,
  fragment_key = "include" 
)

Definition at line 350 of file ConfigText.py.

350 def combineConfigFiles(local, config_path, fragment_key="include"):
351 
352  # if this isn't an iterable there's nothing to combine
353  if isinstance(local, dict):
354  to_combine = local.values()
355  elif isinstance(local, list):
356  to_combine = local
357  else:
358  return
359 
360  # otherwise descend into all the entries here
361  for sub in to_combine:
362  combineConfigFiles(sub, config_path, fragment_key=fragment_key)
363 
364  # if there are no fragments to include we're done
365  if fragment_key not in local:
366  return
367 
368  fragment_path = _find_fragment(
369  pathlib.Path(local[fragment_key]),
370  config_path)
371 
372  with open(fragment_path) as fragment_file:
373  # once https://github.com/yaml/pyyaml/issues/173 is resolved
374  # pyyaml will support the yaml 1.2 spec, which is compatable
375  # with json. Until then yaml and json behave differently, so
376  # we have this override.
377  if fragment_path.suffix == '.json':
378  fragment = json.load(fragment_file)
379  else:
380  fragment = yaml.safe_load(fragment_file)
381 
382  # fill out any sub-fragments, looking in the parent path of the
383  # fragment for local sub-fragments.
385  fragment,
386  fragment_path.parent,
387  fragment_key=fragment_key
388  )
389 
390  # merge the fragment with this one
391  _merge_dicts(local, fragment)
392 
393  # delete the fragment so we don't stumble over it again
394  del local[fragment_key]
395 
396 

◆ makeSequence()

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

Definition at line 309 of file ConfigText.py.

309 def makeSequence(configPath, dataType, algSeq, geometry=None, autoconfigFromFlags=None,
310  isPhyslite=False, noPhysliteBroken=False, noSystematics=None):
311  """
312  """
313 
314  from AnalysisAlgorithmsConfig.ConfigAccumulator import ConfigAccumulator
315 
316  config = TextConfig(configPath)
317 
318  logCPAlgTextCfg.info("Configuration file read in:")
319  config.printConfig()
320 
321  logCPAlgTextCfg.info("Default algorithms:")
322  config.printAlgs(printOpts=True)
323 
324  logCPAlgTextCfg.info("Configuring algorithms based on YAML file:")
325  configSeq = config.configure()
326 
327  # defaults are added to config as algs are configured
328  logCPAlgTextCfg.info("Configuration used:")
329  config.printConfig()
330 
331  # compile
332  configAccumulator = ConfigAccumulator(algSeq, dataType, isPhyslite=isPhyslite, geometry=geometry, autoconfigFromFlags=autoconfigFromFlags, noSystematics=noSystematics)
333  configSeq.fullConfigure(configAccumulator)
334 
335  # blocks can be reordered during configSeq.fullConfigure
336  logCPAlgTextCfg.info("ConfigBlocks and their configuration:")
337  configSeq.printOptions()
338 
339  from AnaAlgorithm.DualUseConfig import isAthena, useComponentAccumulator
340  if isAthena and useComponentAccumulator:
341  return configAccumulator.CA
342  else:
343  return None
344 
345 
346 # Combine configuration files
347 #
348 # See the README for more info on how this works
349 #

◆ printYaml()

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

Definition at line 28 of file ConfigText.py.

28 def printYaml(d, sort=False, jsonFormat=False):
29  """Prints a dictionary as YAML"""
30  print(yaml.dump(d, default_flow_style=jsonFormat, sort_keys=sort))
31 
32 

◆ readYaml()

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

Definition at line 19 of file ConfigText.py.

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

Variable Documentation

◆ logCPAlgTextCfg

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

Definition at line 16 of file ConfigText.py.

python.ConfigText.combineConfigFiles
def combineConfigFiles(local, config_path, fragment_key="include")
Definition: ConfigText.py:350
python.ConfigText.makeSequence
def makeSequence(configPath, dataType, algSeq, geometry=None, autoconfigFromFlags=None, isPhyslite=False, noPhysliteBroken=False, noSystematics=None)
Definition: ConfigText.py:309
python.ConfigText.readYaml
def readYaml(yamlPath)
Definition: ConfigText.py:19
python.ConfigText._find_fragment
def _find_fragment(fragment_path, config_path)
Definition: ConfigText.py:397
python.ConfigText.printYaml
def printYaml(d, sort=False, jsonFormat=False)
Definition: ConfigText.py:28
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
Trk::open
@ open
Definition: BinningType.h:40
python.ConfigText._merge_dicts
def _merge_dicts(local, fragment)
Definition: ConfigText.py:410
Trk::split
@ split
Definition: LayerMaterialProperties.h:38