-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalyze.py
More file actions
93 lines (76 loc) · 3.74 KB
/
analyze.py
File metadata and controls
93 lines (76 loc) · 3.74 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
from typing import List, Dict, Optional, Union
from spice.analyzers.identation import detect_indentation
def analyze_file(file_path: str, selected_stats: Optional[List[str]] = None) -> Dict[str, Union[int, str, List[int]]]:
"""
Analyze a file and return only the requested stats.
Args:
file_path (str): Path to the file to analyze
selected_stats (list, optional): List of stats to compute. If None, compute all stats.
Valid stats are: "line_count", "function_count", "comment_line_count", "indentation_level"
Returns:
dict: Dictionary containing the requested stats and file information
Raises:
FileNotFoundError: If the file does not exist
ValueError: If invalid stats are requested
Exception: For other analysis errors
"""
# Validate file exists
if not os.path.exists(file_path):
raise FileNotFoundError(f"File not found: {file_path}")
# Validate file is a file (not a directory)
if not os.path.isfile(file_path):
raise ValueError(f"Path is not a file: {file_path}")
# Validate file extension
_, ext = os.path.splitext(file_path)
if not ext:
raise ValueError("File has no extension")
# Define valid stats
valid_stats = ["line_count", "function_count", "comment_line_count", "indentation_level"]
# default to all stats if none specified
if selected_stats is None:
selected_stats = valid_stats
else:
# Validate requested stats
invalid_stats = [stat for stat in selected_stats if stat not in valid_stats]
if invalid_stats:
raise ValueError(f"Invalid stats requested: {invalid_stats}. Valid stats are: {valid_stats}")
# initialize results with the file information
results = {
"file_name": os.path.basename(file_path),
"file_path": os.path.abspath(file_path),
"file_size": os.path.getsize(file_path),
"file_extension": ext
}
try:
# read the code file only once and load it into memory
with open(file_path, "r", encoding="utf-8") as file:
code = file.read()
# line count if requested
if "line_count" in selected_stats:
from spice.analyzers.count_lines import count_lines
results["line_count"] = count_lines(code)
# comment line count if requested
if "comment_line_count" in selected_stats:
from spice.analyzers.count_comment_lines import count_comment_lines
from utils.get_lexer import get_lexer_for_file
LexerClass = get_lexer_for_file(file_path)
lexer = LexerClass(source_code=code) # Pass source_code explicitly
results["comment_line_count"] = count_comment_lines(file_path)
# indentation analysis if requested
if "indentation_level" in selected_stats:
indentation_info = detect_indentation(code)
results["indentation_type"] = indentation_info["indent_type"]
results["indentation_size"] = indentation_info["indent_size"]
results["indentation_levels"] = indentation_info["levels"]
# function count if requested
if "function_count" in selected_stats:
from spice.analyzers.count_functions import count_functions
from utils.get_lexer import get_lexer_for_file
LexerClass = get_lexer_for_file(file_path)
lexer = LexerClass(source_code=code) # Pass source_code explicitly
results["function_count"] = count_functions(file_path)
return results
except Exception as e:
# Add context to any errors that occur during analysis
raise Exception(f"Error analyzing file {file_path}: {str(e)}")