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<>'':