===== tools/pygrub/src/GrubConf.py 1.2 vs edited =====
--- 1.2/tools/pygrub/src/GrubConf.py	2005-04-28 21:27:46 +09:00
+++ edited/tools/pygrub/src/GrubConf.py	2005-05-19 01:39:58 +09:00
@@ -12,8 +12,7 @@
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 #
 
-import os, sys
-import logging
+import os, sys, logging
 
 def grub_split(s, maxsplit = -1):
     """Split a grub option screen separated with either '=' or whitespace."""
@@ -27,7 +26,7 @@
     if (tab != -1 and tab < sp) or (tab != -1 and sp == -1):
         sp = tab
 
-    if eq != -1 and eq < sp or (eq != -1 and sp == -1):
+    if (eq < sp) or (sp == -1):
         return s.split('=', maxsplit)
     else:
         return s.split(None, maxsplit)
@@ -42,6 +41,29 @@
     d = s[:idx]
     return (GrubDiskPart(d), s[idx + 1:])
 
+def get_color(s):
+    """Returns a tuple of ((fg,bg), (fg,bg)) corresponding to menu and item color."""
+    s.strip()
+    idx = s.find(' ')
+    if idx == -1:
+        return None
+    c = s.split(None, 1)
+    menucolor = c[0]
+    idx = menucolor.find('/')
+    if idx == -1:
+        return None
+    menufg = menucolor[:idx]
+    menubg = menucolor[idx + 1:]
+    if len(c) == 1:
+        return ((menufg, menubg))
+    itemcolor = c[1]
+    idx = itemcolor.find('/')
+    if idx == -1:
+        return None
+    itemfg = itemcolor[:idx]
+    itembg = itemcolor[idx + 1:]
+    return ((menufg, menubg), (itemfg, itembg))
+
 class GrubDiskPart(object):
     def __init__(self, str):
         if str.find(',') != -1:
@@ -58,29 +80,37 @@
 
     def get_disk(self):
         return self._disk
+
     def set_disk(self, val):
         val = val.replace("(", "").replace(")", "")
         self._disk = int(val[2:])
+
     disk = property(get_disk, set_disk)
 
     def get_part(self):
         return self._part
+
     def set_part(self, val):
         if val is None:
             self._part = val
             return
         val = val.replace("(", "").replace(")", "")
         self._part = int(val)
+
     part = property(get_part, set_part)
 
 class GrubImage(object):
     def __init__(self, lines):
         self._root = self._initrd = self._kernel = self._args = None
         for l in lines:
-            (com, arg) = grub_split(l, 1)
-
+            up = grub_split(l, 1)
+            if len(up) >1:
+                (com, arg) = up
+            else:
+                com = up[0]
+                arg = None
             if self.commands.has_key(com):
-                if self.commands[com] is not None:
+                if self.commands[com] is not None and arg is not None:
                     exec("%s = r\"%s\"" %(self.commands[com], arg.strip()))
                 else:
                     logging.info("Ignored image directive %s" %(com,))
@@ -92,13 +122,15 @@
                 "  root: %s\n"
                 "  kernel: %s\n"
                 "  args: %s\n"
-                "  initrd: %s" %(self.title, self.root, self.kernel,
+                "  initrd: %s\n" %(self.title, self.root, self.kernel,
                                    self.args, self.initrd))
 
     def set_root(self, val):
         self._root = GrubDiskPart(val)
+
     def get_root(self):
         return self._root
+
     root = property(get_root, set_root)
 
     def set_kernel(self, val):
@@ -109,17 +141,23 @@
         (kernel, args) = val.split(None, 1)
         self._kernel = get_path(kernel)
         self._args = args
+
     def get_kernel(self):
         return self._kernel
+
+    kernel = property(get_kernel, set_kernel)
+
     def get_args(self):
         return self._args
-    kernel = property(get_kernel, set_kernel)
+
     args = property(get_args)
 
     def set_initrd(self, val):
         self._initrd = get_path(val)
+
     def get_initrd(self):
         return self._initrd
+
     initrd = property(get_initrd, set_initrd)
 
     # set up command handlers
@@ -129,7 +167,11 @@
                  "kernel": "self.kernel",
                  "initrd": "self.initrd",
                  "chainloader": None,
-                 "module": None}
+                 "module": None,
+                 "boot": None,
+                 "pause": None,
+                 "lock": None,
+                 "savedefault": None }
         
 
 class GrubConfigFile(object):
@@ -155,6 +197,7 @@
         img = []
         for l in lines:
             l = l.strip()
+            l = l.strip('\x00')
             # skip blank lines
             if len(l) == 0:
                 continue
@@ -171,7 +214,6 @@
             if len(img) > 0:
                 img.append(l)
                 continue
-
             try:
                 (com, arg) = grub_split(l, 1)
             except ValueError:
@@ -185,45 +227,56 @@
                     logging.info("Ignored directive %s" %(com,))
             else:
                 logging.warning("Unknown directive %s" %(com,))
-                
         if len(img) > 0:
             self.images.append(GrubImage(img))
 
-    def _get_default(self):
+    def get_default(self):
         return self._default
-    def _set_default(self, val):
+
+    def set_default(self, val):
         if val == "saved":
-            self._default = -1
+            self._default = 0   # use the first item as default value
         else:
             self._default = int(val)
 
         if self._default < 0:
             raise ValueError, "default must be positive number"
-    default = property(_get_default, _set_default)
+
+    default = property(get_default, set_default)
 
     def set_splash(self, val):
         self._splash = get_path(val)
+
     def get_splash(self):
         return self._splash
+
     splash = property(get_splash, set_splash)
 
+    def set_color(self, val):
+        self._color = get_color(val)
+
+    def get_color(self):
+        return self._color
+
+    color = property(get_color, set_color)
+
     # set up command handlers
     commands = { "default": "self.default",
                  "timeout": "self.timeout",
                  "fallback": "self.fallback",
                  "hiddenmenu": "self.hiddenmenu",
                  "splashimage": "self.splash",
+                 "color": "self.color",
                  "password": "self.password" }
-    for c in ("bootp", "color", "device", "dhcp", "hide", "ifconfig",
+    for c in ("bootp", "device", "dhcp", "hide", "ifconfig",
               "pager", "partnew", "parttype", "rarp", "serial",
               "setkey", "terminal", "terminfo", "tftpserver", "unhide"):
         commands[c] = None
-    del c
 
 
 if __name__ == "__main__":
-    if sys.argv < 2:
-        raise RuntimeError, "Need a grub.conf to read"
+    if len(sys.argv) < 2:
+        raise RuntimeError, "Need a grub configuration file to read"
     g = GrubConfigFile(sys.argv[1])
     for i in g.images:
-        print i #, i.title, i.root, i.kernel, i.args, i.initrd
+        print i
