def help(output, bnf, sessionKey, namespace, user, search, usersquery):
    """did you mean ________?"""
    # if the user entered unknown search commands, suggest some
    #output['notices'].extend(didYouMeanCommands(bnf, search))

    if '|' in usersquery:
        #start = time.time()  #### DEBUG
        suggestedcommands, othercommands = didYouMeanCommands(
            bnf, search, user, namespace)
        #logger.error("SHELPER TIMING %s DIDYOUMEAN: commands=%6f" % (sessionKey, time.time() - start)) #### DEBUG

        s = usersquery.strip()
        s = s[:s.rindex('|')].strip()
        # make triplets of (command, description, replacement)
        output['autonexts'] = [(x, utils.getAttr(bnf, x, "shortdesc",
                                                 ""), s + " | " + x)
                               for x in suggestedcommands]
        output['nexts'] = [(x, utils.getAttr(bnf, x, "shortdesc",
                                             ""), s + " | " + x)
                           for x in othercommands]

    #start = time.time()  #### DEBUG
    # if the user entered fields that match very closely to fields in fields.conf, suggest them
    output['notices'].extend(
        didYouMeanFields(sessionKey, user, namespace, bnf, search))
Beispiel #2
0
def help(output, bnf, sessionKey, namespace, user, search, usersquery):
    """did you mean ________?"""
    # if the user entered unknown search commands, suggest some
    #output['notices'].extend(didYouMeanCommands(bnf, search))

    if '|' in usersquery:
        #start = time.time()  #### DEBUG            
        suggestedcommands, othercommands = didYouMeanCommands(bnf, search)
        #logger.error("SHELPER TIMING %s DIDYOUMEAN: commands=%6f" % (sessionKey, time.time() - start)) #### DEBUG
        
        s = usersquery.strip()
        s = s[:s.rindex('|')].strip()
        # make triplets of (command, description, replacement)
        output['autonexts'] = [(x, utils.getAttr(bnf, x, "shortdesc",""), s + " | " + x) for x in suggestedcommands]
        output['nexts'] = [(x, utils.getAttr(bnf, x, "shortdesc",""), s + " | " + x) for x in othercommands]

    #start = time.time()  #### DEBUG            
    # if the user entered fields that match very closely to fields in fields.conf, suggest them
    output['notices'].extend(didYouMeanFields(sessionKey, user, namespace, bnf, search))
def nextCommand(output, sessionKey, namespace, user, search, usersquery, queryprefix, aliasMap, bnf, showargs):

    ## overallstart = start = time.time()
    ## timing_last_command = 0
    ## timing_all_commands = 0
    ## timing_get_next_data = 0
    ## timing_get_args = 0
    ## timing_add_commands = 0
    ## timing_past_matches = 0
    ## timing_sort_past_matches = 0
    
    atPipe = False

    # if search ends in "|", don't give args for last command, but
    # give next information from previous commandd
    if search[-1] == "|":
        search = search[:-1]
        showargs = False
        atPipe = True
        
    nextcommands = []
    typeaheadcommands = []
    
    commandAndArgs = utils.getLastCommand(search, aliasMap)
    
    ###################
    ## now = time.time()
    ## timing_last_command = now - start
    ## start = now
    ###################

    
    if commandAndArgs == None:
        # list all generating commands.
        # make a list() copy so we don't trash it by adding search
        typeaheadcommands = list(utils.getAllCommands(bnf, user, namespace, True))

        ###################
        ## now = time.time()
        ## timing_all_commands = now - start
        ## start = now
        ###################

        
    else:        
        command, args = commandAndArgs
        data, pastsearches = next.getNextData(user, bnf, sessionKey, namespace)

        ###################
        ## now = time.time()
        ## timing_get_next_data = now - start
        ## start = now
        ###################

        
        for datum in data:
            if datum['command'] == command:
                typeaheadcommands = [x for x,y in datum['nextcommands'] if x != "<RUN>"]
                if showargs:

                    matchingargs = []
                    fs = fuzzSearch(usersquery)
                    for arg, perc in datum['args']:
                        replacement = "%s | %s %s" % (queryprefix, command, arg)
                        fr = fuzzSearch(replacement)
                        if fr.startswith(fs) and fr != fs:
                            matchingargs.append((arg,perc))
                    output['args'] =  matchingargs
                    #output['args'] =  datum['args']
                break

        ###################
        ## now = time.time()
        ## timing_get_args = now - start
        ## start = now
        ###################
            
        # now add in all commands that were not already added
        if command in aliasMap:
            # adding all the other commands not already added
            for thiscommand in utils.getAllCommands(bnf, user, namespace):
                if thiscommand not in nextcommands and thiscommand not in typeaheadcommands:
                    nextcommands.append(thiscommand)

        ###################
        ## now = time.time()
        ## timing_add_commands = now - start
        ## start = now
        ###################


        # look for pastsearches that the current search is a subset of  (like firefox url autocomplete looking for any term)
        usersearch = normalizeSearch(search)

        # if user didn't enter anything don't match on "search" or "search *", just get most recent
        if usersearch == "" or usersearch == "*":
            pastMatches = [userifySearch(p) for p in pastsearches]
        else:
            pastMatches = [userifySearch(pastsearch) for pastsearch in pastsearches if normalizedSearchMatch(True, usersquery, pastsearch)]
            pastMatches.extend([userifySearch(pastsearch) for pastsearch in pastsearches if normalizedSearchMatch(False, usersquery, pastsearch)])

        ###################
        ## now = time.time()
        ## timing_past_matches = now - start
        ## start = now
        ###################
            
        # dedup
        pastMatches = sorted(list(set(pastMatches)), key=pastMatches.index)         
        output['autocomplete'] = pastMatches[:10] # just the 10 most recent
        output['autocomplete_match'] = usersearch

        ###################
        ## now = time.time()
        ## timing_sort_past_matches = now - start
        ## start = now
        ###################


    # new.  only show next command if we aren't showing the args for the current command.
    # use will see next commands when they type "|"
    if atPipe:
        # keep only those that alias to themselves -- i.e., don't show aliases
        nextcommands = [x for x in nextcommands if aliasMap.get(x, '') == x]
        typeaheadcommands = [x for x in typeaheadcommands if aliasMap.get(x, '') == x]
        s = usersquery.strip()
        if '|' in s:
            s = s[:s.rindex('|')].strip()
        # make triplets of (command, description, replacement)
        output['autonexts'] = [(x, utils.getAttr(bnf,x,"shortdesc",""), s + " | " + x) for x in typeaheadcommands]
        output['nexts'] = [(x, utils.getAttr(bnf,x,"shortdesc",""), s + " | " + x) for x in nextcommands]
def nextCommand(output, sessionKey, namespace, user, search, usersquery,
                queryprefix, aliasMap, bnf, showargs):

    ## overallstart = start = time.time()
    ## timing_last_command = 0
    ## timing_all_commands = 0
    ## timing_get_next_data = 0
    ## timing_get_args = 0
    ## timing_add_commands = 0
    ## timing_past_matches = 0
    ## timing_sort_past_matches = 0

    atPipe = False

    # if search ends in "|", don't give args for last command, but
    # give next information from previous commandd
    if search[-1] == "|":
        search = search[:-1]
        showargs = False
        atPipe = True

    nextcommands = []
    typeaheadcommands = []

    commandAndArgs = utils.getLastCommand(search, aliasMap)

    ###################
    ## now = time.time()
    ## timing_last_command = now - start
    ## start = now
    ###################

    if commandAndArgs == None:
        # list all generating commands.
        # make a list() copy so we don't trash it by adding search
        typeaheadcommands = list(
            utils.getAllCommands(bnf, user, namespace, True))

        ###################
        ## now = time.time()
        ## timing_all_commands = now - start
        ## start = now
        ###################

    else:
        command, args = commandAndArgs
        data, pastsearches = next.getNextData(user, bnf, sessionKey, namespace)

        ###################
        ## now = time.time()
        ## timing_get_next_data = now - start
        ## start = now
        ###################

        for datum in data:
            if datum['command'] == command:
                typeaheadcommands = [
                    x for x, y in datum['nextcommands'] if x != "<RUN>"
                ]
                if showargs:

                    matchingargs = []
                    fs = fuzzSearch(usersquery)
                    for arg, perc in datum['args']:
                        replacement = "%s | %s %s" % (queryprefix, command,
                                                      arg)
                        fr = fuzzSearch(replacement)
                        if fr.startswith(fs) and fr != fs:
                            matchingargs.append((arg, perc))
                    output['args'] = matchingargs
                    #output['args'] =  datum['args']
                break

        ###################
        ## now = time.time()
        ## timing_get_args = now - start
        ## start = now
        ###################

        # now add in all commands that were not already added
        if command in aliasMap:
            # adding all the other commands not already added
            for thiscommand in utils.getAllCommands(bnf, user, namespace):
                if thiscommand not in nextcommands and thiscommand not in typeaheadcommands:
                    nextcommands.append(thiscommand)

        ###################
        ## now = time.time()
        ## timing_add_commands = now - start
        ## start = now
        ###################

        # look for pastsearches that the current search is a subset of  (like firefox url autocomplete looking for any term)
        usersearch = normalizeSearch(search)

        # if user didn't enter anything don't match on "search" or "search *", just get most recent
        if usersearch == "" or usersearch == "*":
            pastMatches = [userifySearch(p) for p in pastsearches]
        else:
            pastMatches = [
                userifySearch(pastsearch) for pastsearch in pastsearches
                if normalizedSearchMatch(True, usersquery, pastsearch)
            ]
            pastMatches.extend([
                userifySearch(pastsearch) for pastsearch in pastsearches
                if normalizedSearchMatch(False, usersquery, pastsearch)
            ])

        ###################
        ## now = time.time()
        ## timing_past_matches = now - start
        ## start = now
        ###################

        # dedup
        pastMatches = sorted(list(set(pastMatches)), key=pastMatches.index)
        output['autocomplete'] = pastMatches[:10]  # just the 10 most recent
        output['autocomplete_match'] = usersearch

        ###################
        ## now = time.time()
        ## timing_sort_past_matches = now - start
        ## start = now
        ###################

    # new.  only show next command if we aren't showing the args for the current command.
    # use will see next commands when they type "|"
    if atPipe:
        # keep only those that alias to themselves -- i.e., don't show aliases
        nextcommands = [x for x in nextcommands if aliasMap.get(x, '') == x]
        typeaheadcommands = [
            x for x in typeaheadcommands if aliasMap.get(x, '') == x
        ]
        s = usersquery.strip()
        if '|' in s:
            s = s[:s.rindex('|')].strip()
        # make triplets of (command, description, replacement)
        output['autonexts'] = [(x, utils.getAttr(bnf, x, "shortdesc",
                                                 ""), s + " | " + x)
                               for x in typeaheadcommands]
        output['nexts'] = [(x, utils.getAttr(bnf, x, "shortdesc",
                                             ""), s + " | " + x)
                           for x in nextcommands]