From 962213ada3cb8db462ea772ce5ebd3b9a0bd5d8f Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 8 Aug 2009 16:31:44 +0200 Subject: [PATCH] Better error reporting --- t/moe/config.py | 73 ++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/t/moe/config.py b/t/moe/config.py index 3280283..7c41fa2 100644 --- a/t/moe/config.py +++ b/t/moe/config.py @@ -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<>'': -- 2.39.2