]> mj.ucw.cz Git - moe.git/commitdiff
Better error reporting
authorMartin Mares <mj@ucw.cz>
Sat, 8 Aug 2009 14:31:44 +0000 (16:31 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 8 Aug 2009 14:31:44 +0000 (16:31 +0200)
t/moe/config.py

index 3280283e0e3c63add243ee591e463ae935e94185..7c41fa2e774d31772e0f676f84f1a74330ad1a5d 100644 (file)
@@ -20,41 +20,52 @@ class MoeConfig:
        if file is not None:
            self.load(file)
        elif name is not None:
+           self.name = name
            self.load(open(name, 'r'))
 
+    def parse_line(self, x):
+       x = x.rstrip("\n").lstrip(" \t")
+       if x=='' or x.startswith('#'):
+           pass
+       else:
+           sep = x.find('=')
+           if sep >= 0:
+               k = x[:sep]
+               v = x[sep+1:]
+               if k.endswith("+"):
+                   k = k[:-1]
+                   if not self.cfg.has_key(k):
+                       self.cfg[k] = [('a','')];
+               else:
+                   self.cfg[k] = []
+               if not key_pattern.match(k):
+                   raise MoeConfigInvalid, "Malformed name of configuration variable"
+               if v.startswith("'"):
+                   v=v[1:]
+                   if not v.endswith("'"):
+                       raise MoeConfigInvalid, "Misquoted string"
+                   self.cfg[k].append(('s', v[:-1]))
+               elif v.startswith('"'):
+                   v=v[1:]
+                   if not v.endswith('"'):
+                       raise MoeConfigInvalid, "Misquoted string"
+                   self.parse_interpolated(self.cfg[k], v[:-1])
+               else:
+                   self.cfg[k].append(('s', v))
+           else:
+               raise MoeConfigInvalid, "Parse error"
+
     def load(self, file):
+        lino = 0
        for x in file.readlines():
-           x = x.rstrip("\n").lstrip(" \t")
-           if x=='' or x.startswith('#'):
-               pass
-           else:
-               sep = x.find('=')
-               if sep >= 0:
-                   k = x[:sep]
-                   v = x[sep+1:]
-                   if k.endswith("+"):
-                       k = k[:-1]
-                       if not self.cfg.has_key(k):
-                           self.cfg[k] = [('a','')];
-                   else:
-                       self.cfg[k] = []
-                   if not key_pattern.match(k):
-                       raise MoeConfigInvalid, "Malformed name of configuration variable"
-                   if v.startswith("'"):
-                       v=v[1:]
-                       if not v.endswith("'"):
-                           raise MoeConfigInvalid, "Misquoted string"
-                       self.cfg[k].append(('s', v[:-1]))
-                   elif v.startswith('"'):
-                       v=v[1:]
-                       if not v.endswith('"'):
-                           raise MoeConfigInvalid, "Misquoted string"
-                       self.parse_interpolated(self.cfg[k], v[:-1])
-                   else:
-                       self.cfg[k].append(('s', v))
-               else:
-                   ## FIXME: Report line numbers
-                   raise MoeConfigInvalid, "Parse error"
+           lino += 1
+           try:
+               self.parse_line(x)
+           except MoeConfigInvalid, x:
+               msg = x.message + ' at line ' + str(lino)
+               if hasattr(self, 'name'):
+                   msg += ' of ' + self.name
+               raise MoeConfigInvalid, msg
 
     def parse_interpolated(self, list, s):
         while s<>'':