Ejemplo n.º 1
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
Ejemplo n.º 2
0
def test_print_match_interactive_mode():
    cfg = load_config()
    m = re.search(r'test', 'test')
    match = {'match': m, 'filename': 'test', 'line': '1337'}
    assert find_command.print_match(match, 1337, cfg=cfg, interactive=True) is None
Ejemplo n.º 3
0
def test_print_match_no_cfg():
    m = re.search(r'test', 'test')
    match = {'match': m, 'filename': 'test', 'line': '1337'}
    assert find_command.print_match(match, 1337) is None