示例#1
0
文件: bot.py 项目: flipbug/zoey
class Bot:

    input = ""
    welcomeMessage = "Hello, i am a bot."
    quitMessage = "Good bye"
    commandNotFoundMessage = "I don't know what you mean"

    _parser = None

    def __init__(self, input):
        patterns = self.fetchCommandPatterns()
        self._parser = Parser(patterns)
        if input:
            self.input = input
        else:
            print self.welcomeMessage

    def __del__(self):
        if not input:
            print self.quitMessage

    def fetchCommandPatterns(self):
        commandPatterns = {}
        for plugin in PluginProvider.plugins:
            if hasattr(plugin, 'commandPatterns'):
                commandPatterns.update(plugin.commandPatterns)
        return commandPatterns

    def start(self):
        if self.input:
            self.dispatchCommand(self.input)
        else:
            self.beginMainLoop()

    def beginMainLoop(self):
        while True:
            input = raw_input(">> ")
            self.dispatchCommand(input)

    def dispatchCommand(self, input):
        try:
            command = self._parser.getCommandFromInput(input)
            for plugin in PluginProvider.plugins:
                if command.getKeyword() in plugin.commandPatterns:
                    pluginObj = plugin()
                    return pluginObj.processCommand(command)
        except NotImplementedError:
            print commandNotFoundMessage
            return False