Esempio n. 1
0
 def __init__(self, commandList, actorName="ACTOR"):
     """! Generate a command set
     @param[in] commandList: a list of Command objects
     @param[in] actorName: string name of the actor
     """
     # turn list of commands into a dictionary
     self.commandDict = collections.OrderedDict()
     self.actorName = actorName
     for command in commandList:
         self.commandDict[command.commandName] = command
     self.createHelpCmd()
     self.commandMatchList = MatchList(valueList = self.commandDict.keys())
Esempio n. 2
0
 def uniqueMatch(self, matchList):
     """matchList: A RO MatchList object
     """
     matchList = MatchList(matchList)
     def onParse(tolken):
         kw = str(tolken[0])
         # see if keyword is in list, else raise a parse error
         try:
             fullKW = matchList.getUniqueMatch(kw)
         except ValueError:
             raise ParseError("%s not uniquely defined in %s"%(kw, str(matchList.valueList)))
         return fullKW
     return self._word.setParseAction(onParse)
Esempio n. 3
0
 def argMatchList(self):
     return MatchList(valueList = self.floatingArgDict.keys())
Esempio n. 4
0
class CommandSet(object):
    def __init__(self, commandList, actorName="ACTOR"):
        """! Generate a command set
        @param[in] commandList: a list of Command objects
        @param[in] actorName: string name of the actor
        """
        # turn list of commands into a dictionary
        self.commandDict = collections.OrderedDict()
        self.actorName = actorName
        for command in commandList:
            self.commandDict[command.commandName] = command
        self.createHelpCmd()
        self.commandMatchList = MatchList(valueList = self.commandDict.keys())
        # explicitly set the "help command"

    def createHelpCmd(self):
        """Create a help command add it to the command dict.
        """
        helpCmd = Command(
            commandName = "help",
            positionalArguments = [UniqueMatch(self.commandDict.keys(), nElements=(0,1), helpStr="If desired specify a specific command for which to receive help.")],
            helpStr="print help for a command or the whole command set"
        )
        self.commandDict[helpCmd.commandName] = helpCmd

    def getCommand(self, cmdName):
        """! Get a command in the set from a command name. Name may be abbreviated
        as long as it is unique to the command set.
        """
        return self.commandDict[self.commandMatchList.getUniqueMatch(cmdName)]

    def parse(self, cmdStr):
        """! Parse a command string.

        @param[in] cmdStr, the command string to be parsed
        @return ParsedCommand object
        """
        # use pyparsing instead? any advantage?
        # determine which command we are parsing
        try:
            cmdName, cmdArgs = cmdStr.split(" ", 1)
        except ValueError:
            # split didn't work (need more than one value to unpack)
            # means that no cmdArgs were passed!
            cmdName = cmdStr
            cmdArgs = ""
        cmdName = cmdName.strip()
        cmdArgs = cmdArgs.strip()
        cmdObj = self.getCommand(cmdName) # cmdName abbreviations allowed!
        return cmdObj.parse(cmdArgs)

    def toHTML(self, isSubCmdSet=False):
        htmlStr = ""
        if isSubCmdSet:
            headerSize = 4
        else:
            headerSize = 3
            htmlStr += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n'
            htmlStr += "<html>\n"
            htmlStr += "<head>\n"
            htmlStr += "<title>%s Commands</title>\n"%(self.actorName,)
            htmlStr += "</head>\n"
            htmlStr += "<body>\n"
            # create table of contents links
            htmlStr += "<h1>%s Commands</h1>\n"%(self.actorName,)
            htmlStr += "<ul>\n"
            for cmd in self.commandDict.itervalues():
                htmlStr += "<li><a href='#%s'>%s</a>\n"%(cmd.commandName, cmd.commandName.upper())
            htmlStr += "</ul>\n"
        for cmd in self.commandDict.itervalues():
            htmlStr += "%s\n"%(cmd.toHTML(headerSize=headerSize),)
        htmlStr += "</body>\n"
        htmlStr += "</html>\n"
        return htmlStr