Example #1
0
    def menu( self, clientId ):
        menu = "{!"

        menuIndex = 1

        for item in self.menuItems:
            if isString( item ):
                menu += item + endl
                continue

            assert isTuple( item ) # item wasn't tuple or string
            assert len( item ) == 3
            
            ( itemDesc, itemSelectedFunc, itemValueDescFunc ) = item
            
            assert isString( itemDesc )
            assert isFunc( itemSelectedFunc)
            assert isFunc( itemValueDescFunc)

            itemLabel = menuIndex
            if self.alphabeticOptions:
                itemLabel = chr(96 + menuIndex )

            menu += " {FC%s{FG) {FU%s{FG: {FY%s" % ( itemLabel, itemDesc, itemValueDescFunc( clientId ) ) + endl

            menuIndex += 1

        menu += " {FCa{FG) abort " + endl
        menu += " {FCf{FG) finish " + endl

        return menu
Example #2
0
    def __init__( self, modeName, modeCmd, modeDefaultCallback = None, modeAddDefaultCmds = True ):
        assert isString( modeCmd )
        assert isString( modeName )

        if not modeDefaultCallback:
            modeDefaultCallback = lambda clientId, remaining: sendToClient( clientId, "Invalid %s mode command. ({!{FC!{@ to exit, {!{FCcommands{@ for help)\r\n" % self.modeName )

        self.modeCmd = modeCmd
        self.modeName = modeName
        self.modeMap = CmdMap( modeDefaultCallback ) 

        def enterMode( clientId, remaining ):
            pushCmdHandler( clientId, self.modeMap )
            sendToClient( clientId, "\r\n{!{FG[{FYEntering %s mode{FG] {@({!{FC!{@ to exit)\r\n" % self.modeName)
            if modeAddDefaultCmds:
                sendToClient( clientId, self.modeMap.commands() )
            sendToClient( clientId, "\r\n" )

        def exitMode( clientId, remaining ):
            popCmdHandler( clientId )
            sendToClient( clientId, "{!{FG[{FYExiting %s Mode{FG]\r\n" % self.modeName)


        self.enterModeCallback = enterMode

        self.modeMap.addCmd("!", exitMode )
        if modeAddDefaultCmds:
            self.modeMap.addCmd("exit", exitMode )
            self.modeMap.addCmd("commands", lambda clientId, remaining: sendToClient( clientId, self.modeMap.commands() ) )
Example #3
0
    def __init__( self, clientId, nameOfEdit, initialText, submitCallback, invalidSelectionCallback = "DEFAULT" ):
        """
        creates a text editor widget and activates it for clientId, used to input arbitrarily formatted text

        submitCallback: func( clientId, text )
        """

        assert isString( nameOfEdit )
        assert isString( initialText )
        assert isFunc( submitCallback )

        # invalidSelectionCallback may be:
        #  "DEFAULT" - an internal hack to bind to self.menu
        #  None      - meaning an invalid selection defers to a later cmdHandler
        #  Func      - a client-supplied invalidSelectionCallback
        if invalidSelectionCallback == "DEFAULT":
           invalidSelectionCallback = lambda clientId, remaining: _defaultInvalidSelectionCallback( clientId, self.menu)
        if isDefined( invalidSelectionCallback ):
            assert isFunc( invalidSelectionCallback )

        self.text = initialText
        self.submitCallback = submitCallback
        self.menu = endl + "{!{FYEditing %s" % nameOfEdit + endl
        self.cmdMap = CmdMap( invalidSelectionCallback )

        _addDisplay( self )
        _addFinish( self )

        self.menu += endl

        _activate( clientId, self )
Example #4
0
    def __init__( self, menuItems, submitCallback, invalidSelectionCallback = "DEFAULT", alphabeticOptions = False ):
        """
        creates a form menu widget, used to contain other menu widgets
        """
        assert isList( menuItems )
        assert isFunc( submitCallback )
        assert isBool( alphabeticOptions )


        # invalidSelectionCallback may be:
        #  "DEFAULT" - an internal hack to bind to self.menu
        #  None      - meaning an invalid selection defers to a later cmdHandler
        #  Func      - a client-supplied invalidSelectionCallback
        if invalidSelectionCallback == "DEFAULT":
           invalidSelectionCallback = lambda clientId, remaining: defaultInvalidSelectionCallback( clientId, self.menu)
        if isDefined( invalidSelectionCallback ):
            assert isFunc( invalidSelectionCallback )
        
        
        assert len(menuItems) > 0

        self.menuItems = menuItems
        self.prompt = "{!{FB<options: "
        self.cmdMap = CmdMap( invalidSelectionCallback )
        self.alphabeticOptions = alphabeticOptions

        menuIndex = 1

        self.cmdMap.addCmd( "f", lambda clientId, remaining: submitCallback( clientId ) )
        self.cmdMap.addCmd( "a", lambda clientId, remaining: submitCallback( clientId, abort=True ) )
        for item in self.menuItems:
            if isString( item ):
                continue

            assert isTuple( item ) # item wasn't tuple or string
            assert len( item ) == 3
            
            ( itemDesc, itemSelectedFunc, itemValueDescFunc ) = item
            
            assert isString( itemDesc )
            assert isFunc( itemSelectedFunc)
            assert isFunc( itemValueDescFunc)

            itemLabel = menuIndex
            if self.alphabeticOptions:
                itemLabel = chr(96 + menuIndex )

            self.cmdMap.addCmd( "%s" % itemLabel, lambda clientId, remaining: itemSelectedFunc( clientId ) )
            self.prompt += "{FC%s{FB, " % itemLabel
            
            menuIndex += 1

        self.prompt += "{FCa{FB, {FCf{FB> " 
Example #5
0
def _submitText( clientId, submitCallback, text ):
    assert isFunc( submitCallback)
    assert isString( text )
    
    popCmdHandler( clientId )

    submitCallback( clientId, text )
Example #6
0
 def test_promptPushPopIdempotent(self):
     self.prompt.pushPrompt(lambda x: "bob")
     self.prompt.popPrompt()
     self.assert_(isString(self.prompt.prompt(None)))
     self.prompt.pushPrompt(lambda x: x)
     self.prompt.pushPrompt(lambda x: "jim")
     self.prompt.popPrompt()
     self.assert_(self.prompt.prompt("fred") == "fred")
Example #7
0
 def test_promptPushPopIdempotent( self ):
     self.prompt.pushPrompt( lambda x: "bob" )
     self.prompt.popPrompt()
     self.assert_( isString( self.prompt.prompt( None )) )
     self.prompt.pushPrompt( lambda x: x )
     self.prompt.pushPrompt( lambda x: "jim" )
     self.prompt.popPrompt()
     self.assert_( self.prompt.prompt( "fred" ) == "fred" )
Example #8
0
 def setType( self, typeName, typeValue ):
     """
     maps a database type, e.g. 'WORLD', to a database type value, e.g 'LAZARUS'
     
     typeName : string
     typeValue: string
         
     this map is injective, i.e. values are distinct
     """
     assert isString( typeName )
     assert isString( typeValue )
         
     assert typeName not in self.types
     for t in self.types:
         assert self.types[ t ] != typeValue
         
     self.types[ typeName ] = typeValue
Example #9
0
def sendToClientFromID(clientId, msg):
    """ Internal use only """
    assert isString(msg)
    msg = color.color(msg + "{@")
    if clientId in clientMsgs:
        clientMsgs[clientId] = clientMsgs[clientId] + msg
    else:
        clientMsgs[clientId] = msg
Example #10
0
    def setType(self, typeName, typeValue):
        """
        maps a database type, e.g. 'WORLD', to a database type value, e.g 'LAZARUS'
        
        typeName : string
        typeValue: string
            
        this map is injective, i.e. values are distinct
        """
        assert isString(typeName)
        assert isString(typeValue)

        assert typeName not in self.types
        for t in self.types:
            assert self.types[t] != typeValue

        self.types[typeName] = typeValue
Example #11
0
 def setPrefix( self, prefix ):
     """
     sets the prefix on all dbms database names, e.g. 'BETA'
     
     prefix: string
     """
     assert isString( prefix )
     self.prefix = prefix
Example #12
0
 def setPrefix(self, prefix):
     """
     sets the prefix on all dbms database names, e.g. 'BETA'
     
     prefix: string
     """
     assert isString(prefix)
     self.prefix = prefix
Example #13
0
def getSessionFactory( dbName, dbType, metadata=data ):
    """
    returns an sqlalchemy sessionmaker, used to access an underlying data model, as described by declarativeBase
    e.g. s = session(), s.commit(), s.add(user)

    dbName         : string, the logical database name, e.g. 'MOB_DESCRIPTIONS'
    dbType         : string, the persistence type class, e.g. 'STATIC', 'INSTANCE'
    """
    assert isString( dbName )
    assert isString( dbType )

    assert dbType in metadata.types
    
    dbActualName = "%s_%s_%s" % (metadata.prefix, metadata.types[ dbType ], dbName)

    engine = create_engine('mysql://*****:*****@localhost/%s' % dbActualName )
    _Base.metadata.create_all(engine) 
    
    return sessionmaker(bind=engine)
Example #14
0
    def __init__(self,
                 modeName,
                 modeCmd,
                 modeDefaultCallback=None,
                 modeAddDefaultCmds=True):
        assert isString(modeCmd)
        assert isString(modeName)

        if not modeDefaultCallback:
            modeDefaultCallback = lambda clientId, remaining: sendToClient(
                clientId,
                "Invalid %s mode command. ({!{FC!{@ to exit, {!{FCcommands{@ for help)\r\n"
                % self.modeName)

        self.modeCmd = modeCmd
        self.modeName = modeName
        self.modeMap = CmdMap(modeDefaultCallback)

        def enterMode(clientId, remaining):
            pushCmdHandler(clientId, self.modeMap)
            sendToClient(
                clientId,
                "\r\n{!{FG[{FYEntering %s mode{FG] {@({!{FC!{@ to exit)\r\n" %
                self.modeName)
            if modeAddDefaultCmds:
                sendToClient(clientId, self.modeMap.commands())
            sendToClient(clientId, "\r\n")

        def exitMode(clientId, remaining):
            popCmdHandler(clientId)
            sendToClient(clientId,
                         "{!{FG[{FYExiting %s Mode{FG]\r\n" % self.modeName)

        self.enterModeCallback = enterMode

        self.modeMap.addCmd("!", exitMode)
        if modeAddDefaultCmds:
            self.modeMap.addCmd("exit", exitMode)
            self.modeMap.addCmd(
                "commands", lambda clientId, remaining: sendToClient(
                    clientId, self.modeMap.commands()))
Example #15
0
def getSessionFactory(dbName, dbType, metadata=data):
    """
    returns an sqlalchemy sessionmaker, used to access an underlying data model, as described by declarativeBase
    e.g. s = session(), s.commit(), s.add(user)

    dbName         : string, the logical database name, e.g. 'MOB_DESCRIPTIONS'
    dbType         : string, the persistence type class, e.g. 'STATIC', 'INSTANCE'
    """
    assert isString(dbName)
    assert isString(dbType)

    assert dbType in metadata.types

    dbActualName = "%s_%s_%s" % (metadata.prefix, metadata.types[dbType],
                                 dbName)

    engine = create_engine('mysql://*****:*****@localhost/%s' %
                           dbActualName)
    _Base.metadata.create_all(engine)

    return sessionmaker(bind=engine)
Example #16
0
    def __init__(self,
                 clientId,
                 nameOfEdit,
                 initialText,
                 submitCallback,
                 invalidSelectionCallback="DEFAULT"):
        """
        creates a text editor widget and activates it for clientId, used to input arbitrarily formatted text

        submitCallback: func( clientId, text )
        """

        assert isString(nameOfEdit)
        assert isString(initialText)
        assert isFunc(submitCallback)

        # invalidSelectionCallback may be:
        #  "DEFAULT" - an internal hack to bind to self.menu
        #  None      - meaning an invalid selection defers to a later cmdHandler
        #  Func      - a client-supplied invalidSelectionCallback
        if invalidSelectionCallback == "DEFAULT":
            invalidSelectionCallback = lambda clientId, remaining: _defaultInvalidSelectionCallback(
                clientId, self.menu)
        if isDefined(invalidSelectionCallback):
            assert isFunc(invalidSelectionCallback)

        self.text = initialText
        self.submitCallback = submitCallback
        self.menu = endl + "{!{FYEditing %s" % nameOfEdit + endl
        self.cmdMap = CmdMap(invalidSelectionCallback)

        _addDisplay(self)
        _addFinish(self)

        self.menu += endl

        _activate(clientId, self)
Example #17
0
 def test_promptEmptyReturnsEpsilon( self ):
     self.assert_( isString( self.prompt.prompt( None )) )
Example #18
0
def clientSentCmd( clientId, cmd ):
    isClient( clientId )
    assert isString( cmd )
    clients[ clientId ].cmds.append( cmd )
Example #19
0
    def __init__(self, menuItems, selectionCallback, invalidSelectionCallback="DEFAULT", alphabeticOptions=False):
        """
        creates a menu widget, used to prompt the user to select a value from a set

        menuItems: [item] - the list of items to display in the menu.
                             item:  string - a line of text (e.g. header) to display in the menu
                                   or
                                   ( string, value ) - a description, value pair that the user can select

        selectionCallback: func - the function called with args ( clientId, selectedValue ) when the user selects a value

        invalidSelectionCallback: func - the function called with args ( clientId, clientInputRemaining )
                                         when the user's input doesn't map to a valid choice

        alphabeticOptions: bool - if true, use alphabetic, i.e. a..z, instead of numeric indices for the menu
        """
        assert isList(menuItems)
        assert isFunc(selectionCallback)
        assert isBool(alphabeticOptions)

        assert len(menuItems) > 0

        self.menu = "{!"

        # invalidSelectionCallback may be:
        #  "DEFAULT" - an internal hack to bind to self.menu
        #  None      - meaning an invalid selection defers to a later cmdHandler
        #  Func      - a client-supplied invalidSelectionCallback
        if invalidSelectionCallback == "DEFAULT":
            invalidSelectionCallback = lambda clientId, remaining: defaultInvalidSelectionCallback(clientId, self.menu)
        if isDefined(invalidSelectionCallback):
            assert isFunc(invalidSelectionCallback)

        self.prompt = ""
        self.cmdMap = CmdMap(invalidSelectionCallback)

        menuIndex = 1

        for item in menuItems:
            if isString(item):
                self.menu += item + endl
                continue

            assert isTuple(item)  # item wasn't string
            assert len(item) == 2

            (itemDesc, itemValue) = item

            assert isString(itemDesc)

            itemLabel = menuIndex
            if alphabeticOptions:
                itemLabel = chr(96 + menuIndex)

            self.menu += " {FC%s{FG) - {FU%s" % (itemLabel, itemDesc) + endl

            def getItemSelectedFunction(selectedValue):
                return lambda clientId, remaining: selectionCallback(clientId, selectedValue)

            self.cmdMap.addCmd("%s" % itemLabel, getItemSelectedFunction(itemValue))
            menuIndex += 1

        self.menu += "{@"
Example #20
0
 def test_promptPushPopNWorks( self ):
     self.prompt.pushPrompt( lambda x: "bob" )
     self.prompt.pushPrompt( lambda x: "bob" )
     self.prompt.popPrompt( 2 )
     self.assert_( isString( self.prompt.prompt( None )) )
Example #21
0
 def test_promptEmptyReturnsEpsilon(self):
     self.assert_(isString(self.prompt.prompt(None)))
Example #22
0
 def test_promptPushPopNWorks(self):
     self.prompt.pushPrompt(lambda x: "bob")
     self.prompt.pushPrompt(lambda x: "bob")
     self.prompt.popPrompt(2)
     self.assert_(isString(self.prompt.prompt(None)))
Example #23
0
 def test_menuGenerates( self ):
     self.assert_( isString( self.menu1.menu ) )
     self.assert_( isString( self.menu2.menu ) )
     self.assert_( isString( self.menu5.menu ) )
Example #24
0
 def test_promptGenerates( self ):
     self.assert_( isString( self.menu1.prompt ) )
     self.assert_( isString( self.menu2.prompt ) )
     self.assert_( isString( self.menu5.prompt ) )