Ejemplo n.º 1
0
def test_import_no_match(all_filters, ast_test_code):

    f = all_filters['import']
    patterns = [
        pattern.compile('notimported'),
    ]

    for node in ast_test_code:
        assert not f.match(node, patterns)
Ejemplo n.º 2
0
def test_docstring_no_match(all_filters, ast_test_code):

    f = all_filters['doc']
    patterns = [
        pattern.compile('nomatches'),
    ]

    for node in ast_test_code:
        assert not f.match(node, patterns)
Ejemplo n.º 3
0
def test_call_no_match(all_filters, ast_test_code):

    f = all_filters['call']
    patterns = [
        pattern.compile('sauce'),
    ]

    for node in ast_test_code:
        assert not f.match(node, patterns)
Ejemplo n.º 4
0
def test_docstring(all_filters, ast_test_code):

    f = all_filters['doc']
    patterns = [
        pattern.compile('docstring'),
    ]

    for node in ast_test_code:
        if isinstance(node, ast.Module):
            assert f.match(node, patterns)
        else:
            assert not f.match(node, patterns)
Ejemplo n.º 5
0
def test_call(all_filters, ast_test_code):

    f = all_filters['call']
    patterns = [
        pattern.compile('juice'),
    ]
    count = 0

    for node in ast_test_code:
        if f.match(node, patterns):
            assert isinstance(node, (ast.Call))
            count += 1

    assert count == 1
Ejemplo n.º 6
0
def test_import(all_filters, ast_test_code):

    f = all_filters['import']
    patterns = [
        pattern.compile('mything'),
    ]

    count = 0
    for node in ast_test_code:
        if f.match(node, patterns):
            count += 1
            assert isinstance(node, (ast.Import, ast.ImportFrom))

    assert count == 2
Ejemplo n.º 7
0
def search(args):

    start = time.time()

    ignore_dirs = [pattern.compile(i_d) for i_d in args.ignore_dir]
    ast_walker = astutils.ASTWalker(args.paths, ignore_dirs)
    files_with_matches = set()
    activated_filters = filters.get_active_filters(args)

    if len(activated_filters) == 0:
        LOG.info("No filters were provided. Using all filters")
        activated_filters = filters.get_all_filters()

    patterns = pattern.matchers(args)

    node_count = 0

    for file_count, (file_path, nodes) in enumerate(ast_walker.walk()):
        for file_node_count, node in enumerate(nodes):
            for f in activated_filters:
                if f.match(node, patterns):
                    if file_path not in files_with_matches:
                        files_with_matches.add(file_path)
                        if args.files_with_matches:
                            print(file_path)
                        else:
                            first = len(files_with_matches) == 1
                            print_file_path(file_path, first=first)

                    if not args.files_with_matches:
                        display_result(f, file_path, node)
        node_count += file_node_count

    if args.show_stats:
        seconds = round(time.time() - start, 3)
        print("**STATS**")
        print("Ran for {} seconds".format(seconds))
        print("Parsed {} Python files ({}/s)".format(
            file_count, round(file_count / seconds, 2)))
        print("Visited {} AST nodes ({}/s)".format(
            node_count, round(node_count / seconds, 2)))
        for pat in patterns:
            print("Ran regular expression '{}' {} times.".format(
                pat.pat, pat.count))