Exemple #1
0
def main():
    from percol import __version__
    parser = OptionParser(usage = "Usage: %prog [options] [FILE]", version = "%prog {0}".format(__version__))
    setup_options(parser)
    options, args = parser.parse_args()

    if options.peep:
        exit(1)

    def exit_program(msg = None, show_help = True):
        if not msg is None:
            print("\n" + msg + "\n")
        if show_help:
            parser.print_help()
        exit(1)

    # get ttyname
    ttyname = options.tty or tty.get_ttyname()
    if not ttyname:
        exit_program("""Error: No tty name is given and failed to guess it from descriptors.
Maybe all descriptors are redirecred.""")

    # decide which encoding to use
    output_encoding = set_proper_locale(options)
    input_encoding = options.input_encoding

    with open(ttyname, "r+w") as tty_f:
        if not tty_f.isatty():
            exit_program("Error: {0} is not a tty file".format(ttyname), show_help = False)

        filename = args[0] if len(args) > 0 else None

        if filename is None and sys.stdin.isatty():
            tty_f.write(INSTRUCTION_TEXT)
            exit_program(show_help = False)

        # read input
        try:
            candidates = read_input(filename, input_encoding, reverse=options.reverse)
        except KeyboardInterrupt:
            exit_program("Canceled", show_help = False)

        # setup actions
        import percol.actions as actions
        if (options.quote):
            acts = (actions.output_to_stdout_double_quote, )
        else:
            acts = (actions.output_to_stdout, actions.output_to_stdout_double_quote)

        # arrange finder class
        candidate_finder_class = action_finder_class = decide_match_method(options)

        def set_finder_attribute_from_option(finder_instance):
            finder_instance.lazy_finding = not options.eager
            finder_instance.case_insensitive = not options.case_sensitive

        def set_if_not_none(src, dest, name):
            value = getattr(src, name)
            if value is not None:
                setattr(dest, name, value)

        with Percol(descriptors = tty.reconnect_descriptors(tty_f),
                    candidates = candidates,
                    actions = acts,
                    finder = candidate_finder_class,
                    action_finder = action_finder_class,
                    query = options.query,
                    caret = options.caret,
                    index = options.index,
                    encoding = output_encoding) as percol:
            # load run-command file
            load_rc(percol, options.rcfile)
            # override prompts
            if options.prompt is not None:
                percol.view.__class__.PROMPT = property(lambda self: options.prompt)
            if options.right_prompt is not None:
                percol.view.__class__.RPROMPT = property(lambda self: options.right_prompt)
            # evalutate strings specified by the option argument
            if options.string_to_eval is not None:
                eval_string(percol, options.string_to_eval, locale.getpreferredencoding())
            # finder settings from option values
            set_finder_attribute_from_option(percol.model_candidate.finder)
            set_finder_attribute_from_option(percol.model_action.finder)
            # view settings from option values
            set_if_not_none(options, percol.view, 'prompt_on_top')
            set_if_not_none(options, percol.view, 'results_top_down')
            # enter main loop
            if options.auto_fail and percol.has_no_candidate():
                exit_code = percol.cancel_with_exit_code()
            elif options.auto_match and percol.has_only_one_candidate():
                exit_code = percol.finish_with_exit_code()
            else:
                exit_code = percol.loop()

        exit(exit_code)
Exemple #2
0
def main():
    from percol import __version__
    parser = OptionParser(usage="Usage: %prog [options] [FILE]",
                          version="%prog {0}".format(__version__))
    setup_options(parser)
    options, args = parser.parse_args()

    if options.peep:
        exit(1)

    def exit_program(msg=None, show_help=True):
        if not msg is None:
            print(msg)
        if show_help:
            parser.print_help()
        exit(1)

    # get ttyname
    ttyname = options.tty or tty.get_ttyname()
    if not ttyname:
        exit_program(
            error_message(
                """No tty name is given and failed to guess it from descriptors.
Maybe all descriptors are redirecred."""))

    # decide which encoding to use
    output_encoding = set_proper_locale(options)
    input_encoding = options.input_encoding

    def open_tty(ttyname):
        if six.PY2:
            return open(ttyname, "r+w")
        else:
            # See https://github.com/stefanholek/term/issues/1
            return open(ttyname, "wb+", buffering=0)

    with open_tty(ttyname) as tty_f:
        if not tty_f.isatty():
            exit_program(error_message(
                "{0} is not a tty file".format(ttyname)),
                         show_help=False)

        filename = args[0] if len(args) > 0 else None

        if filename and not os.access(filename, os.R_OK):
            exit_program(error_message("Cannot read a file '" + filename +
                                       "'"),
                         show_help=False)

        if filename is None and sys.stdin.isatty():
            tty_f.write(INSTRUCTION_TEXT.encode(output_encoding))
            exit_program(show_help=False)

        # read input
        try:
            candidates = read_input(filename,
                                    input_encoding,
                                    reverse=options.reverse)
        except KeyboardInterrupt:
            exit_program("Canceled", show_help=False)

        # setup actions
        import percol.actions as actions
        if (options.quote):
            acts = (actions.output_to_stdout_double_quote, )
        else:
            acts = (actions.output_to_stdout,
                    actions.output_to_stdout_double_quote)

        # arrange finder class
        candidate_finder_class = action_finder_class = decide_match_method(
            options)

        def set_finder_attribute_from_option(finder_instance):
            finder_instance.lazy_finding = not options.eager
            finder_instance.case_insensitive = not options.case_sensitive
            finder_instance.invert_match = options.invert_match

        def set_if_not_none(src, dest, name):
            value = getattr(src, name)
            if value is not None:
                setattr(dest, name, value)

        with Percol(descriptors=tty.reconnect_descriptors(tty_f),
                    candidates=candidates,
                    actions=acts,
                    finder=candidate_finder_class,
                    action_finder=action_finder_class,
                    query=options.query,
                    caret=options.caret,
                    index=options.index,
                    encoding=output_encoding) as percol:
            # load run-command file
            load_rc(percol, options.rcfile)
            # override prompts
            if options.prompt is not None:
                percol.view.__class__.PROMPT = property(
                    lambda self: options.prompt)
            if options.right_prompt is not None:
                percol.view.__class__.RPROMPT = property(
                    lambda self: options.right_prompt)
            # evalutate strings specified by the option argument
            if options.string_to_eval is not None:
                eval_string(percol, options.string_to_eval,
                            locale.getpreferredencoding())
            # finder settings from option values
            set_finder_attribute_from_option(percol.model_candidate.finder)
            # view settings from option values
            set_if_not_none(options, percol.view, 'prompt_on_top')
            set_if_not_none(options, percol.view, 'results_top_down')
            # enter main loop
            if options.auto_fail and percol.has_no_candidate():
                exit_code = percol.cancel_with_exit_code()
            elif options.auto_match and percol.has_only_one_candidate():
                exit_code = percol.finish_with_exit_code()
            else:
                exit_code = percol.loop()

        exit(exit_code)
Exemple #3
0
def main():
    from percol import __version__
    parser = OptionParser(usage = "Usage: %prog [options] [FILE]", version = "%prog {0}".format(__version__))
    setup_options(parser)
    options, args = parser.parse_args()

    if options.peep:
        sys.exit(1)

    def exit_program(msg = None, show_help = True):
        if not msg is None:
            print(msg)
        if show_help:
            parser.print_help()
        sys.exit(1)

    # get ttyname
    ttyname = options.tty or tty.get_ttyname()
    if not ttyname:
        exit_program(error_message("""No tty name is given and failed to guess it from descriptors.
Maybe all descriptors are redirecred."""))

    # decide which encoding to use
    output_encoding = set_proper_locale(options)
    input_encoding = options.input_encoding

    def open_tty(ttyname):
        if six.PY2:
            return open(ttyname, "r+w")
        else:
            # See https://github.com/stefanholek/term/issues/1
            return open(ttyname, "wb+", buffering=0)

    with open_tty(ttyname) as tty_f:
        if not tty_f.isatty():
            exit_program(error_message("{0} is not a tty file".format(ttyname)),
                         show_help = False)

        filename = args[0] if len(args) > 0 else None

        if filename and not os.access(filename, os.R_OK):
            exit_program(error_message("Cannot read a file '" + filename + "'"),
                         show_help=False)

        if filename is None and sys.stdin.isatty():
            tty_f.write(INSTRUCTION_TEXT.encode(output_encoding))
            exit_program(show_help = False)

        # read input
        try:
            candidates = read_input(filename, input_encoding, reverse=options.reverse, seperator=options.seperator)
        except KeyboardInterrupt:
            exit_program("Canceled", show_help = False)

        # setup actions
        import percol.actions as actions
        if (options.quote):
            acts = (actions.output_to_stdout_double_quote, )
        else:
            acts = (actions.output_to_stdout, actions.output_to_stdout_double_quote)

        # arrange finder class
        candidate_finder_class = action_finder_class = decide_match_method(options)

        def set_finder_attribute_from_option(finder_instance):
            finder_instance.lazy_finding = not options.eager
            finder_instance.case_insensitive = not options.case_sensitive
            finder_instance.invert_match = options.invert_match

        def set_if_not_none(src, dest, name):
            value = getattr(src, name)
            if value is not None:
                setattr(dest, name, value)

        with Percol(descriptors = tty.reconnect_descriptors(tty_f),
                    candidates = candidates,
                    actions = acts,
                    finder = candidate_finder_class,
                    action_finder = action_finder_class,
                    query = options.query,
                    caret = options.caret,
                    index = options.index,
                    encoding = output_encoding) as percol:
            # load run-command file
            load_rc(percol, options.rcfile)
            # seperator can now be set via command-line
            if options.seperator is not None:
                percol.view.__class__.FIELD_SEP = property(lambda self: options.seperator)
                percol.command.set_field_sep(options.seperator)
            # override prompts
            if options.prompt is not None:
                percol.view.__class__.PROMPT = property(lambda self: options.prompt)
            if options.right_prompt is not None:
                percol.view.__class__.RPROMPT = property(lambda self: options.right_prompt)
            

            # evalutate strings specified by the option argument
            if options.string_to_eval is not None:
                eval_string(percol, options.string_to_eval, locale.getpreferredencoding())
            # finder settings from option values
            set_finder_attribute_from_option(percol.model_candidate.finder)
            # view settings from option values
            set_if_not_none(options, percol.view, 'prompt_on_top')
            set_if_not_none(options, percol.view, 'results_top_down')
            # enter main loop
            if options.auto_fail and percol.has_no_candidate():
                exit_code = percol.cancel_with_exit_code()
            elif options.auto_match and percol.has_only_one_candidate():
                exit_code = percol.finish_with_exit_code()
            else:
                exit_code = percol.loop()

            if exit_code == 10:
                stack = [s+'\n' for s in percol.model.stack]
                # outfilename = input("Script file name: ")
                # print(outfilename)
                # outfile = open(outfilename,'w')
                # outfile.writelines(stack)
                # outfile.close()

        sys.exit(exit_code)