Coverage for yaptide/converter/converter/api.py: 59%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-07-01 12:55 +0000

1from pathlib import Path 

2from typing import Union 

3from converter.shieldhit.parser import ShieldhitParser 

4from converter.topas.parser import TopasParser 

5from converter.common import Parser 

6from converter.fluka.parser import FlukaParser 

7 

8 

9def get_parser_from_str(parser_type: str) -> Parser: 

10 """Get a converter object based on the provided type.""" 

11 # This is temporary, suggestions on how to do this better appreciated. 

12 if parser_type.lower() == 'shieldhit': 

13 return ShieldhitParser() 

14 if parser_type.lower() == 'topas': 

15 return TopasParser() 

16 if parser_type.lower() == 'fluka': 

17 return FlukaParser() 

18 

19 print(f"Invalid parser type \"{parser_type}\".") 

20 raise ValueError("Parser type must be either 'shieldhit', 'topas' or 'fluka'.") 

21 

22 

23def run_parser(parser: Parser, input_data: dict, output_dir: Union[Path, None] = None, silent: bool = True) -> dict: 

24 """ 

25 Convert the configs and return a dict representation of the config 

26 files. Can save them in the output_dir directory if specified. 

27 """ 

28 parser.parse_configs(input_data) 

29 

30 if not silent: 

31 for key, value in parser.get_configs_json().items(): 

32 print(f'File {key}:') 

33 print(value) 

34 

35 if output_dir: 

36 if not output_dir.exists(): 

37 output_dir.mkdir(parents=True) 

38 elif not output_dir.is_dir(): 

39 print(f'Output path {output_dir} is not a directory.') 

40 raise NotADirectoryError(output_dir) 

41 parser.save_configs(output_dir) 

42 

43 return parser.get_configs_json()