Coverage for sparc/sparc_parsers/utils.py: 98%

53 statements  

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

1from warnings import warn 

2 

3 

4def get_label(fileobj, ext): 

5 """Return the label of file by stripping the extension (e.g. .ion)""" 

6 return fileobj.name.rsplit(ext, 1)[0] 

7 

8 

9def strip_comments(rawtext, symbol="#"): 

10 """Strip comments from the text, including trailing comments""" 

11 stripped = [] 

12 comments = [] 

13 for line in rawtext.splitlines(): 

14 data, comment = bisect_and_strip(line, symbol) 

15 if data: 

16 stripped.append(data) 

17 if comment: 

18 comments.append(comment) 

19 return stripped, comments 

20 

21 

22def bisect_and_strip(text, delimiter): 

23 """split string in 2 at first occurence of a character and remove whitespace 

24 useful for separating comments from data, keys from values, etc. 

25 """ 

26 # wrap around to len(text) if not found (-1) 

27 index = text.find(delimiter) % (len(text) + 1) 

28 return text[:index].strip(), text[index + len(delimiter) :].strip() 

29 

30 

31def read_block_input(block, validator=None): 

32 """Read blocks of inputs from ion or inpt file and convert with validator 

33 

34 the following inputs are accepted: 

35 1) single line input: KEY: VALUE 

36 2) multiline input: KEY: VALUE1 \n VALUE2 --> (concanate the values) 

37 3) multiline input w/ blank first line: KEY: \n VALUE1 \n VALUE2 --> (append the values) 

38 """ 

39 block_dict = {} 

40 multiline_key = "" 

41 concat = False 

42 use_validator = True if validator else False 

43 for line in block: 

44 if ":" not in line: 

45 # import pdb; pdb.set_trace() 

46 # no key, assume multiline value. 

47 # be careful not to add blank lines 

48 if multiline_key: 

49 if concat: 

50 block_dict[multiline_key] = ( 

51 block_dict[multiline_key] + f" {line.strip()}" 

52 ) 

53 else: 

54 block_dict[multiline_key].append(line.strip()) 

55 continue 

56 key, value = bisect_and_strip(line, ":") 

57 key = key.upper() 

58 

59 if key and value: 

60 block_dict[key] = value 

61 multiline_key = key 

62 concat = True 

63 elif key: 

64 # no value, assume that this key has a list of values 

65 # in the following lines 

66 block_dict[key] = [] 

67 multiline_key = key 

68 concat = False 

69 for key, val in block_dict.items(): 

70 _use_validator_this_key = use_validator 

71 if _use_validator_this_key: 

72 if key not in validator.parameters.keys(): 

73 warn( 

74 f"Key {key} not in validator's parameter list, ignore value conversion!" 

75 ) 

76 _use_validator_this_key = False 

77 if _use_validator_this_key: 

78 val = validator.convert_string_to_value(key, val) 

79 block_dict[key] = val 

80 return block_dict 

81 

82 

83def make_reverse_mapping(mapping): 

84 """Given a list of mapping, get its reverse mapping 

85 

86 For example: 

87 mapping = [0, 2, 3, 1, 5, 4] 

88 reverse = [0, 3, 1, 2, 5, 4] 

89 """ 

90 reverse = [0] * len(mapping) 

91 for i, j in enumerate(mapping): 

92 reverse[j] = i 

93 return reverse