Crombie Tools
FileConfigReader.py
Go to the documentation of this file.
1 """@package CrombieTools.CommonTools.FileConfigReader
2 Holds centralized SetupConfigFromEnv functions.
3 @author Daniel Abercrombie <dabercro@mit.edu>
4 """
5 
6 import os
7 
8 from .. import LoadConfig
9 
10 def SetFunctionFromEnv(targets):
11  """Calls functions using environment variables
12 
13  @param targets is a list of tuples, each containing a function and
14  the environment variable to use as an argument
15  """
16  for func, envname in targets:
17  if os.environ.get(envname) == None:
18  print 'Cannot find ' + envname + ' in config'
19  else:
20  try:
21  func(os.environ[envname])
22  except:
23  try:
24  func(float(os.environ[envname]))
25  except:
26  func(int(os.environ[envname]))
27 
28 
30  """Sets up FileConfigReaders to read the config file automatically.
31  It uses the environment variable $DEBUG to set the verbosity level of a class.
32  The default is 1, with a maximum of 3 for real debugging output.
33 
34  @param obj is the python object that inherits from FileConfigReader.
35  """
36 
37  level = int(os.environ.get('DEBUG', 1))
38  if os.environ.get('DEBUG'):
39  print 'Setting debug level to %i' % level
40 
41  obj.SetDebugLevel(level)
42 
43  def readMC(config):
44  obj.ReadMCConfig(config, obj.kBackground)
45  def readSignal(config):
46  obj.ReadMCConfig(config, obj.kSignal)
47 
49  (obj.SetInDirectory, 'CrombieInFilesDir'),
50  (obj.SetLuminosity, 'CrombieLuminosity'),
51  (readMC, 'CrombieMCConfig'),
52  (readSignal, 'CrombieSignalConfig')
53  ])
54 
55  obj.SetFileType(obj.kBackground)