-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniParser.py
More file actions
35 lines (25 loc) · 710 Bytes
/
IniParser.py
File metadata and controls
35 lines (25 loc) · 710 Bytes
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
class IniParser:
def __init__(self, file):
self.file = file
self.IniDict = {}
self.__FillIniDict(file)
def GetValue(self, key):
try:
return self.IniDict[key]
except KeyError:
raise Exception('"%s" does not exists' % key)
def __FillIniDict(self, file):
keyValRegEx = re.compile( r"^\s*(.+?)\s*=\s*(.+?)\s*$" )
for line in self.__ReadFileAsListLine(file):
result = keyValRegEx.search(line)
if result:
self.IniDict[result.group(1)] = result.group(2)
def __ReadFileAsListLine(self, file):
try:
with open(file, 'r') as file:
return file.readlines()
except Exception as e:
raise e