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 331 of file ConfigText.py.

331 def _find_fragment(fragment_path, config_path):
332  paths_to_check = [
333  fragment_path,
334  config_path / fragment_path,
335  *[x / fragment_path for x in os.environ["DATAPATH"].split(":")]
336  ]
337  for path in paths_to_check:
338  if path.exists():
339  return path
340 
341  raise FileNotFoundError(fragment_path)
342 
343 

◆ _merge_dicts()

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

Definition at line 344 of file ConfigText.py.

344 def _merge_dicts(local, fragment):
345  # in the list case append the fragment to the local list
346  if isinstance(local, list):
347  local += fragment
348  return
349  # In the dict case, append only missing values to local: the local
350  # values take precidence over the fragment ones.
351  if isinstance(local, dict):
352  for key, value in fragment.items():
353  if key in local:
354  _merge_dicts(local[key], value)
355  else:
356  local[key] = value
357  return

◆ combineConfigFiles()

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

Definition at line 284 of file ConfigText.py.

284 def combineConfigFiles(local, config_path, fragment_key="include"):
285 
286  # if this isn't an iterable there's nothing to combine
287  if isinstance(local, dict):
288  to_combine = local.values()
289  elif isinstance(local, list):
290  to_combine = local
291  else:
292  return
293 
294  # otherwise descend into all the entries here
295  for sub in to_combine:
296  combineConfigFiles(sub, config_path, fragment_key=fragment_key)
297 
298  # if there are no fragments to include we're done
299  if fragment_key not in local:
300  return
301 
302  fragment_path = _find_fragment(
303  pathlib.Path(local[fragment_key]),
304  config_path)
305 
306  with open(fragment_path) as fragment_file:
307  # once https://github.com/yaml/pyyaml/issues/173 is resolved
308  # pyyaml will support the yaml 1.2 spec, which is compatable
309  # with json. Until then yaml and json behave differently, so
310  # we have this override.
311  if fragment_path.suffix == '.json':
312  fragment = json.load(fragment_file)
313  else:
314  fragment = yaml.safe_load(fragment_file)
315 
316  # fill out any sub-fragments, looking in the parent path of the
317  # fragment for local sub-fragments.
319  fragment,
320  fragment_path.parent,
321  fragment_key=fragment_key
322  )
323 
324  # merge the fragment with this one
325  _merge_dicts(local, fragment)
326 
327  # delete the fragment so we don't stumble over it again
328  del local[fragment_key]
329 
330 

◆ makeSequence()

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

Definition at line 243 of file ConfigText.py.

243 def makeSequence(configPath, dataType, algSeq, geometry=None, autoconfigFromFlags=None,
244  isPhyslite=False, noPhysliteBroken=False, noSystematics=None):
245  """
246  """
247 
248  from AnalysisAlgorithmsConfig.ConfigAccumulator import ConfigAccumulator
249 
250  config = TextConfig(configPath)
251 
252  logCPAlgTextCfg.info("Configuration file read in:")
253  config.printConfig()
254 
255  logCPAlgTextCfg.info("Default algorithms:")
256  config.printAlgs(printOpts=True)
257 
258  logCPAlgTextCfg.info("Configuring algorithms based on YAML file:")
259  configSeq = config.configure()
260 
261  # defaults are added to config as algs are configured
262  logCPAlgTextCfg.info("Configuration used:")
263  config.printConfig()
264 
265  # compile
266  configAccumulator = ConfigAccumulator(algSeq, dataType, isPhyslite=isPhyslite, geometry=geometry, autoconfigFromFlags=autoconfigFromFlags, noSystematics=noSystematics)
267  configSeq.fullConfigure(configAccumulator)
268 
269  # blocks can be reordered during configSeq.fullConfigure
270  logCPAlgTextCfg.info("ConfigBlocks and their configuration:")
271  configSeq.printOptions()
272 
273  from AnaAlgorithm.DualUseConfig import isAthena, useComponentAccumulator
274  if isAthena and useComponentAccumulator:
275  return configAccumulator.CA
276  else:
277  return None
278 
279 
280 # Combine configuration files
281 #
282 # See the README for more info on how this works
283 #

◆ 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:284
python.ConfigText.makeSequence
def makeSequence(configPath, dataType, algSeq, geometry=None, autoconfigFromFlags=None, isPhyslite=False, noPhysliteBroken=False, noSystematics=None)
Definition: ConfigText.py:243
python.ConfigText.readYaml
def readYaml(yamlPath)
Definition: ConfigText.py:19
python.ConfigText._find_fragment
def _find_fragment(fragment_path, config_path)
Definition: ConfigText.py:331
python.ConfigText.printYaml
def printYaml(d, sort=False, jsonFormat=False)
Definition: ConfigText.py:28
Trk::open
@ open
Definition: BinningType.h:40
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
python.ConfigText._merge_dicts
def _merge_dicts(local, fragment)
Definition: ConfigText.py:344
Trk::split
@ split
Definition: LayerMaterialProperties.h:38