[Bio] / KahApp / KahConfigMgr.py Repository:
ViewVC logotype

View of /KahApp/KahConfigMgr.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (download) (as text) (annotate)
Fri Mar 11 21:33:13 2005 UTC (14 years, 7 months ago) by efrank
Branch: MAIN
CVS Tags: before-dynamics-persistence, lwc, treatman-lastDay-0, preTreatmanDyn, HEAD
Changes since 1.3: +1 -1 lines
1. introduce Model as king of the hill rather than RecoElem.
2. migrations to follow
3. new modules for by-peg filtering
4. first stab at externalized app fwk
5. moved seed data factory

bunch of timing code still in this ...need to take out

#--------------------------------------------------------------
# 
# KahConfigManager
#
# HISTORY:
#    07Jul04  efrank   first version
#
# BUGS:
#--------------------------------------------------------------
"""
     Manages configuration information.  Underneath, at least for now
     is pythons ConfigParser.  This defines the filefomrat (for now).
     But we make a different interface here.  I want to make sure that
     we don't get too stuck in python, and so I worry about type
     genericity.  Also, previous experience has shown that config
     needs to be fairly rigid for apps to scale or else it becomes too
     easy to misconfigure.
     
     The idea will be that Kah apps are made up of modules.  Modules
     do work and are configurable.  A module is a class and you can
     have multiple instances of the module.  Each instance has a
     globally unique name.  The module name is used to rendezvous
     on config information and to define workflow.

     Modules have a config() method that will be called before they
     ar asked to do any processing.  Config is passed an instance
     of KahConfigSection that is specific to that module instance.
     The module must register all config data, by name and type,
     that it wants to use.  It's just key/value pairs.

     The config manager will read config information and will
     use it to override values, as needed.

     Later, when the module is asked to process, it is passed
     a KahConfigInstance that it can use to get paramters back.
     
"""

import ConfigParser
from KahApp.KahConfigSection    import KahConfigSection

class KahConfigMgr:
    
    #------------------------------------------------------------------------
    def __init__(self):
    #------------------------------------------------------------------------
        self.__sectionMap = {}     # maps module name to KahConfigSection
        self.__parser = ConfigParser.ConfigParser()
        self.__parser.optionxform = str

        return


    #------------------------------------------------------------------------
    def newSection(self, name):
    #------------------------------------------------------------------------
        """
        Add a new section to the configuration.  Name must not have been
        used before.  Name corresponds to the name of an instance of a module.

        @type name: string
        @rtype: KahConfigSection just created
        """

        if  self.__sectionMap.has_key( name ):
            raise ValueError
            
        cfg = KahConfigSection( name )
        self.__sectionMap[name] = cfg

        return cfg
    
    #------------------------------------------------------------------------
    def getSection( self, name ):
    #------------------------------------------------------------------------
        """
        Return KahSectionMap for given name.
        @type name: string
        """

        return self.__sectionMap[name]
        
    #------------------------------------------------------------------------
    def loadConfigFile( self, fileName ):
    #------------------------------------------------------------------------
        """
        Loads in config info from fileName and overrides stuff in
        the various KahConfigSections.  If config file has config info
        that has not been asked for (registered), we ignore it silently.

        @type fileName: string
        @rtype: none         
        """

        # create the parser and set optionxform to str so that
        # case sensitivity is preserved.
        
        sections = self.__parser.read( fileName )
        
        self.redoOverrides()

        return

    #------------------------------------------------------------------------
    def redoOverrides( self):
    #------------------------------------------------------------------------
        """
        Data in config file overrides possible default values for parameters.
        Re-execute the overrides. (Config file not reloaded)
        """
       
        for sName in self.__parser.sections():
            try:
                cfgSection = self.__sectionMap[ sName ]
            except KeyError:
                continue
                
            options    = self.__parser.options( sName )
            for o in options:
                val = self.__parser.get(sName, o, -1)  #do not allow variable eval
                try:
                    cfgSection.override( o, val )
                except KeyError:
                    continue

        return
    
    
    #------------------------------------------------------------------------
    def dump( self):
    #------------------------------------------------------------------------
        """
        debugging dump
        """

        for k in self.__sectionMap.keys():
            print k, ":"
            self.__sectionMap[k].dump()
            print

        return

#------------------------------------------------------------------------
if (__name__ == "__main__"):
#------------------------------------------------------------------------
    print "testing KahConfigMgr"

    from KahApp.KahModuleExample import KahModuleExample
    from KahOM.RecoElem          import RecoElem
    
    ##
    # Make the mgr and some modules to play with
    ##

    mgr = KahConfigMgr()

    modules = [ KahModuleExample( "Module1"), 
                KahModuleExample( "Module2")
                ]

    ##
    # Let the modules define the config params they expect.
    ##

    for m in modules:
        mgr.newSection( m.name() )
        m.config( mgr.getSection( m.name()) )
    
    ##
    # Load in config file, perhaps causing values to be
    # overriden
    ##

    mgr.loadConfigFile( "test" )

    mgr.dump()

    ##
    # run the dummy modules
    ##
    
    for m in modules:
        m.process(  None, RecoElem( "theElem" ), mgr.getSection( m.name()) )
        

    print
    print "done"
    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3