From: agaffney Date: Sun, 7 Sep 2008 04:21:16 +0000 (-0500) Subject: A few fixes to ParserBase after actually testing it X-Git-Tag: CATALYST_2_0_6_916~72 X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=112c38ac822679347605cdd2487193e1af0b539e;p=catalyst.git A few fixes to ParserBase after actually testing it switch to parsing config file with ConfigParser --- diff --git a/ChangeLog b/ChangeLog index 88bd3c3d..2e7caca4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,11 @@ # Copyright 2002-2008 Gentoo Foundation; Distributed under the GPL v2 # $Id: $ + 07 Sep 2008; Andrew Gaffney 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 catalyst, +modules/catalyst/config.py, -modules/catalyst/util.py: More indecisiveness..move util.spec to config.SpecParser diff --git a/catalyst b/catalyst index 518a82e2..9b3cf156 100755 --- a/catalyst +++ b/catalyst @@ -9,7 +9,7 @@ import pdb sys.path.append("./modules") -import catalyst +import catalyst.config __maintainer__="Chris Gianelloni " __version__="2.0.6" @@ -81,7 +81,9 @@ def parse_config(myconfig): # 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 diff --git a/modules/catalyst/config.py b/modules/catalyst/config.py index 2b02f180..1f221b89 100644 --- a/modules/catalyst/config.py +++ b/modules/catalyst/config.py @@ -1,3 +1,5 @@ +import re + class ParserBase: filename = "" @@ -13,6 +15,12 @@ class ParserBase: 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") @@ -34,7 +42,7 @@ class ParserBase: 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 @@ -66,7 +74,7 @@ class ParserBase: 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: @@ -75,7 +83,7 @@ class ParserBase: # 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: @@ -92,7 +100,7 @@ class ParserBase: self.values = values -class SpecParser: +class SpecParser(ParserBase): key_value_separator = ':' multiple_values = True @@ -102,7 +110,13 @@ class SpecParser: 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) +