-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalyze.py
More file actions
123 lines (104 loc) · 4.85 KB
/
analyze.py
File metadata and controls
123 lines (104 loc) · 4.85 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from InquirerPy import inquirer
from utils.get_translation import get_translation
from spice.analyze import analyze_file
def analyze_to_dict(file, selected_stats=None):
"""
Analyze the given file and return results as a dictionary.
This function is used by tests and the JSON output functionality.
Args:
file (str): Path to the file to analyze
selected_stats (list): List of stats to include in analysis
Returns:
dict: Analysis results in dictionary format
"""
# If no stats specified, use all available stats
if selected_stats is None:
selected_stats = [
"line_count",
"function_count",
"comment_line_count",
"inline_comment_count",
"external_dependencies_count",
"indentation_level"
]
# Get analysis results from analyze_file
return analyze_file(file, selected_stats=selected_stats)
def analyze_command(file, all, json_output, LANG_FILE):
"""
Analyze the given file.
"""
# load translations
messages = get_translation(LANG_FILE)
# define available stats
available_stats = [
"line_count",
"function_count",
"comment_line_count",
"inline_comment_count",
"external_dependencies_count",
"indentation_level"
]
# dictionary for the stats
stats_labels = {
"line_count": messages.get("line_count_option", "Line Count"),
"function_count": messages.get("function_count_option", "Function Count"),
"comment_line_count": messages.get("comment_line_count_option", "Comment Line Count"),
"inline_comment_count": messages.get("inline_comment_count_option", "Inline Comment Count"),
"external_dependencies_count": messages.get("external_dependencies_count_option", "External Dependencies Count"),
"indentation_level": messages.get("indentation_level_option", "Indentation Analysis")
}
# If --all flag is used, skip the selection menu and use all stats
if all:
selected_stat_keys = available_stats
else:
# Don't show interactive menu in JSON mode (assumes all stats)
if json_output:
selected_stat_keys = available_stats
else:
# print checkbox menu to select which stats to show
selected_stats = inquirer.checkbox(
message=messages.get("select_stats", "Select stats to display:"),
choices=[stats_labels[stat] for stat in available_stats],
pointer="> ",
default=[stats_labels[stat] for stat in available_stats], # All selected by default
instruction=messages.get("checkbox_hint", "(Use space to select, enter to confirm)")
).execute()
# if no stats were selected
if not selected_stats:
if json_output:
import json
print(json.dumps({"error": messages.get("no_stats_selected", "No stats selected. Analysis cancelled.")}))
else:
print(messages.get("no_stats_selected", "No stats selected. Analysis cancelled."))
return
# create a mapping from displayed labels back to stat keys
reverse_mapping = {v: k for k, v in stats_labels.items()}
# convert selected labels back to stat keys
selected_stat_keys = [reverse_mapping[label] for label in selected_stats]
# try to analyze and if error then print the error
try:
# show analyzing message if not in JSON mode
if not json_output:
print(f"{messages.get('analyzing_file', 'Analyzing file')}: {file}")
# get analysis results using analyze_to_dict function
results = analyze_to_dict(file, selected_stats=selected_stat_keys)
# output in JSON format if flag
if json_output:
import json
print(json.dumps(results, indent=2))
else:
# only print the selected stats in normal mode
for stat in selected_stat_keys:
if stat == "indentation_level" and "indentation_type" in results:
print(f"{messages.get('indentation_type', 'Indentation Type')}: {results['indentation_type']}")
print(f"{messages.get('indentation_size', 'Indentation Size')}: {results['indentation_size']}")
elif stat in results:
print(messages.get(stat, f"{stat.replace('_', ' ').title()}: {{count}}").format(count=results[stat]))
except Exception as e:
if json_output:
import json
# Replace newlines with spaces or escape them properly
error_msg = str(e).replace('\n', ' ')
print(json.dumps({"error": error_msg}))
else:
print(f"{messages.get('error', 'Error')}: {e}")