A few fixes to ParserBase after actually testing it
authoragaffney <agaffney@kagome.(none)>
Sun, 7 Sep 2008 04:21:16 +0000 (23:21 -0500)
committeragaffney <agaffney@kagome.(none)>
Sun, 7 Sep 2008 04:21:16 +0000 (23:21 -0500)
switch to parsing config file with ConfigParser

ChangeLog
catalyst
modules/catalyst/config.py

index 88bd3c3d41b974aa9d0d7438674005c8873f3e4f..2e7caca4b8b5be0f3354d7d23b42dc9c4342d0c1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,11 @@
 # 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
index 518a82e281a82a4f6d4c2f0d65e1bb1ea430adb5..9b3cf15691bcd55980bcb12099424131bbdab7fc 100755 (executable)
--- a/catalyst
+++ b/catalyst
@@ -9,7 +9,7 @@ import pdb
 
 sys.path.append("./modules")
 
-import catalyst
+import catalyst.config
 
 __maintainer__="Chris Gianelloni <wolf31o2@gentoo.org>"
 __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
index 2b02f18017d9ada0ca92baae270c114ac63e2173..1f221b89333cdea44f93a676b8d1515fb7dad40d 100644 (file)
@@ -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)
+