44from spice .analyze import analyze_file
55
66
7+ def analyze_to_dict (file , selected_stats = None ):
8+ """
9+ Analyze the given file and return results as a dictionary.
10+ This function is used by tests and the JSON output functionality.
11+
12+ Args:
13+ file (str): Path to the file to analyze
14+ selected_stats (list): List of stats to include in analysis
15+
16+ Returns:
17+ dict: Analysis results in dictionary format
18+ """
19+ # If no stats specified, use all available stats
20+ if selected_stats is None :
21+ selected_stats = [
22+ "line_count" ,
23+ "function_count" ,
24+ "comment_line_count" ,
25+ "inline_comment_count" ,
26+ "external_dependencies_count" ,
27+ "indentation_level"
28+ ]
29+
30+ # Get analysis results from analyze_file
31+ return analyze_file (file , selected_stats = selected_stats )
32+
33+
734def analyze_command (file , all , json_output , LANG_FILE ):
835 """
936 Analyze the given file.
@@ -70,8 +97,8 @@ def analyze_command(file, all, json_output, LANG_FILE):
7097 if not json_output :
7198 print (f"{ messages .get ('analyzing_file' , 'Analyzing file' )} : { file } " )
7299
73- # get analysis results from analyze_file
74- results = analyze_file (file , selected_stats = selected_stat_keys )
100+ # get analysis results using analyze_to_dict function
101+ results = analyze_to_dict (file , selected_stats = selected_stat_keys )
75102
76103 # output in JSON format if flag
77104 if json_output :
@@ -93,4 +120,4 @@ def analyze_command(file, all, json_output, LANG_FILE):
93120 error_msg = str (e ).replace ('\n ' , ' ' )
94121 print (json .dumps ({"error" : error_msg }))
95122 else :
96- print (f"{ messages .get ('error' , 'Error' )} : { e } " )
123+ print (f"{ messages .get ('error' , 'Error' )} : { e } " )
0 commit comments