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

View of /KahApp/KahFwk.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (as text) (annotate)
Tue Jan 17 17:12:47 2006 UTC (13 years, 10 months ago) by efrank
Branch: MAIN
CVS Tags: lwc, HEAD
Changes since 1.1: +49 -25 lines
persistence for dynamics stuff
bunch of flux modelinig stuff including flux model building in modeleditor

import sys

from KahOM.Model                       import Model
from KahApp.KahConfigMgr               import KahConfigMgr
from CatalogServices.Catalog           import Catalog


#------------------------------------------------------------------------
def die(msg):
#------------------------------------------------------------------------
    print msg
    sys.exit(1)
    return


#------------------------------------------------------------------------
class KahFwk:
#------------------------------------------------------------------------

    #------------------------------------------------------------------------
    def __init__(self, configFileName, moduleList, model=None ):
    #------------------------------------------------------------------------
        """
        @type configFileName: string
        @type moduleList: list of KahModule derivative instances
        @type model: KahOM.Model instance        
        """

        self.nameToInstance  = {}   # map instance name to instance
        self.exeChain=[]            #list of modules in order to call them

        self.__model = model
        if (None == model): self.__model = Model( "From KahFwk" )

        self.configMgr = KahConfigMgr()

        self.setupConfig(configFileName)

        #
        for m in moduleList:
            self.addModuleToWeb(m.name(), "", m)


        return

    #------------------------------------------------------------------------
    def getModel( self ):
    #------------------------------------------------------------------------
        return self.__model
    
    #------------------------------------------------------------------------
    def setupConfig( self, configFileName ):
    #------------------------------------------------------------------------

        # Config ourselves.  Unusual usage of configMgr.

        self.configMgr.newSection( "KahFwk" )
        cfg = self.configMgr.getSection( "KahFwk" )

        cfg.regString( "DefaultSeedInstance", "http://theSeed.uchicago.edu/FIG" )
        cfg.regString( "DefaultDataFactory", "pgsql:5432:kahdb:trust:inme" )
        cfg.regString( "DefaultDataFactoryDbType", "postgres" )
        cfg.regString( "DefaultChemDb", "pgsql:5432:kahchemdb:trust:inme" )
        cfg.regString( "DefaultChemDbType", "postgres" )
        cfg.regStringList( "ModuleList", [] )
        cfg.regString( "InModelPath", "")

        cfg.regInt( "verbose", 0, "Print out info.")

        # first loadConfigFile snarfs in the module list
        
        self.configMgr.loadConfigFile( configFileName )

    #------------------------------------------------------------------------
    def configModules( self ):
    #------------------------------------------------------------------------

        # let modules config themselves

        for m in self.exeChain:
            cfg =  self.configMgr.newSection( m.name() )
            m.config(cfg)

        # now load the config file to override default vlues

        self.configMgr.loadConfigFile( "KahFwk.cfg" )
        self.configMgr.dump()

        return


    #------------------------------------------------------------------------
    def doDefaultDataFactories(self):
    #------------------------------------------------------------------------
        from KahDataServices.KahDbDataFactory  import KahDbDataFactory
        from KahChemDb.KahChemDbDataFactory    import KahChemDbDataFactory
        from KahDataServices.SeedDataFactory   import SeedDataFactory

        cfg    = self.configMgr.getSection( "KahFwk" )
        ddf    =  cfg.getString("DefaultDataFactory")
        ddft   = cfg.getString( "DefaultDataFactoryDbType")
        dchem  = cfg.getString( "DefaultChemDb")
        dchemt = cfg.getString( "DefaultChemDbType")
        dseed  = cfg.getString( "DefaultSeedInstance")

        # nb: making these datafactories auto registeres them in catalog

        try:
            chem = KahChemDbDataFactory( dchem, dchemt )
        except NotImplementedError:
            pass

        try:
            df   = KahDbDataFactory( ddf, ddft )
        except NotImplementedError:
            pass

        try:
            sdf = SeedDataFactory( dseed)
        except Exception, e:
            print "Exception getting default seed instance, %s", e

        return

    #------------------------------------------------------------------------
    def getModulesByCfg(self):
    #------------------------------------------------------------------------

        cfg    = self.configMgr.getSection( "KahFwk" )

        moduleList = cfg.getStringList("ModuleList")
        for p in moduleList:
            print "trying to get module named: ", p

            try:
                mod = __import__( p, globals(), locals(), "registerHook" )
                mod.registerHook( self.addModuleToWeb )
            except ImportError, e:
                die( "Could not import %s: %s" % (p, e) )
            except LookupError:
                die("No register hook called 'register' found in module.")

        return
    
    #------------------------------------------------------------------------
    def addModuleToWeb(self, instanceName, helpString, instance):
    #------------------------------------------------------------------------
        """
        maintain the execution web.
        Note- instanceName is carryOver from ModelEditor where it's the
        dropdown menu item name.  here we want to enforce that it comes
        from the actual init of the kahModule.
        """

        if instanceName in self.nameToInstance.keys():
            die("Duplicate module instance name %s" % instanceName)
            
        self.nameToInstance[instance.name()] = instance
        self.exeChain.append(instance)

        return


    def printfoo(self,  ce ):
        print ce.path()

    #------------------------------------------------------------------------
    def loadModel(self):
    #------------------------------------------------------------------------

        cfg          =  self.configMgr.getSection( "KahFwk" )
        inModelPath  =  cfg.getString("InModelPath")

        if ("" == inModelPath ):
            die( "Must provide input model path via InModelPath in KahFwk section")

        ce = Catalog.instance().open( inModelPath )
        if (None == ce):
            ctop=Catalog.instance().apply(self.printfoo)
            die ("Provided path %s can not be read" % inModelPath )


        self.__model = ce.read()

        return

    #------------------------------------------------------------------------
    def run(self):
    #------------------------------------------------------------------------
        """
        """

        self.configModules()
        fwkCfg =  self.configMgr.getSection( "KahFwk"  )
        verbose = fwkCfg.getInt( "verbose" )

        for m in self.exeChain:
            if (verbose): print "Entering module ", m.name()

            # there are cases where a module will utterly replace the
            # the top recoelem, so getTop ever time 
            top=self.__model.getTop()

            cfg =  self.configMgr.getSection( m.name() )
            m.process( self.__model, top, cfg)

        return



MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3