Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

"""Parsers for the specific XIA INI file format.""" 

 

from collections import OrderedDict 

 

 

def parse_xia_ini_file(content): 

"""Parse the content of a XIA INI file. 

 

The return result is an OrderedDict of <section name: list>, 

where the list items are OrderedDict of <key: value>. 

""" 

dct = OrderedDict() 

section, item = None, None 

# Loop over striped lines 

for line in content.splitlines(): 

line = line.strip() 

# Comment 

if line.startswith('*'): 

pass 

# New section 

elif line.startswith('[') and line.endswith(']'): 

if item is not None: 

msg = "New section within section {} item {}" 

raise ValueError(msg.format(section, item)) 

item = None 

section = line[1:-1].strip() 

dct[section] = [] 

# New item 

elif line.startswith('START #'): 

if item is not None: 

msg = "New item within section {} item {}" 

raise ValueError(msg.format(section, item)) 

item = int(line.split('#')[1]) 

if section is None: 

msg = 'Item {} outside of section' 

raise ValueError(msg.format(item)) 

if item != len(dct[section]): 

msg = 'Corrupted start (section {}, {} should be {})' 

msg = msg.format(section, item, len(dct[section])) 

raise ValueError(msg) 

dct[section].append(OrderedDict()) 

# End item 

elif line.startswith('END #'): 

if item is None: 

msg = 'End markup outside of item' 

raise ValueError(msg) 

item = int(line.split('#')[1]) 

if item != len(dct[section]) - 1: 

msg = 'Corrupted end (section {}, {} should be {})' 

msg = msg.format(section, item, len(dct[section]) - 1) 

raise ValueError(msg) 

item = None 

# New pair 

elif '=' in line: 

key, value = map(str.strip, line.split('=')) 

if item is None: 

msg = 'Key/value pair {} outside of item' 

raise ValueError(msg.format((key, value))) 

dct[section][item][key] = value 

# Error 

elif line: 

raise ValueError('Line not recognized: {!r}'.format(line)) 

# Return result 

return dct