File size: 2,030 Bytes
5a58620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# parser.py
import ast

def get_category(node):
    """
    Determine the category of an AST node based on its type.
    """
    if isinstance(node, (ast.Import, ast.ImportFrom)):
        return 'import'
    elif isinstance(node, (ast.Assign, ast.AnnAssign, ast.AugAssign)):
        return 'assignment'
    elif isinstance(node, ast.FunctionDef):
        return 'function'
    elif isinstance(node, ast.AsyncFunctionDef):
        return 'async_function'
    elif isinstance(node, ast.ClassDef):
        return 'class'
    elif isinstance(node, ast.Expr):
        return 'expression'
    else:
        return 'other'

def parse_python_file(file_path):
    """
    Parse a Python file and return a list of its parts with categories, source code, and locations.

    Args:
        file_path (str): Path to the Python file to parse.

    Returns:
        list: A list of dictionaries, each containing 'category', 'source', and 'location'.
    """
    with open(file_path, 'r') as file:
        code = file.read()

    lines = code.splitlines(keepends=True)
    tree = ast.parse(code)

    parts = []
    prev_end = 0

    for stmt in tree.body:
        start_line = stmt.lineno
        end_line = getattr(stmt, 'end_lineno', start_line)

        if start_line > prev_end + 1:
            spacer_lines = lines[prev_end:start_line - 1]
            parts.append({
                'category': 'spacer',
                'source': ''.join(spacer_lines),
                'location': (prev_end + 1, start_line - 1)
            })

        stmt_lines = lines[start_line - 1:end_line]
        parts.append({
            'category': get_category(stmt),
            'source': ''.join(stmt_lines),
            'location': (start_line, end_line)
        })

        prev_end = end_line

    if prev_end < len(lines):
        remaining_lines = lines[prev_end:]
        parts.append({
            'category': 'spacer',
            'source': ''.join(remaining_lines),
            'location': (prev_end + 1, len(lines) + 1)
        })

    return parts