# Copyright 2002-2008 Gentoo Foundation; Distributed under the GPL v2
# $Id: $
+ 07 Sep 2008; Andrew Gaffney <agaffney@gentoo.org> catalyst,
+ modules/catalyst/config.py:
+ A few fixes to ParserBase after actually testing it switch to parsing
+ config file with ConfigParser
+
07 Sep 2008; Andrew Gaffney <agaffney@gentoo.org> catalyst,
+modules/catalyst/config.py, -modules/catalyst/util.py:
More indecisiveness..move util.spec to config.SpecParser
sys.path.append("./modules")
-import catalyst
+import catalyst.config
__maintainer__="Chris Gianelloni <wolf31o2@gentoo.org>"
__version__="2.0.6"
# now, try and parse the config file "config_file"
try:
- execfile(config_file, myconf, myconf)
+# execfile(config_file, myconf, myconf)
+ myconfig = catalyst.config.ConfigParser(config_file)
+ myconf.update(myconfig.get_values())
except:
print "!!! catalyst: Unable to parse configuration file, "+myconfig
+import re
+
class ParserBase:
filename = ""
def get_values(self):
return self.values
+ def dump(self):
+ dump = ""
+ for x in self.values.keys():
+ dump += x + " = " + repr(self.values[x]) + "\n"
+ return dump
+
def parse_file(self, filename):
try:
myf = open(filename, "r")
trailing_comment=re.compile('\s*#.*$')
white_space=re.compile('\s+')
- for x, myline in enum(self.lines):
+ for x, myline in enumerate(self.lines):
myline = myline.strip()
# Force the line to be clean
subarray = mobjs[1].split()
cur_array += subarray
else:
- cur_array += mobjs[1]
+ cur_array += [mobjs[1]]
# Else add on to the last key we were working on
else:
# cur_array += mobjs
cur_array += myline.split()
else:
- throw Exception("Value without a key found on line " + x)
+ raise Exception("Value without a key found on line " + x)
# XXX: Do we really still need this "single value is a string" behavior?
if len(cur_array) == 2:
self.values = values
-class SpecParser:
+class SpecParser(ParserBase):
key_value_separator = ':'
multiple_values = True
if filename:
self.parse_file(filename)
- def dump(self):
- dump = ""
- for x in self.values.keys():
- dump += x + ": " + repr(self.values[x]) + "\n"
+class ConfigParser(ParserBase):
+
+ key_value_separator = '='
+ multiple_values = False
+ empty_values = True
+
+ def __init__(self, filename=""):
+ if filename:
+ self.parse_file(filename)
+