Beispiel #1
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 )
Beispiel #2
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
Beispiel #3
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> " 
Beispiel #4
0
    def __init__(self,
                 menuPairs,
                 finishedCallback,
                 invalidSelectionCallback=None,
                 alphabeticOptions=False):
        """
         menuPairs: [], each element is string or (string, any type)
         finishedCallback: f( clientId, selectedValue )
         invalidSelectionCallback: f( clientId, remaining )
         """
        isList(menuPairs)
        isFunc(finishedCallback)

        if invalidSelectionCallback:
            isFunc(invalidSelectionCallback)
        else:
            invalidSelectionCallback = lambda clientId, remaining: defaultInvalidSelectionCallback(
                clientId, self.menuStr)

        self.menuStr = "{!"
        self.menuMap = CmdMap(invalidSelectionCallback)
        menuIndex = 1

        def finishMenuWithValue(value):
            return lambda clientId, remaining: finishMenu(
                clientId, finishedCallback, value)

        for item in menuPairs:
            if type(item) == str:
                self.menuStr += item + "\r\n"
                continue

            (menuItemDescription, menuItemValue) = item

            optionLabel = ""
            if alphabeticOptions:
                optionLabel = chr(96 + menuIndex)
            else:
                optionLabel = menuIndex

            isStr(menuItemDescription)
            self.menuStr = self.menuStr + " {FC%s{FG) - {FU%s\r\n" % (
                optionLabel, menuItemDescription)
            self.menuMap.addCmd("%s" % optionLabel,
                                finishMenuWithValue(menuItemValue))
            menuIndex += 1

        self.menuStr += "{@"
Beispiel #5
0
def _submitText( clientId, submitCallback, text ):
    assert isFunc( submitCallback)
    assert isString( text )
    
    popCmdHandler( clientId )

    submitCallback( clientId, text )
Beispiel #6
0
def getOneLine( clientId, submitCallback ):
    """
    activates a text input widget for clientId, which returns one line of text

    submitCallback: func( clientId, text )
    """
    
    assert isInt( clientId )
    assert isFunc( submitCallback)

    pushCmdHandler( clientId, CmdMap( lambda x, remaining: _submitText( x, submitCallback, remaining ) ) )
Beispiel #7
0
    def __init__( self, menuPairs, finishedCallback, invalidSelectionCallback = None, alphabeticOptions = False ):
        """
         menuPairs: [], each element is string or (string, any type)
         finishedCallback: f( clientId, selectedValue )
         invalidSelectionCallback: f( clientId, remaining )
         """
        isList( menuPairs)
        isFunc( finishedCallback)

        if invalidSelectionCallback:
            isFunc( invalidSelectionCallback )
        else:
            invalidSelectionCallback = lambda clientId, remaining: defaultInvalidSelectionCallback( clientId, self.menuStr )

        self.menuStr = "{!"
        self.menuMap = CmdMap( invalidSelectionCallback )
        menuIndex = 1

        def finishMenuWithValue( value ):
            return lambda clientId, remaining: finishMenu( clientId, finishedCallback, value )

        for item in menuPairs:
            if type(item) == str:
                self.menuStr += item + "\r\n"
                continue

            ( menuItemDescription, menuItemValue ) = item

            optionLabel = ""
            if alphabeticOptions:
                optionLabel = chr(96 + menuIndex)
            else:
                optionLabel = menuIndex
                
            isStr( menuItemDescription)
            self.menuStr = self.menuStr + " {FC%s{FG) - {FU%s\r\n" % ( optionLabel, menuItemDescription )
            self.menuMap.addCmd( "%s" % optionLabel, finishMenuWithValue( menuItemValue ) )
            menuIndex += 1

        self.menuStr += "{@"
Beispiel #8
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)
Beispiel #9
0
    def pushPrompt(self, promptFunc):
        assert isFunc(promptFunc)

        self.prompts.append(promptFunc)
Beispiel #10
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 += "{@"
Beispiel #11
0
 def checkFind( o, s ):
     self.assert_( isTuple( o.cmdMap.find(s) ) )
     self.assert_( isFunc( o.cmdMap.find(s)[0] ) )
Beispiel #12
0
    def pushPrompt(self, promptFunc ):
        assert isFunc( promptFunc )

        self.prompts.append( promptFunc )