|
9 | 9 |
|
10 | 10 | from ..application.ports.caching import Cache as AbstractCache |
11 | 11 | from ..application.ports.caching import CacheMiss |
12 | | - |
| 12 | +from grimp import _rustgrimp as rust |
13 | 13 | logger = logging.getLogger(__name__) |
14 | 14 | PrimitiveFormat = Dict[str, List[Tuple[str, Optional[int], str]]] |
15 | 15 |
|
@@ -199,74 +199,20 @@ def _read_data_map_file(self) -> Dict[Module, Set[DirectImport]]: |
199 | 199 | ), |
200 | 200 | ) |
201 | 201 | try: |
202 | | - serialized = self.file_system.read(data_cache_filename) |
| 202 | + imports_by_module = rust.read_cache_data_map_file(data_cache_filename, self.file_system.convert_to_basic()) |
203 | 203 | except FileNotFoundError: |
204 | 204 | logger.info(f"No cache file: {data_cache_filename}.") |
205 | 205 | return {} |
206 | | - |
207 | | - # Deserialize to primitives. |
208 | | - try: |
209 | | - deserialized_json = json.loads(serialized) |
210 | | - logger.info(f"Used cache data file {data_cache_filename}.") |
211 | | - except json.JSONDecodeError: |
| 206 | + except rust.CorruptCache: |
212 | 207 | logger.warning(f"Could not use corrupt cache file {data_cache_filename}.") |
213 | 208 | return {} |
214 | 209 |
|
215 | | - primitives_map: PrimitiveFormat = self._to_primitives_data_map(deserialized_json) |
216 | | - |
217 | | - return { |
218 | | - Module(name=name): { |
219 | | - DirectImport( |
220 | | - importer=Module(name), |
221 | | - imported=Module(import_data[0]), |
222 | | - line_number=int(import_data[1]), # type: ignore |
223 | | - line_contents=import_data[2], |
224 | | - ) |
225 | | - for import_data in imports_data |
226 | | - } |
227 | | - for name, imports_data in primitives_map.items() |
228 | | - } |
| 210 | + logger.info(f"Used cache data file {data_cache_filename}.") |
| 211 | + return imports_by_module |
229 | 212 |
|
230 | 213 | def _build_data_cache_filename(self, found_package: FoundPackage) -> str: |
231 | 214 | return self.file_system.join(self.cache_dir, f"{found_package.name}.data.json") |
232 | 215 |
|
233 | | - def _to_primitives_data_map(self, deserialized_json: object) -> PrimitiveFormat: |
234 | | - """ |
235 | | - Convert the deserialized json from a data file to a narrower schema. |
236 | | -
|
237 | | - Anything that doesn't fit the schema will be removed. |
238 | | - """ |
239 | | - if not isinstance(deserialized_json, dict): |
240 | | - return {} |
241 | | - |
242 | | - primitives_map: PrimitiveFormat = {} |
243 | | - |
244 | | - for key, value in deserialized_json.items(): |
245 | | - if not isinstance(key, str): |
246 | | - continue |
247 | | - if not isinstance(value, list): |
248 | | - continue |
249 | | - primitive_imports = [] |
250 | | - for deserialized_import in value: |
251 | | - try: |
252 | | - [imported, line_number, line_contents] = deserialized_import |
253 | | - except ValueError: |
254 | | - continue |
255 | | - try: |
256 | | - primitive_imports.append( |
257 | | - ( |
258 | | - str(imported), |
259 | | - int(line_number) if line_number else None, |
260 | | - str(line_contents), |
261 | | - ) |
262 | | - ) |
263 | | - except TypeError: |
264 | | - continue |
265 | | - |
266 | | - primitives_map[key] = primitive_imports |
267 | | - |
268 | | - return primitives_map |
269 | | - |
270 | 216 | def _write_marker_files_if_not_already_there(self) -> None: |
271 | 217 | marker_files_info = ( |
272 | 218 | (".gitignore", "# Automatically created by Grimp.\n*"), |
|
0 commit comments