Coverage for skema/program_analysis/snippet_ingester.py: 39%

18 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1import argparse 

2import tempfile 

3import os 

4 

5from skema.program_analysis.single_file_ingester import process_file 

6from skema.gromet.fn import GrometFNModuleCollection 

7 

8 

9def process_snippet( 

10 source: str, extension: str, write_to_file=False 

11) -> GrometFNModuleCollection: 

12 """Run a Python or Fortran code snippet through the CODE2FN pipeline and return the GrometFNModuleCollection. 

13 Optionally, output the Gromet JSON to a file. 

14 """ 

15 

16 # Create temporary snippet file 

17 tmp = tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=extension) 

18 tmp.write(source) 

19 tmp.close() 

20 

21 gromet_collection = process_file(tmp.name, write_to_file) 

22 

23 # Delete temporary snippet file 

24 os.unlink(tmp.name) 

25 

26 return gromet_collection 

27 

28 

29if __name__ == "__main__": 

30 parser = argparse.ArgumentParser() 

31 parser.add_argument("source", type=str, help="Python or Fortran source code") 

32 parser.add_argument( 

33 "extension", 

34 type=str, 

35 help="The language file extension of the code snippet (i.e. .py, .f95)", 

36 ) 

37 args = parser.parse_args() 

38 process_snippet(args.source, args.extension, True)