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

View of /KahApp/KahConfigSection.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (as text) (annotate)
Fri Jan 7 14:40:32 2005 UTC (14 years, 10 months ago) by efrank
Branch: MAIN
CVS Tags: post-st-migration2, post-st-migration, before-dynamics-persistence, lwc, treatman-lastDay-0, preTreatmanDyn, HEAD
Changes since 1.1: +75 -9 lines
migration to new schematools generated classes.
new chemdb.
factor chem out of seed data factory
imporved configuration of modeleditor

#--------------------------------------------------------------
# 
# KahConfigSection
#   Holds configuration values and is passed to a module when it is
#   called by the framework. See KahConfigMgr.
#
#   Why are there int, float, string methods rather than using
#   some kind of genericity?  A) So that this will work in other
#   languages. B) Prior experience shows that if it isn't clear
#   what value type is expected for config variables, the system
#   does not scale and you fight mis-configuration errors.  So,
#   this is meant to be a straight jacket over top of pythons Configure.
#
#
# HISTORY:
#    07Jul04  efrank   first version
#
# BUGS:
#--------------------------------------------------------------

import re

class KahConfigSection:
    """
    Holds the configuration informtion (paramter values) associated
    with one module in the application
    """

    #------------------------------------------------------------------------
    def __init__( self, name ):
    #------------------------------------------------------------------------
        """
        @type sectionName: string
        """

        self.__sectionName = name
        self.__dict        = {}       #need to rewrite to replace tuple with obj
        return


    #------------------------------------------------------------------------
    def name( self):
    #------------------------------------------------------------------------
        return self.__sectionName

    
    #------------------------------------------------------------------------
    def wasOverridden( self, keyName):
    #------------------------------------------------------------------------
        """
        Returns 0/1, was this param changed from its defaultValue?
        """

        return self.__dict[keyName][3]

    #------------------------------------------------------------------------
    def override(self, keyName, value):
    #------------------------------------------------------------------------
        """
        Used by the KahConfigMgr to override values in this ConfigSection.
        """

        v=self.__dict[keyName]
        v[2] = apply( v[1], [value] )
        v[3]=1

        return

    #------------------------------------------------------------------------
    def getInt( self, keyName):
    #------------------------------------------------------------------------
        """
        Gets integral value.
        """
    
        return self.__get( "int", keyName)

    #------------------------------------------------------------------------
    def getFloat( self, keyName):
    #------------------------------------------------------------------------
        """
        Gets float value.
        """
    
        return self.__get( "float", keyName)

    #------------------------------------------------------------------------
    def getString( self, keyName):
    #------------------------------------------------------------------------
        """
        Gets string value.
        """
    
        return self.__get( "str", keyName)

    #------------------------------------------------------------------------
    def getStringList( self, keyName):
    #------------------------------------------------------------------------
        """
        Gets string value.
        """
    
        return self.__get( "stringList", keyName)

    #------------------------------------------------------------------------
    def regInt( self, keyName, defaultVal, docString="No doc string" ):
    #------------------------------------------------------------------------
        """
        Register an integer with this keyName and default value.

        @type keyName: string
        @type defaultValue: integer
        """

        self.__register( "int", int, keyName, defaultVal, docString)
        return

    #------------------------------------------------------------------------
    def regFloat( self, keyName, defaultVal, docString="No doc string"  ):
    #------------------------------------------------------------------------

        """
        Register a float  with this keyName and default value.

        @type keyName: string
        @type defaultValue: float
        """

        self.__register( "float", float, keyName, defaultVal, docString)
        return

    #------------------------------------------------------------------------
    def regString( self, keyName, defaultVal, docString="No doc string"  ):
    #------------------------------------------------------------------------

        """
        Register an integer with this keyName and default value.

        @type keyName: string
        @type defaultValue: string
        """

        self.__register( "str", str, keyName, defaultVal, docString)
        return

    #------------------------------------------------------------------------
    def regStringList( self, keyName, defaultVal, docString="No doc string" ):
    #------------------------------------------------------------------------

        """
        Register an integer with this keyName and default value.

        @type keyName: string
        @type defaultValue: list of strings
        """

        s = "%s" % defaultVal

        self.__register( "stringList", self.__stringList, keyName, s, docString)
        return


    #------------------------------------------------------------------------
    def __register( self, type, cast, keyName, defaultValue, docString="No doc string."):
    #------------------------------------------------------------------------
        """
        Register a datum with this keyName and default value.
        Remember the type.

        @type keyName: string
        @type defaultValue: integer
        """

        if self.__dict.has_key( keyName ):
            raise Exception, "Duplicated key"
        else:
            # record value and 0/1 flag for "was overridden"
            self.__dict[keyName] = [type, cast, apply(cast, [defaultValue]), 0, docString]

        return

    
    #------------------------------------------------------------------------
    def __get( self, type, keyName):
    #------------------------------------------------------------------------
        """
        Gets value of specified name and type.
        """
    
        v = self.__dict[keyName]    #can throw
        if ( v[0] == type):
            return v[2]
        else:
            raise Exception, "Wrong type"

    #------------------------------------------------------------------------
    def __stringList( self, arg):
    #------------------------------------------------------------------------
        """
        Gets value of specified name and type.
        """
    
        l = []

        #drop [ and ] and whitespace
        m=re.search(r'\s*\[\s*(.*)\s*\]\s*', arg)
        if (not m):
            raise TypeError

        listguts =m.group(1)

        if ""==listguts:
            return l
        
        # ignore problem that strings could have , in them

        rawElems = listguts.split(",")

        # strings still have " or ' in them.

        for e in rawElems:
            m=re.search(r'\w*\"(.*)\"\w*', e)
            if m:
                l.append( m.group(1))
            else:
                m=re.search(r"\w*\'(.*)\'\w*", e)
                if m:
                    l.append(m.group(1))
                else:
                    raise TypeError, arg


        return l


    #------------------------------------------------------------------------
    def dump( self ):
    #------------------------------------------------------------------------
        """
        Dump for debugging.
        """

        for k in self.__dict.keys():
            print k, self.__dict[k]
        return
    

#------------------------------------------------------------------------
if (__name__ == "__main__"):
#------------------------------------------------------------------------
    print "Testing KahConfigSection.py"
    
    cfg = KahConfigSection( "testSection" )
    print "Made section named [testSection] :", cfg.name()

    cfg.regInt( "int1", 1 )
    cfg.regInt( "int2", 2 )
    cfg.regFloat( "float1", 10.0 )
    cfg.regFloat( "float2", 20.0)
    cfg.regString( "string1", "theString1")
    cfg.regString( "string2", "theString2")
    cfg.dump()
    
    # try cross-wire

    try:
        cfg.regInt( "int3", "gork")
        print "Error: regInt from string"
    except:
        pass
    
    try:
        cfg.regFloat( "float3", "gork")
        print "Error: regFloat from string"
    except:
        pass

    try:
        cfg.regString( "string3", self )  # a funky way to get a non stringable
        print "Error: regString from self"
    except:
        pass

    
    print "done test"

    ##
    # Now override some
    ##

    cfg.override( "int1", 11)
    cfg.override( "float1", "110.0")
    cfg.override( "string1", "theNewString1")

    cfg.dump()

    ##
    # try some gets
    ##

    print cfg.getInt( "int1")
    print cfg.getInt( "int2")
    print cfg.getFloat( "float1")
    print cfg.getFloat( "float2")
    print cfg.getString( "string1")
    print cfg.getString( "string2")

    try:
        cfg.getInt( "feh" )
        print "getInt should have failed"
    except:
        pass

    try:
        cfg.getInt( "float1" )
        print "getInt should have failed"
    except:
        pass

    try:
        cfg.getFloat( "feh" )
        print "getFloat shhould have failed"
    except:
        pass

    try:
        cfg.getFloat( "int1" )
        print "getFloat shhould have failed"
    except:
        pass

    try:
        cfg.getString( "feh" )
        print "getString shhould have failed"
    except:
        pass


    try:
        cfg.getString( "int1" )
        print "getString should have failed"
    except:
        pass

    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3