Skip to content
Snippets Groups Projects
ConfigHelper.py 1.19 KiB
Newer Older
"""

Helper object to identify configuration parameters so we can easily overwrite
them via command line magic or via the steering file

"""

from pprint import pprint

class ConfigHelper( object ):
  """Base class for configuration helper"""
  def __init__( self ):
    pass

  def getOptions(self):
    finalVars = {}

    # get all direct members not starting with underscore
    allVars = vars(self)
    for var,val in allVars.iteritems():
      if not var.startswith('_'):
        finalVars[var] = (val,'')

    # now get things defined with @property
    props = [(p, getattr(type(self),p)) for p in dir(type(self)) if isinstance(getattr(type(self),p),property)]
    for propName, prop in props:
      finalVars[propName] = (getattr(self, propName), prop.__doc__)

    return finalVars

  def printOptions( self ):
    """print all paramters"""
    return pprint(self.getOptions())

  def setOption( self, name, val ):
    """ set the attribute name to val """
    setattr(self, name, val)

  @staticmethod
  def listifyString( stringVal, sep=" "):
    """returns a list from a string separated by sep"""
    if isinstance( stringVal, list ):
      return stringVal
    else:
      return stringVal.split(sep)