예제 #1
0
def main():
    """The main entrypoint for program when run standalone."""

    # CLI Arguments
    parser = ArgumentParser()
    parser.add_argument(
        '-c',
        '--config',
        dest='config',
        type=open,
        default=Config.default_config,
        help='Specify a custom configuration file to load.',
    )

    # Get the arguments
    args = parser.parse_args()

    # Keep trying to load the configuration until it either explodes
    # spectacularly or works.
    while True:
        try:
            Config.load_from_yaml(args.config)
        except Exception as e:
            print(f'There was an error getting the configuration:\n\t{e}\n')
            print('Would you like to')
            print('\t(1) Exit')
            print('\t(2) Generate the default configuration (recommended)')
            response = prompt.regex(
                '^[12]$',
                prompt='Enter one of the keys in parentheses: ',
            )
            if response.string == '1':
                exit(0)
            else:
                Config.create_default_config()
        else:
            # If everything worked, then we can proceed
            break

    # There are multiple threads so this allows them to communicate
    messages = Queue()

    if Config.get('libvisplaygui'):
        gui_thread = Thread(target=libvisplaygui.init_gui)
        gui_thread.daemon = True
        gui_thread.start()
        sleep(2)

    if not Config.get('sources'):
        raise KeyError('No sources found in configuration file.')

    with open(Config.sources) as source_file:
        sources = get_sources_list(source_file)
        media.find_and_play(
            messages,
            playable_generator(sources, messages),
            Config.get('libvisplaygui', False),
        )
예제 #2
0
def test_regex(input_patch):
    import re
    response = "1. x=9"
    real_match = re.match(r"[0-9]\.\s([a-z])=([0-9])", response)
    input_patch.do(response)
    prompt_match = prompt.regex("[0-9]\\.\\s([a-z])=([0-9])")
    assert prompt_match is not None
    assert prompt_match.group() == real_match.group()
    assert prompt_match.groups() == real_match.groups()
    assert prompt_match.re == real_match.re
    assert prompt_match.span() == real_match.span()
    assert prompt_match.string == real_match.string
예제 #3
0
def ask(
    text: str = config.ANSWER_PATTERN,
    pattern: str = '.*',
) -> str:
    """Ask answer and return.

    Parameters:
        pattern: regex pattern for check correctly user answer
        text: question text

    Returns:
        Player's answer
    """
    answer = prompt.regex(pattern, prompt=text)
    return answer.group()
예제 #4
0
def test_regex():
    assert prompt.regex("foo", empty=True) is None
    assert prompt.regex("foo", default="foo").group(0) == 'foo'
예제 #5
0
def test_regex():
    assert prompt.regex("foo", empty=True) is None