Exemplo n.º 1
0
def test_show_all_matches():
    cfg = load_config()
    find_settings = load_settings()
    find_settings['dirs'] = [os.path.dirname(__file__)]
    find_settings['file_patterns'] = '*{file}'.format(file=os.path.basename(__file__))
    find_settings['pattern'] = 'test'
    assert execute_module_command(show_command, command=show_command.commands[0],
            command_line=[], cfg=cfg, find_settings=find_settings)
Exemplo n.º 2
0
def test_edit_at_least_one_match():
    cfg = load_config()
    cfg.set('edit', 'editor', 'echo')
    find_settings = load_settings()
    find_settings['dirs'] = [os.path.dirname(__file__)]
    find_settings['file_patterns'] = '*{file}'.format(file=os.path.basename(__file__))
    find_settings['pattern'] = 'test'
    assert execute_module_command(edit_command, command=edit_command.commands[0],
            command_line=['1'], cfg=cfg, find_settings=find_settings)
Exemplo n.º 3
0
def execute(parsed_args=None, cfg=None, logger=None, interactive=False, find_settings=load_settings(),
        *args, **kwargs):
    """ Executes the edit command. """

    if not cfg: return False

    # Aggregate all the matches into a generator
    matches = iter(())
    for d in find_settings['dirs']:
        matches = itertools.chain(matches, find_pattern_in_directory(pattern=find_settings['pattern'],
            directory=d, recursive=find_settings['recursive'], file_patterns=find_settings['file_patterns']))

    # If match numbers were specified, print the matches
    if parsed_args.numbers:
        match_numbers = set()
        for number in parsed_args.numbers:
            if _number_regex.match(number):
                match_numbers.add(int(number, 0))
            else:
                m = _range_regex.match(number)
                if m:
                    match_numbers.update([n for n in range(int(m.group('start'), 0), int(m.group('end'), 0) + 1)])
                else:
                    print('Invalid syntax: {numbers}\nValid syntax:\n\t<number>\n\t<start>-<end>\n\t<start>:<end>'.format(numbers=' '.join(parsed_args.numbers)))
                    return False

        numbers = sorted(match_numbers)
        for idx, match in enumerate(matches):
            if idx + 1 not in numbers: continue
            print_match(match, idx + 1, cfg=cfg, interactive=interactive)
            numbers.remove(idx + 1)
        if len(numbers) > 0:
            print('Invalid match number(s): {numbers}'.format(numbers=', '.join([str(n) for n in numbers])))
            return False
    else:
        # Otherwise, print all the matches
        try:
            for idx, match in enumerate(matches):
                print_match(match, idx + 1, cfg=cfg, interactive=interactive)
        except KeyboardInterrupt:
            pass

    return True
Exemplo n.º 4
0
def execute(parsed_args=None, cfg=None, logger=None, interactive=False, find_settings=load_settings(),
        *args, **kwargs):
    """ Executes the edit command. """

    if not cfg: return False

    # Load settings from config file
    editor = cfg.get('edit', 'EDITOR')
    line_num_option = cfg.get('edit', 'EDITOR_LINE_NUM_OPTION')

    # Aggregate all the matches into a generator
    matches = iter(())
    for d in find_settings['dirs']:
        matches = itertools.chain(matches, find_pattern_in_directory(pattern=find_settings['pattern'],
            directory=d, recursive=find_settings['recursive'], file_patterns=find_settings['file_patterns']))

    # Select the match with the match number that matches the supplied argument
    match_idx = parsed_args.number - 1
    try:
        match = list(itertools.islice(matches, match_idx, match_idx + 1))[0]
    except (IndexError, ValueError):
        print('Invalid match number: {num}'.format(num=parsed_args.number))
        return False

    # Open the match file in the editor
    return subprocess.call([editor, '{option}{num}'.format(option=line_num_option, num=match['line']),
        match['filename']]) == 0
Exemplo n.º 5
0
def test_edit_current_dir():
    cfg = load_config()
    find_settings = load_settings()
    find_settings['dirs'] = [os.path.dirname(__file__)]
    assert not execute_module_command(edit_command, command=edit_command.commands[0],
            command_line=['1'], cfg=cfg, find_settings=find_settings)