-
Notifications
You must be signed in to change notification settings - Fork 0
/
ginie.py
executable file
·135 lines (112 loc) · 3.84 KB
/
ginie.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"gINIe Parser for Wish"
import re
import sys
class GinieParseError(ValueError):
"Exception Class for errors in"
def loads(string):
"""
Deserialize `string` (a `str` instance containing a gINIe document)
into a Python list.
Return Value: List of `block` and `section` dicts
`block` dicts are of form
{
'type': 'block',
'name': 'name_of_block_as_in_config',
'config': {}
}
where `name` and `config` keys are based on the gINIe document.
`section` dicts are of form
{
'type': 'block',
'name': 'name_of_block_as_in_config',
'blocks': []
}
where `name` and `blocks` keys are based on the gINIe document and blocks
is a list of block dicts found in the section.
"""
config = []
section_re = re.compile(r"^\|(?P<section>.+)\|$")
block_re = re.compile(r"^\[(?P<block>[^\]]*)\]$")
line_re = re.compile(r"^\s*(?P<key>[^\s=]+)(\s*)=\s*?(?P<value>.*)$")
empty_re = re.compile(r"^\s*$")
comment_re = re.compile(r"^\s*(#|;)")
current_block = None
current_section = None
for idx, line in enumerate(string.splitlines()):
idx += 1 # Since line numbers begin with 1
# Skip comments and empty lines
if empty_re.match(line) or comment_re.match(line):
continue
# Section parsing
if line.startswith("|"):
match = section_re.match(line)
if match is None:
err = "Invalid line {}".format(idx)
raise GinieParseError(err)
current_section = {
"type": "section",
"name": match.group("section"),
"blocks": []
}
config.append(current_section)
continue
# Block Parsing
if line.startswith("["):
match = block_re.match(line)
if match is None:
err = "Invalid block name on line {}".format(idx)
raise GinieParseError(err)
current_block = {
"type": "block",
"name": match.group("block"),
"config": {}
}
if current_section is None:
config.append(current_block)
else:
current_section["blocks"].append(current_block)
continue
# If it is neither a comment, nor a section, nor a block, it has to be
# a key, value pair.
if current_block is None:
raise GinieParseError("Found lines outside a block")
match = line_re.match(line)
if match is None:
raise GinieParseError("Invalid line {}: {}".format(idx, line))
value = match.group('value').strip()
if value:
if value[0] == value[-1] == "'" or value[0] == value[-1] == '"':
value = value[1:-1]
current_block["config"][match.group('key').strip()] = value
return config
def load(file):
"""
Deserialize `file` (a file-like object containing a gINIe document)
into a Python list.
Return Value: List of `block` and `section` dicts
`block` dicts are of form
{
'type': 'block',
'name': 'name_of_block_as_in_config',
'config': {}
}
where `name` and `config` keys are based on the gINIe document.
`section` dicts are of form
{
'type': 'block',
'name': 'name_of_block_as_in_config',
'blocks': []
}
where `name` and `blocks` keys are based on the gINIe document and blocks
is a list of block dicts found in the section.
"""
return loads(file.read())
if __name__ == "__main__":
if len(sys.argv) == 1:
print("Usage: {} file.gie|-".format(sys.argv[0]))
elif sys.argv[1] == '-':
print(load(sys.stdin))
else:
with open(sys.argv[1]) as source:
print(load(source))