Exemple #1
0
def main(args):
    # get the absolute path of the FastSearch directory
    fastSearchDir = os.path.abspath(args[0])[:os.path.abspath(args[0]).rfind(os.sep)] + os.sep
    
    # instantiate the exit code
    code = EXIT_CLEAN
    
    # instantiate pre-run update condition
    preRunUpdateCheck = False
    
    # declare options list
    optionsList = FileHandler.initializeOptionsList(fastSearchDir, [])
    colors = Output.Colors(optionsList)
    
    # FastSearch has been updated
    if len(args) > 3 and args[1] == '-updatesuccess':
        print colors.alert() + '\nFastSearch has been successfully updated to v' + str(Updater.CURRENT_VERSION) + '!' + colors.end()
        # display the post update message retrieved from the server
        if not (args[2] in ('None', '', None)):
            print colors.alert() + args[2:] + colors.end()
    
    # run a QuickSearch and return the results as a list and quit the application
    if len(args) > 1 and args[1][1:].lower() in ('r', 'return'):
        if len(args) > 2:
            optionsList = FileHandler.defaultList
            if len(args) > 3 and args[2][1:].lower() in ('d', 'deep', 'deepsearch', 'ds'):
                optionsList[0] = True
                start = 3
            else:
                start = 2
            
            # if the user requested a deep search but provided no string
            if args[start][1:] in ('d', 'deep', 'deepsearch', 'ds'):
                return EXIT_RESULTS_BAD, None
            
            string = args[start].lower()            
            for i in range(start + 1, len(args)):
                string += ' ' + args[i].lower()
            
            search = Search.Search(string, optionsList, colors)
            search.start()
            search.join()
            return EXIT_RESULTS, search.finish()
        else:
            return EXIT_RESULTS_BAD, None
    
    print colors.blue() + '\n::Welcome to FastSearch v' + str(Updater.CURRENT_VERSION) + ' by Alex Laird::' + colors.end()
    
    # if arguments are specified, use them as the search string
    if len(args) > 1 and not args[1] == '-updatesuccess':
        if args[1].startswith('-'):
            if len(args) == 2 and not args[1][1:].lower() in ('d', 'debug'):
                if args[1][1:].lower() in ('help', 'h'):
                    Output.help(colors)
                elif args[1][1:].lower() in ('update', 'updates', 'u'):
                    runUpdater(fastSearchDir, colors)
                elif args[1][1:].lower() in ('credit', 'credits', 'c'):
                    Output.credits(colors)
                elif args[1][1:].lower() in ('option', 'options', 'o', 'pref', 'preferences', 'setting', 'settings'):
                    optionsList = Options.options(fastSearchDir, optionsList, colors)
                else:
                    print colors.alert() + 'The command-line arguments were not valid. View the help menu for assistance.\n' + colors.end()
            # run a benchmark comparing a standard os.walk method (with no comparisons) to the FastSearch localWalk method
            elif args[1][1:].lower() in ('d', 'debug'):
                try:
                    code = debug(args, colors)
                except:
                    code = EXIT_DEBUG_ERROR
                
                return code
            else:
                print colors.alert() + 'The command-line arguments were not valid. View the help menu for assistance.\n' + colors.end()
        else:
            string = args[1].lower()
            for i in range(2, len(args)):
                string += ' ' + args[i].lower()
    
            # the search will be launched immedietly with the current working directory as root
            code = runSearch(fastSearchDir, string, optionsList, False, colors)[0]
    # if no arguments were specified
    elif len(args) == 1:
        if preRunUpdateCheck:
            simpleUpdateChecker(colors)

    looping = True
    results = None
        
    # loop until the user wants out
    while looping:
        # retreive a command from the user
        string = raw_input(colors.blue() + 'Type (s)earch, (o)ptions, (u)pdate, (h)elp, (c)redits, or (q)uit: ' + colors.end()).lower()
        
        # the user entered a number
        if string.strip('[]').isdigit():
            if not results == None:
                location = int(string.strip('[]')) - 1
                ref = 0
                # decrement the number as needed to keep it consistent with the correct results subarray
                if location > len(results[0]) - 1:
                    location -= len(results[0])
                    ref = 1
                if location > len(results[1]) - 1:
                    location -= len(results[1])
                    ref = 2
                
                # ensure that the location is/was a valid search result
                if location < 0 or location > len(results[ref]) - 1:
                    print colors.alert() + 'The number entered did not correspond to a search results. Try again.\n' + colors.end()
                
                # launch the search result
                print 'Opening ' + os.path.abspath(results[ref][location]) + ' ...'
                os.system('open ' + os.path.normpath(os.path.abspath(results[ref][location])).replace(' ', '\\ '))
                print ''
            else:
                print colors.alert() + '\nThat wasn\'t an option. Try again.\n' + colors.end()
            
            continue

        # the user wants to search
        if string in ('s', 'search', 'f', 'find'):
            # retreive the string to search for from the user
            string = raw_input(colors.blue() + 'Enter a search string: ' + colors.end()).lower()
            
            code, results = runSearch(fastSearchDir, string, optionsList, False, colors)
        # the user wants to check for updates
        elif string in ('u', 'update', 'updates'):
            runUpdater(fastSearchDir, colors)
        # the user wants help
        elif string in ('h', 'help'):
            Output.help(colors)
        # the user wants to see the credits
        elif string in ('c', 'credit', 'credits'):
            Output.credits(colors)
        # the user wants to specify search options
        elif string in ('o', 'options', 'p', 'pref', 'prefs', 'preferences', 'setting', 'settings'):
            optionsList = Options.options(fastSearchDir, optionsList, colors)
        # the user wants to end the program
        elif string in ('q', 'quit', 'e', 'exit', 'c', 'close', 'end'):
            # clean exit without errors
            code = EXIT_CLEAN
            looping = False
        # the user is an idiot
        else:
            print colors.alert() + '\nThat wasn\'t an option. Try again.\n' + colors.end()
    
    if not optionsList[6] == None:
        optionsList[6].close()
    
    if code == None:
        return EXIT_UNKNOWN_ERROR
    else:
        return code