1616from pathlib import Path
1717
1818from core .schemas import ParseResult
19+ from utilities .file_io import read_json , write_json
1920
2021# Root of openant-core (where parsers/ lives)
2122_CORE_ROOT = Path (__file__ ).parent .parent
@@ -161,9 +162,7 @@ def _maybe_apply_diff_filter(
161162 )
162163 return
163164
164- with open (result .dataset_path , "r" ) as f :
165- dataset = json .load (f )
166-
165+ dataset = read_json (result .dataset_path )
167166 # Dataset may be a dict with "units" or a raw list.
168167 if isinstance (dataset , dict ):
169168 units = dataset .get ("units" , [])
@@ -172,14 +171,11 @@ def _maybe_apply_diff_filter(
172171
173172 stats = apply_diff_filter (units , manifest )
174173
175- with open (result .dataset_path , "w" ) as f :
176- json .dump (dataset , f , indent = 2 )
177-
174+ write_json (result .dataset_path , dataset )
178175 # Expose stats on the ParseResult via a side-channel file; the parse
179176 # step_context reads this when assembling parse.report.json.
180177 diff_report_path = os .path .join (output_dir , "diff_filter.report.json" )
181- with open (diff_report_path , "w" ) as f :
182- json .dump (stats .to_dict (), f , indent = 2 )
178+ write_json (diff_report_path , stats .to_dict ())
183179
184180 print (
185181 f" Diff filter ({ stats .scope } ): { stats .selected } /{ stats .total } units selected"
@@ -245,9 +241,7 @@ def _load_module(name, filename):
245241
246242 print (f"\n [Reachability Filter] Filtering to { processing_level } units..." , file = sys .stderr )
247243
248- with open (call_graph_path , "r" ) as f :
249- call_graph_data = json .load (f )
250-
244+ call_graph_data = read_json (call_graph_path )
251245 functions = call_graph_data .get ("functions" , {})
252246 call_graph = call_graph_data .get ("call_graph" , {})
253247 reverse_call_graph = call_graph_data .get ("reverse_call_graph" , {})
@@ -352,12 +346,8 @@ def _parse_python(repo_path: str, output_dir: str, processing_level: str, skip_t
352346 dataset = _apply_reachability_filter (dataset , output_dir , processing_level )
353347
354348 # Write outputs
355- with open (dataset_path , "w" ) as f :
356- json .dump (dataset , f , indent = 2 )
357-
358- with open (analyzer_output_path , "w" ) as f :
359- json .dump (analyzer_output , f , indent = 2 )
360-
349+ write_json (dataset_path , dataset )
350+ write_json (analyzer_output_path , analyzer_output )
361351 units_count = len (dataset .get ("units" , []))
362352 print (f" Python parser complete: { units_count } units" , file = sys .stderr )
363353
@@ -413,8 +403,7 @@ def _parse_javascript(repo_path: str, output_dir: str, processing_level: str, sk
413403 # Count units
414404 units_count = 0
415405 if os .path .exists (dataset_path ):
416- with open (dataset_path ) as f :
417- data = json .load (f )
406+ data = read_json (dataset_path )
418407 units_count = len (data .get ("units" , []))
419408
420409 print (f" JavaScript parser complete: { units_count } units" , file = sys .stderr )
@@ -470,8 +459,7 @@ def _parse_go(repo_path: str, output_dir: str, processing_level: str, skip_tests
470459 # Count units
471460 units_count = 0
472461 if os .path .exists (dataset_path ):
473- with open (dataset_path ) as f :
474- data = json .load (f )
462+ data = read_json (dataset_path )
475463 units_count = len (data .get ("units" , []))
476464
477465 print (f" Go parser complete: { units_count } units" , file = sys .stderr )
@@ -530,8 +518,7 @@ def _parse_c(repo_path: str, output_dir: str, processing_level: str, skip_tests:
530518 # Count units
531519 units_count = 0
532520 if os .path .exists (dataset_path ):
533- with open (dataset_path ) as f :
534- data = json .load (f )
521+ data = read_json (dataset_path )
535522 units_count = len (data .get ("units" , []))
536523
537524 print (f" C/C++ parser complete: { units_count } units" , file = sys .stderr )
@@ -590,8 +577,7 @@ def _parse_ruby(repo_path: str, output_dir: str, processing_level: str, skip_tes
590577 # Count units
591578 units_count = 0
592579 if os .path .exists (dataset_path ):
593- with open (dataset_path ) as f :
594- data = json .load (f )
580+ data = read_json (dataset_path )
595581 units_count = len (data .get ("units" , []))
596582
597583 print (f" Ruby parser complete: { units_count } units" , file = sys .stderr )
@@ -650,8 +636,7 @@ def _parse_php(repo_path: str, output_dir: str, processing_level: str, skip_test
650636 # Count units
651637 units_count = 0
652638 if os .path .exists (dataset_path ):
653- with open (dataset_path ) as f :
654- data = json .load (f )
639+ data = read_json (dataset_path )
655640 units_count = len (data .get ("units" , []))
656641
657642 print (f" PHP parser complete: { units_count } units" , file = sys .stderr )
@@ -710,8 +695,7 @@ def _parse_zig(repo_path: str, output_dir: str, processing_level: str, skip_test
710695 # Count units
711696 units_count = 0
712697 if os .path .exists (dataset_path ):
713- with open (dataset_path ) as f :
714- data = json .load (f )
698+ data = read_json (dataset_path )
715699 units_count = len (data .get ("units" , []))
716700
717701 print (f" Zig parser complete: { units_count } units" , file = sys .stderr )
0 commit comments