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

View of /KahApp/KahFwk.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Fri Mar 11 21:33:13 2005 UTC (14 years, 8 months ago) by efrank
Branch: MAIN
CVS Tags: before-dynamics-persistence, treatman-lastDay-0, preTreatmanDyn
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

#!/usr/bin/env python
import sys

from KahApp.KahConfigMgr               import KahConfigMgr
from CatalogServices.Catalog           import Catalog

from KahDataServices.KahDbDataFactory  import KahDbDataFactory
from KahChemDb.KahChemDbDataFactory    import KahChemDbDataFactory
from KahDataServices.SeedDataFactory   import SeedDataFactory

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


class KahFwk:
    #------------------------------------------------------------------------
    def __init__(self):
    #------------------------------------------------------------------------
        self.nameToInstance  = {}   # map instance name to instance
        self.exeChain=[]            #list of modules in order to call them

        self.configMgr = KahConfigMgr()

        self.setupConfig()

        self.getModules()

        self.configModules()

        self.doDefaultDataFactories()
        self.loadModel()

        return

    #------------------------------------------------------------------------
    def setupConfig( self ):
    #------------------------------------------------------------------------

        # 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", "kahchoo/bss@db02" )
        cfg.regString( "DefaultDataFactoryDbType", "oracle" )
        cfg.regString( "DefaultChemDb", "pgsql:5432:kahdb:trust:inme" )
        cfg.regString( "DefaultChemDbType", "postgres" )
        cfg.regStringList( "ModuleList", [] )
        cfg.regString( "InModelPath", "")

        # first loadConfigFile snarfs in the module list
        
        self.configMgr.loadConfigFile( "KahFwk.cfg" )

    #------------------------------------------------------------------------
    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):
    #------------------------------------------------------------------------

        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 getModules(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 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):
            die ("Provided path %s can not be read" % inModelPath )


        self.model = ce.read()

        return

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

        top=self.model.getTop()

        for m in self.exeChain:
            cfg =  self.configMgr.getSection( m.name() )
            m.process( self.model, top, cfg)

        return


if __name__ == "__main__":
    print "kahfwk main"
    f = KahFwk()
    f.run()
    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3