1
# Copyright (C) 2004,2005 PreludeIDS Technologies. All Rights Reserved.
2
# Author: Nicolas Delon <nicolas.delon@prelude-ids.com>
4
# This file is part of the Prewikka program.
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2, or (at your option)
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; see the file COPYING. If not, write to
18
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
class Error(Exception):
28
class ParseError(Error):
29
def __init__(self, filename, lineno, line):
30
self.filename = filename
35
return "parse error in \"%s\" at %s line %d" % (self.line.rstrip(), self.filename, self.lineno)
39
class OrderedDict(dict):
42
self.ordered_key_list = [ ]
44
def __delitem__(self, key):
45
dict.__delitem__(self, key)
46
self.ordered_key_list.remove(key)
48
def __setitem__(self, key, value):
49
dict.__setitem__(self, key, value)
50
if not key in self.ordered_key_list:
51
self.ordered_key_list.append(key)
54
return map(lambda k: self[k], self.ordered_key_list)
57
return self.ordered_key_list
60
return map(lambda key: (key, self[key]), self.ordered_key_list)
64
for key in self.keys():
70
class ConfigParserSection(OrderedDict):
71
def __init__(self, name):
72
OrderedDict.__init__(self)
75
def __nonzero__(self):
78
def getOption(self, name):
81
def getOptionValue(self, key, value=None):
83
return self[key].value
92
class ConfigParserOption:
93
def __init__(self, name, value, lineno, line):
101
class MyConfigParser:
103
A config parser class ala ConfigParser.ConfigParser (only read operations
104
are (will be) supported).
105
ConfigParser.ConfigParser did not feed all our needs:
106
- we need the '= value' part of option to be optionnal
107
- we need to support special characters (like ':') in option name (for urls)
108
- we need to keep the right order of options in sections (this is done via
109
the OrderedDict class that subclass dict)
112
EMPTY_LINE_REGEXP = re.compile("^\s*(\#.*)?$")
113
SECTION_REGEXP = re.compile("\[(?P<name>.+)]")
114
OPTION_REGEXP = re.compile("^\s*(?P<name>[\s]*[^:]+)(\:\s*(?P<value>.+))?$")
116
def __init__(self, filename):
117
self.filename = filename
118
self._sections = OrderedDict()
119
self._root_section = OrderedDict()
120
self._current_section = self._root_section
125
for line in open(self.filename).readlines():
127
result = self.EMPTY_LINE_REGEXP.match(line)
131
result = self.SECTION_REGEXP.match(line)
133
name = result.group("name")
135
self._current_section = self._sections[name] = ConfigParserSection(name)
137
result = self.OPTION_REGEXP.match(line)
139
name, value = result.group("name", "value")
142
value = value.strip()
143
self._current_section[name] = ConfigParserOption(name, value, lineno, line)
145
raise ParseError(file.name, lineno, line)
147
def getSection(self, name):
148
return self._sections[name]
150
def getSections(self):
151
return self._sections.values()
155
for section in self.getSections():
156
content += "[%s]\n" % section.name
157
for option in section.getOptions():
158
content += "%s: %s\n" % (option.name, option.value)