示例#1
0
def test_parse_int_string():
    """Test int string parser."""
    doc_example = "1 23 4-8 32 1"
    res = Util.parse_int_string(doc_example)
    assert sorted(res) == [1, 4, 5, 6, 7, 8, 23, 32]

    invalid_nums = "1 fish a 4 1 23-- 5 5 5 5 5 9"
    res = Util.parse_int_string(invalid_nums)
    assert sorted(res) == [1, 4, 5, 9]

    ranges = "1-5, 7-10, 9-12"
    res = Util.parse_int_string(ranges)
    assert sorted(res) == [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12]
示例#2
0
def _choose_entries():
    done = False
    while not done:
        num_string = input(textwrap.dedent(
            """
            Provide numbers of entries for this command.
            Invalid numbers will be ignored.
            Press enter with an empty line to go back to command menu.
            """))

        if len(num_string) == 0:
            done = True
            num_list = None
            break

        num_list = Util.parse_int_string(num_string)

        while True:
            answer = input(textwrap.dedent(
                """\
                Happy with {}?
                (If indices are too big/small, they'll be pulled out later.)
                (No will let you try again)
                [Yes/yes/y or No/no/n]
                """.format(num_list)))

            if len(answer) < 1:
                continue

            ans = answer.lower()[0]
            if ans == "y":
                done = True
                break

            elif ans == "n":
                break

    return num_list
示例#3
0
def _handle_command(command, config, command_options):
    # TODO use config exit status to return exit codes.
    if command == Config.Command.update_once.name:
        (res, msg) = config.update_once()

    elif command == Config.Command.update_forever.name:
        (res, msg) = config.update_forever()

    elif command == Config.Command.load.name:
        (res, msg) = config.load_state()

    elif command == Config.Command.list.name:
        (res, msg) = config.list()

    elif command == Config.Command.details.name:
        sub_index = _choose_sub(config)
        (res, msg) = config.details(sub_index)
        input("Press enter when done.")

    elif command == Config.Command.enqueue.name:
        sub_index = _choose_sub(config)

        done = False
        while not done:
            num_string = rawest_input(textwrap.dedent(
                """
                Provide numbers of entries to put in download queue.
                Invalid numbers will be ignored.
                Press enter with an empty line to go back to command menu.
                """))

            if len(num_string) == 0:
                done = True
                (res, msg) = (False, "Canceled enqueuing")
                break

            num_list = Util.parse_int_string(num_string)

            while True:
                answer = rawest_input(textwrap.dedent(
                    """\
                    Happy with {}?
                    (If indices are too big/small, they'll be pulled out later.)
                    (No will let you try again)
                    [Yes/yes/y or No/no/n]
                    """.format(num_list)))

                if len(answer) < 1:
                    continue

                ans = answer.lower()[0]
                if ans == "y":
                    (res, msg) = config.enqueue(sub_index, num_list)
                    done = True
                    break

                elif ans == "n":
                    break

    elif command == Config.Command.download_queue.name:
        sub_index = _choose_sub(config)
        (res, msg) = config.download_queue(sub_index)

    else:
        print("Unknown command!")
        print("Allowed commands:")
        for command in command_options:
            print("    {}: {}".format(command["return"], command["prompt"]))
        return

    if not res:
        print(msg)