More indecisiveness..move util.spec to config.SpecParser
authoragaffney <agaffney@kagome.(none)>
Sun, 7 Sep 2008 01:23:07 +0000 (20:23 -0500)
committeragaffney <agaffney@kagome.(none)>
Sun, 7 Sep 2008 01:23:07 +0000 (20:23 -0500)
ChangeLog
catalyst
modules/catalyst/config.py [new file with mode: 0644]
modules/catalyst/util.py [deleted file]

index 271dc913506d11bf3e1f0a590b1255719e5bc08c..88bd3c3d41b974aa9d0d7438674005c8873f3e4f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,10 @@
 # Copyright 2002-2008 Gentoo Foundation; Distributed under the GPL v2
 # $Id: $
 
+  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
+
   06 Sep 2008; Andrew Gaffney <agaffney@gentoo.org> catalyst,
   -modules/catalyst/spec.py, +modules/catalyst/util.py,
   modules/catalyst_support.py:
index 9ea67459c6b7e62e6341b48064d398001f60a510..518a82e281a82a4f6d4c2f0d65e1bb1ea430adb5 100755 (executable)
--- a/catalyst
+++ b/catalyst
@@ -381,7 +381,7 @@ if __name__ == "__main__":
        addlargs={}
        
        if myspecfile:
-               spec = catalyst.util.spec(myspecfile)
+               spec = catalyst.config.SpecParser(myspecfile)
                addlargs.update(spec.get_values())
        
        if mycmdline:
diff --git a/modules/catalyst/config.py b/modules/catalyst/config.py
new file mode 100644 (file)
index 0000000..2b02f18
--- /dev/null
@@ -0,0 +1,108 @@
+class ParserBase:
+
+       filename = ""
+       lines = None
+       values = None
+       key_value_separator = "="
+       multiple_values = False
+       empty_values = True
+
+       def __getitem__(self, key):
+               return self.values[key]
+
+       def get_values(self):
+               return self.values
+
+       def parse_file(self, filename):
+               try:
+                       myf = open(filename, "r")
+               except:
+                       raise CatalystError, "Could not open file " + filename
+               self.lines = myf.readlines()
+               myf.close()
+               self.filename = filename
+               self.parse()
+
+       def parse_lines(self, lines):
+               self.lines = lines
+               self.parse()
+
+       def parse(self):
+               values = {}
+               cur_array = []
+
+               trailing_comment=re.compile('\s*#.*$')
+               white_space=re.compile('\s+')
+
+               for x, myline in enum(self.lines):
+                       myline = myline.strip()
+
+                       # Force the line to be clean 
+                       # Remove Comments ( anything following # )
+                       myline = trailing_comment.sub("", myline)
+
+                       # Skip any blank lines
+                       if not myline: continue
+
+                       # Look for separator
+                       msearch = myline.find(self.key_value_separator)
+               
+                       # If separator found assume its a new key
+                       if msearch != -1:
+                               # Split on the first occurence of the separator creating two strings in the array mobjs
+                               mobjs = myline.split(self.key_value_separator, 1)
+                               mobjs[1] = mobjs[1].strip().strip('"')
+
+#                              # Check that this key doesn't exist already in the spec
+#                              if values.has_key(mobjs[0]):
+#                                      raise Exception("You have a duplicate key (" + mobjs[0] + ") in your spec. Please fix it")
+
+                               # Start a new array using the first element of mobjs
+                               cur_array = [mobjs[0]]
+                               if mobjs[1]:
+                                       if self.multiple_values:
+                                               # split on white space creating additional array elements
+#                                              subarray = white_space.split(mobjs[1])
+                                               subarray = mobjs[1].split()
+                                               cur_array += subarray
+                                       else:
+                                               cur_array += mobjs[1]
+
+                       # Else add on to the last key we were working on
+                       else:
+                               if self.multiple_values:
+#                                      mobjs = white_space.split(myline)
+#                                      cur_array += mobjs
+                                       cur_array += myline.split()
+                               else:
+                                       throw 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:
+                               values[cur_array[0]] = cur_array[1]
+                       else:
+                               values[cur_array[0]] = cur_array[1:]
+       
+               if not self.empty_values:
+                       for x in values.keys():
+                               # Delete empty key pairs
+                               if not values[x]:
+                                       print "\n\tWARNING: No value set for key " + x + "...deleting"
+                                       del values[x]
+
+               self.values = values
+
+class SpecParser:
+
+       key_value_separator = ':'
+       multiple_values = True
+       empty_values = False
+
+       def __init__(self, filename=""):
+               if filename:
+                       self.parse_file(filename)
+
+       def dump(self):
+               dump = ""
+               for x in self.values.keys():
+                       dump += x + ": " + repr(self.values[x]) + "\n"
diff --git a/modules/catalyst/util.py b/modules/catalyst/util.py
deleted file mode 100644 (file)
index c637aea..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-class spec:
-
-       filename = ""
-       speclines = None
-       values = None
-
-       def __init__(self, filename=""):
-               if filename:
-                       self.parse(filename)
-
-       def __getitem__(self, key):
-               return self.values[key]
-
-       def get_values(self):
-               return self.values
-
-       def dump(self):
-               dump = ""
-               for x in self.values.keys():
-                       dump += x + ": " + repr(self.values[x]) + "\n"
-
-       def load(self, filename):
-               self.filename = filename
-               try:
-                       myf = open(filename, "r")
-               except:
-                       raise CatalystError, "Could not open spec file " + filename
-               self.speclines = myf.readlines()
-               myf.close()
-               self.parse()
-
-       def parse(self):
-               myspec = {}
-               cur_array = []
-               trailing_comment=re.compile("#.*$")
-               white_space=re.compile("\s+")
-               while len(self.speclines):
-                       myline = mylines.pop(0).strip()
-
-                       # Force the line to be clean 
-                       # Remove Comments ( anything following # )
-                       myline = trailing_comment.sub("", myline)
-
-                       # Skip any blank lines
-                       if not myline: continue
-
-                       # Look for colon
-                       msearch = myline.find(':')
-               
-                       # If semicolon found assume its a new key
-                       # This may cause problems if : are used for key values but works for now
-                       if msearch != -1:
-                               # Split on the first semicolon creating two strings in the array mobjs
-                               mobjs = myline.split(':', 1)
-                               mobjs[1] = mobjs[1].strip()
-
-                               # Check that this key doesn't exist already in the spec
-                               if myspec.has_key(mobjs[0]):
-                                       raise Exception("You have a duplicate key (" + mobjs[0] + ") in your spec. Please fix it")
-
-                               # Start a new array using the first element of mobjs
-                               cur_array = [mobjs[0]]
-                               if mobjs[1]:
-                                       # split on white space creating additional array elements
-                                       subarray = white_space.split(mobjs[1])
-                                       if subarray:
-                                               if len(subarray)==1:
-                                                       # Store as a string if only one element is found.
-                                                       # this is to keep with original catalyst behavior 
-                                                       # eventually this may go away if catalyst just works
-                                                       # with arrays.
-                                                       cur_array.append(subarray[0])
-                                               else:
-                                                       cur_array += subarray
-               
-                       # Else add on to the last key we were working on
-                       else:
-                               mobjs = white_space.split(myline)
-                               cur_array += mobjs
-               
-                       # XXX: Do we really still need this "single value is a string" behavior?
-                       if len(cur_array) == 2:
-                               myspec[cur_array[0]] = cur_array[1]
-                       else:
-                               myspec[cur_array[0]] = cur_array[1:]
-       
-               for x in myspec.keys():
-                       # Delete empty key pairs
-                       if not myspec[x]:
-                               print "\n\tWARNING: No value set for key " + x + "...deleting"
-                               del myspec[x]
-               self.values = myspec
-
-
-