@@ -24,7 +24,7 @@ class FileService(BaseService):
24
24
- Language-specific file analysis
25
25
"""
26
26
27
-
27
+
28
28
def get_file_content (self , file_path : str ) -> str :
29
29
"""
30
30
Get the content of a specific file.
@@ -44,11 +44,11 @@ def get_file_content(self, file_path: str) -> str:
44
44
"""
45
45
self ._require_project_setup ()
46
46
self ._require_valid_file_path (file_path )
47
-
47
+
48
48
# Normalize the file path
49
49
norm_path = os .path .normpath (file_path )
50
50
full_path = os .path .join (self .base_path , norm_path )
51
-
51
+
52
52
try :
53
53
with open (full_path , 'r' , encoding = 'utf-8' ) as f :
54
54
content = f .read ()
@@ -62,7 +62,7 @@ def get_file_content(self, file_path: str) -> str:
62
62
except (FileNotFoundError , PermissionError , OSError ) as e :
63
63
raise FileNotFoundError (f"Error reading file: { e } " ) from e
64
64
65
-
65
+
66
66
def analyze_file (self , file_path : str ) -> Dict [str , Any ]:
67
67
"""
68
68
Analyze a file and return summary information from index data.
@@ -83,21 +83,21 @@ def analyze_file(self, file_path: str) -> Dict[str, Any]:
83
83
84
84
# Normalize the file path to use forward slashes (consistent with index storage)
85
85
norm_path = normalize_file_path (file_path )
86
-
86
+
87
87
# Get file extension
88
88
_ , ext = os .path .splitext (norm_path )
89
-
89
+
90
90
# Only use index data - no fallback to real-time analysis
91
91
if not self .index_cache or 'files' not in self .index_cache :
92
92
raise ValueError (f"No index data available for file: { norm_path } " )
93
-
93
+
94
94
# Find file in index
95
95
for file_entry in self .index_cache ['files' ]:
96
96
if file_entry .get ('path' ) == norm_path :
97
97
# Validate index data structure
98
98
if not self ._validate_index_entry (file_entry ):
99
99
raise ValueError (f"Malformed index data for file: { norm_path } " )
100
-
100
+
101
101
# Extract complete relationship data from index
102
102
functions = file_entry .get ('functions' , [])
103
103
classes = file_entry .get ('classes' , [])
@@ -116,48 +116,48 @@ def analyze_file(self, file_path: str) -> Dict[str, Any]:
116
116
language_specific = file_entry .get ('language_specific' , {}),
117
117
index_cache = self .index_cache # Pass index cache for qualified name resolution
118
118
)
119
-
119
+
120
120
# File not found in index
121
121
raise ValueError (f"File not found in index: { norm_path } " )
122
122
123
-
124
123
125
124
126
-
125
+
126
+
127
127
def _validate_index_entry (self , file_entry : Dict [str , Any ]) -> bool :
128
128
"""
129
129
Validate the structure of an index entry to ensure it's not malformed.
130
-
130
+
131
131
Args:
132
132
file_entry: Index entry to validate
133
-
133
+
134
134
Returns:
135
135
True if the entry is valid, False if malformed
136
136
"""
137
137
try :
138
138
# Check required fields
139
139
if not isinstance (file_entry , dict ):
140
140
return False
141
-
141
+
142
142
# Validate basic file information
143
143
if 'path' not in file_entry or not isinstance (file_entry ['path' ], str ):
144
144
return False
145
-
145
+
146
146
# Validate optional numeric fields
147
147
for field in ['line_count' , 'size' ]:
148
148
if field in file_entry and not isinstance (file_entry [field ], (int , float )):
149
149
return False
150
-
150
+
151
151
# Validate optional string fields
152
152
for field in ['language' ]:
153
153
if field in file_entry and not isinstance (file_entry [field ], str ):
154
154
return False
155
-
155
+
156
156
# Validate functions list structure
157
157
functions = file_entry .get ('functions' , [])
158
158
if not isinstance (functions , list ):
159
159
return False
160
-
160
+
161
161
for func in functions :
162
162
if isinstance (func , dict ):
163
163
# Validate function object structure
@@ -173,12 +173,12 @@ def _validate_index_entry(self, file_entry: Dict[str, Any]) -> bool:
173
173
elif not isinstance (func , str ):
174
174
# Functions can be strings (legacy) or dicts (enhanced)
175
175
return False
176
-
176
+
177
177
# Validate classes list structure
178
178
classes = file_entry .get ('classes' , [])
179
179
if not isinstance (classes , list ):
180
180
return False
181
-
181
+
182
182
for cls in classes :
183
183
if isinstance (cls , dict ):
184
184
# Validate class object structure
@@ -194,12 +194,12 @@ def _validate_index_entry(self, file_entry: Dict[str, Any]) -> bool:
194
194
elif not isinstance (cls , str ):
195
195
# Classes can be strings (legacy) or dicts (enhanced)
196
196
return False
197
-
197
+
198
198
# Validate imports list structure
199
199
imports = file_entry .get ('imports' , [])
200
200
if not isinstance (imports , list ):
201
201
return False
202
-
202
+
203
203
for imp in imports :
204
204
if isinstance (imp , dict ):
205
205
# Validate import object structure
@@ -214,13 +214,13 @@ def _validate_index_entry(self, file_entry: Dict[str, Any]) -> bool:
214
214
elif not isinstance (imp , str ):
215
215
# Imports can be strings (legacy) or dicts (enhanced)
216
216
return False
217
-
217
+
218
218
# Validate language_specific field
219
219
if 'language_specific' in file_entry and not isinstance (file_entry ['language_specific' ], dict ):
220
220
return False
221
-
221
+
222
222
return True
223
-
223
+
224
224
except (KeyError , TypeError , AttributeError ):
225
225
return False
226
226
@@ -238,4 +238,4 @@ def validate_file_path(self, file_path: str) -> bool:
238
238
return False
239
239
240
240
error = self ._validate_file_path (file_path )
241
- return error is None
241
+ return error is None
0 commit comments