configFile.py
1.89 KB
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
'''
Created on 18 lut 2014
@author: mlenart
'''
import re
import codecs
import exceptions
def getHeaderValue(line, lineNum):
m = re.match(ur'\s*\[(.*?)\]\s*(\#.*)?', line)
if m:
return m.group(1)
else:
return None
class ConfigFile(object):
def __init__(self, filename, sectionNames):
self.filename = filename
self.sectionNames = sectionNames
self.section2Lines = {}
self.currSection = None
self._parse()
def _addSectionStart(self, sectionName, lineNum):
if not sectionName in self.sectionNames:
raise exceptions.ConfigFileException(self.filename, lineNum, 'Invalid section: %s' % sectionName)
if sectionName in self.section2Lines:
raise exceptions.ConfigFileException(self.filename, lineNum, 'Duplicate section: %s' % sectionName)
self.section2Lines[sectionName] = []
self.currSection = sectionName
def _addLine(self, line, lineNum):
line = line.strip()
if line:
if self.currSection is None and not line.startswith('#'):
raise exceptions.ConfigFileException(self.filename, lineNum, 'Text outside of any section')
self.section2Lines[self.currSection].append((lineNum, line))
def _getHeaderValue(self, line, lineNum):
m = re.match(ur'\s*\[(.*?)\]\s*(\#.*)?', line)
if m:
return m.group(1)
else:
return None
def enumerateLinesInSection(self, sectionName):
return self.section2Lines[sectionName]
def _parse(self):
with codecs.open(self.filename, 'r', 'utf8') as f:
for lineNum, line in enumerate(f, start=1):
header = self._getHeaderValue(line, lineNum)
if header:
self._addSectionStart(header, lineNum)
else:
self._addLine(line, lineNum)