def describeCommand(stanzas, key, useSimpleSyntax):

    # pair of (example, comment)
    description = {
        'name': None,
        'shortdesc': None,
        'details': None,
        'syntax': None,
        'examples': None
    }
    command = key
    if not key.endswith("-command"):
        key += "-command"
    if key not in stanzas.stanzas:
        return None
    unexpanded = {}
    stanza = stanzas[key]
    syntax = cleanSyntax(
        recurseSyntax(command, stanzas, stanza, unexpanded, useSimpleSyntax))
    if useSimpleSyntax:
        exp = parser.getExp(syntax)
        syntax = exp.toSimpleRegex(True)
    syntax = stylizeVariables(syntax).replace("\n", "<br/>")

    examples = []
    for attr in stanza.keys():
        if attr.startswith("example"):
            example = getValue(stanza, attr)
            suffix = attr[len("example"):]
            commentid = "comment" + suffix
            comment = "Example usage"
            if commentid in stanza:
                comment = getValue(stanza, commentid)
            if "cheat" in suffix:  # put cheatsheet examples at the top
                examples.insert(0, (example, comment))
            else:
                examples.append((example, comment))

    description['name'] = saxutils.escape(key[0:-8])
    description['shortdesc'] = removeWhitespaces(
        stylizeVariables(getValue(stanza, "shortdesc")))
    description['details'] = removeWhitespaces(
        stylizeVariables(getValue(stanza, "description"))).replace("\n", " ")
    description['syntax'] = syntax
    description['examples'] = examples
    description['aliases'] = getValue(stanza, "alias", "")
    description['related'] = getValue(stanza, "related", "")
    description['category'] = getValue(stanza, "category", "")

    # ack! we have no short description
    if len(description['shortdesc']) == 0:
        # if details is short, use that
        if len(description['details']) < 100:
            description['shortdesc'] = description['details']
            description['details'] = ''
        else:
            # otherwise give generic shortdesc
            description['shortdesc'] = 'See details below'

    return description
def argTypeahead(output, sessionKey, namespace, user, bnf, search):
    try:
        commandAndArgs = utils.getLastCommand(search, None)
        if commandAndArgs != None:
            cmd, args = commandAndArgs
            typeahead = []
            stanza = cmd + '-command'
            s = describer.cleanSyntax(
                describer.recurseSyntax(
                    stanza, bnf, bnf[stanza], {}, True, 0,
                    1500))  # recurse syntax up to 1500 chars
            e = parser.getExp(s)

            tokens = {}
            hasFields = False
            parser.getTokens(e, tokens)
            getvalue = False
            for a, v in tokens.items():
                if a == cmd: continue

                if a.startswith('<') and a.endswith(
                        '>') and 'field' in a.lower() or v == '<field>':
                    hasFields = True

                if args.endswith('='):
                    args = args[:-1]
                    getvalue = True
                b1, replacement, b2 = getReplacement(args, a)
                # only show keywords when we match because we have so low confidence about their correctness in any given spot of a search command
                if replacement != '':
                    prev = len(replacement) + 1
                    if prev > len(args) or not args[-prev].isalpha():
                        #print "%s\t: %s ('%s')" % (a,v, replacement)
                        if getvalue:
                            if v == '<bool>':
                                v = ['true', 'false']
                            a = v
                            v = 'datatype'
                        if isinstance(a, list):
                            for val in a:
                                typeahead.append((val, 'choice', replacement))
                        else:
                            if isinstance(v, list):
                                v = '<list>'
                            typeahead.append((a, v, replacement))

            output['has_field_args'] = hasFields
            output['arg_typeahead'] = typeahead

    except Exception, e:
        msg = str(e) + traceback.format_exc()
        output['notices'].insert(0, msg)
示例#3
0
def describeCommand(stanzas, key, useSimpleSyntax):

    # pair of (example, comment)
    description = {'name':None, 'shortdesc':None, 'details':None, 'syntax':None, 'examples': None }
    command = key
    if not key.endswith("-command"):
        key += "-command"
    if key not in stanzas.stanzas:
        return None
    unexpanded = {}
    stanza = stanzas[key]
    syntax = cleanSyntax(recurseSyntax(command, stanzas, stanza, unexpanded, useSimpleSyntax))
    if useSimpleSyntax:
        exp = parser.getExp(syntax)
        syntax = exp.toSimpleRegex(True)        
    syntax = stylizeVariables(syntax).replace("\n", "<br/>")    

    examples = []
    for attr in stanza.keys():
        if attr.startswith("example"):
            example = getValue(stanza, attr)
            suffix = attr[len("example"):]
            commentid = "comment" + suffix
            comment = "Example usage"
            if commentid in stanza:
                comment = getValue(stanza,commentid)
            if "cheat" in suffix: # put cheatsheet examples at the top
                examples.insert(0, (example, comment))
            else:
                examples.append((example, comment))

    description['name'] = saxutils.escape(key[0:-8])
    description['shortdesc'] = removeWhitespaces(stylizeVariables(getValue(stanza, "shortdesc")))
    description['details'] = removeWhitespaces(stylizeVariables(getValue(stanza, "description"))).replace("\n", " ")
    description['syntax'] = syntax
    description['examples'] = examples
    description['aliases'] = getValue(stanza, "alias", "")
    description['related'] = getValue(stanza, "related", "")
    description['category'] = getValue(stanza, "category", "")
    
    # ack! we have no short description
    if len(description['shortdesc']) == 0:
        # if details is short, use that
        if len(description['details']) < 100:
            description['shortdesc'] = description['details']
            description['details'] = ''
        else:
            # otherwise give generic shortdesc
            description['shortdesc'] = 'See details below'
    
    return description
def argTypeahead(output, sessionKey, namespace, user, bnf, search):
    try:
        commandAndArgs = utils.getLastCommand(search, None)
        if commandAndArgs != None:
            cmd, args = commandAndArgs
            typeahead = []
            stanza = cmd + '-command'
            s = describer.cleanSyntax(describer.recurseSyntax(stanza, bnf, bnf[stanza], {}, True, 0, 1500)) # recurse syntax up to 1500 chars
            e = parser.getExp(s)

            tokens = {}
            hasFields = False
            parser.getTokens(e, tokens)
            getvalue = False
            for a,v in tokens.items():
                if a == cmd: continue

                if a.startswith('<') and a.endswith('>') and 'field' in a.lower() or v == '<field>':
                    hasFields = True

                if args.endswith('='):
                    args = args[:-1]
                    getvalue = True
                b1, replacement, b2 = getReplacement(args, a)
                # only show keywords when we match because we have so low confidence about their correctness in any given spot of a search command
                if replacement != '':
                    prev = len(replacement)+1
                    if prev > len(args) or not args[-prev].isalpha():
                        #print "%s\t: %s ('%s')" % (a,v, replacement)
                        if getvalue:
                            if v == '<bool>':
                                v = ['true','false']
                            a = v
                            v = 'datatype'
                        if isinstance(a, list):
                            for val in a:
                                typeahead.append((val,'choice', replacement))
                        else:
                            if isinstance(v, list):
                                v = '<list>'
                            typeahead.append((a,v, replacement))

            output['has_field_args'] = hasFields
            output['arg_typeahead'] = typeahead

    except Exception, e:
        msg = str(e) + traceback.format_exc()
        output['notices'].insert(0,msg)