Exemplo n.º 1
0
def commands_grammar():
    """Return a grammar for COMMAND_NAMES values."""
    def cmd2reg(cmd, subcmd=None, args=None):
        """layout automatization"""
        return (
            '(\s*  (?P<cmd>(' + '|'.join(COMMAND_NAMES[cmd]) + '))'
            + ('' if subcmd is None
               else ('\s+  (?P<subcmd>('+'|'.join(COMMAND_NAMES[subcmd]) + '))   \s*  '))
            + ('' if args   is None
               else ('\s+  (?P<args>('  +'|'.join(COMMAND_NAMES[args  ]) + '))   \s*  '))
            + ') |\n'
        )
    # get grammar, log it and return it
    grammar = (
          cmd2reg('sudo'   , 'subsudo' , 'args')
        + cmd2reg('quit'   , None      , None  )
        + cmd2reg('plugins', 'subpgnoa', None  )
        + cmd2reg('plugins', 'subpgarg', 'args')
        + cmd2reg('irc'    , None      , 'args')
        + cmd2reg('say'    , None      , 'args')
        + cmd2reg('help'   , None      , None  )
        + cmd2reg('operate', None      , None  )
    )
    LOGGER.debug('GRAMMAR:\n' + grammar)
    return pt_compile(grammar)
Exemplo n.º 2
0
def commands_grammar(config, commands=COMMAND_NAMES):
    """Return a grammar for given commands (dict command:aliases)
    that use given Configuration for field autocompletion.

    """
    def aliases(cmd):
        """access the aliases of given (sub)command.
        if not in commands dict, will use it as an iterable."""
        try:             return '|'.join(commands[cmd])
        except KeyError: return '|'.join(cmd)
    def cmd2reg(cmd, subcmd=None, args=None):
        """layout automatization"""
        return (
            '(\s*  (?P<cmd>(' + aliases(cmd) + '))'
            + ('' if subcmd is None
               else ('\s+  (?P<subcmd>('+ aliases(subcmd) + '))   \s*  '))
            + ('' if args   is None else  ('\s+  (?P<args>(.*))   \s*  '))
            + ') |\n'
        )
    # get grammar, log it and return it
    grammar = (
          cmd2reg('quit', None, None)
        + cmd2reg('help', None, None)
        + cmd2reg('conf', None, None)
        + cmd2reg('set', config.mutable_fields, True)
        + cmd2reg('get', config.all_fields, None)
        + cmd2reg('apply', None, None)
    )
    LOGGER.debug('PROMPT GRAMMAR:\n' + str(grammar))
    return pt_compile(grammar)
Exemplo n.º 3
0
def commands_grammar(config, commands=COMMAND_NAMES):
    """Return a grammar for given commands (dict command:aliases)
    that use given Configuration for field autocompletion.

    """
    def aliases(cmd):
        """access the aliases of given (sub)command.
        if not in commands dict, will use it as an iterable."""
        try:
            return '|'.join(commands[cmd])
        except KeyError:
            return '|'.join(cmd)

    def cmd2reg(cmd, subcmd=None, args=None):
        """layout automatization"""
        return ('(\s*  (?P<cmd>(' + aliases(cmd) + '))' +
                ('' if subcmd is None else
                 ('\s+  (?P<subcmd>(' + aliases(subcmd) + '))   \s*  ')) +
                ('' if args is None else
                 ('\s+  (?P<args>(.*))   \s*  ')) + ') |\n')

    # get grammar, log it and return it
    grammar = (cmd2reg('quit', None, None) + cmd2reg('help', None, None) +
               cmd2reg('conf', None, None) +
               cmd2reg('set', config.mutable_fields, True) +
               cmd2reg('get', config.all_fields, None) +
               cmd2reg('apply', None, None))
    LOGGER.debug('PROMPT GRAMMAR:\n' + str(grammar))
    return pt_compile(grammar)
Exemplo n.º 4
0
def commands_grammar():
    """Return a grammar for COMMAND_NAMES values."""

    def cmd2reg(cmd, subcmd=None, args=None):
        """layout automatization"""
        return (
            "(\s*  (?P<cmd>("
            + "|".join(COMMAND_NAMES[cmd])
            + "))"
            + ("" if subcmd is None else ("\s+  (?P<subcmd>(" + "|".join(COMMAND_NAMES[subcmd]) + "))   \s*  "))
            + ("" if args is None else ("\s+  (?P<args>(" + "|".join(COMMAND_NAMES[args]) + "))   \s*  "))
            + ") |\n"
        )

    # get grammar, log it and return it
    grammar = (
        cmd2reg("sudo", "subsudo", "args")
        + cmd2reg("quit", None, None)
        + cmd2reg("plugins", "subpgnoa", None)
        + cmd2reg("plugins", "subpgarg", "args")
        + cmd2reg("irc", None, "args")
        + cmd2reg("say", None, "args")
    )
    LOGGER.debug("GRAMMAR:\n" + grammar)
    return pt_compile(grammar)
Exemplo n.º 5
0
def commands_grammar():
    """Return a grammar for COMMAND_NAMES values."""
    def cmd2grm(cmd, subcmd=None, args=None):
        """layout automatization"""
        return ('(\s* (?P<' + cmd + '>(' + '|'.join(COMMANDS[cmd]) + '))' +
                ('' if subcmd is None else
                 ('\s+ (?P<' + subcmd + '>(' + '|'.join(SUBCOMMANDS[subcmd]) +
                  ')) \s* ')) + ('' if args is None else
                                 ('\s+ (?P<' + args + '>(' +
                                  '|'.join(ARGUMENTS[args]) + ')) \s* ')) +
                ') |\n')

    # get grammar, log it and return it
    global GRAMMAR_RAW
    GRAMMAR_RAW = (cmd2grm('request', 'agent', 'coords') +
                   cmd2grm('lists', 'player', None) +
                   cmd2grm('lists', 'agent', None) +
                   cmd2grm('help', None, None) + cmd2grm('quit', None, None))
    LOGGER.debug('GRAMMAR:\n' + GRAMMAR_RAW)
    return pt_compile(GRAMMAR_RAW)
Exemplo n.º 6
0
def commands_grammar():
    """Return a grammar for COMMAND_NAMES values."""
    def cmd2reg(cmd, subcmd=None, args=None):
        """layout automatization"""
        return ('(\s*  (?P<cmd>(' + '|'.join(COMMAND_NAMES[cmd]) + '))' +
                ('' if subcmd is None else
                 ('\s+  (?P<subcmd>(' + '|'.join(COMMAND_NAMES[subcmd]) +
                  '))   \s*  ')) +
                ('' if args is None else
                 ('\s+  (?P<args>(' + '|'.join(COMMAND_NAMES[args]) +
                  '))   \s*  ')) + ') |\n')

    # get grammar, log it and return it
    grammar = (cmd2reg('quit', None, None) +
               cmd2reg('plugins', 'subpgnoa', None) +
               cmd2reg('plugins', 'subpgarg', 'args') +
               cmd2reg('sudoers', 'subsudos', 'nick') +
               cmd2reg('irc', None, 'args') + cmd2reg('say', None, 'args') +
               cmd2reg('help', None, None) + cmd2reg('operate', None, None) +
               cmd2reg('save', None, None) + cmd2reg('debug', None, None))
    LOGGER.debug('GRAMMAR:\n' + grammar)
    return pt_compile(grammar)
Exemplo n.º 7
0
def commands_grammar():
    """Return a grammar for COMMAND_NAMES values."""
    def cmd2grm(cmd, subcmd=None, args=None):
        """layout automatization"""
        return (
            '(\s* (?P<'+cmd+'>(' + '|'.join(COMMANDS[cmd]) + '))'
            + ('' if subcmd is None
               else ('\s+ (?P<'+subcmd+'>(' + '|'.join(SUBCOMMANDS[subcmd]) + ')) \s* '))
            + ('' if args is None
               else ('\s+ (?P<'+args+'>('   + '|'.join(ARGUMENTS[args  ])   + ')) \s* '))
            + ') |\n'
        )
    # get grammar, log it and return it
    global GRAMMAR_RAW
    GRAMMAR_RAW = (
        cmd2grm('request', 'agent' , 'coords')
        + cmd2grm('lists', 'player',  None   )
        + cmd2grm('lists', 'agent' ,  None   )
        + cmd2grm('help' ,  None   ,  None   )
        + cmd2grm('quit' ,  None   ,  None   )
    )
    LOGGER.debug('GRAMMAR:\n' + GRAMMAR_RAW)
    return pt_compile(GRAMMAR_RAW)