diff options
author | Rob Austein <sra@hactrn.net> | 2007-08-02 02:29:06 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2007-08-02 02:29:06 +0000 |
commit | f4a34d2b984c243b1b1706b3748ce039a8a82d78 (patch) | |
tree | ed40c5cc3203c4f80b155fc1b954d428771cd691 /pow/POW-0.7/cfgparse.py | |
parent | dfbd879eb471809dd58b449e1fe63b5c7a2c9ee4 (diff) |
Rewrite parser as the old one was hopelessly buggy.
svn path=/pow/POW-0.7/cfgparse.py; revision=817
Diffstat (limited to 'pow/POW-0.7/cfgparse.py')
-rwxr-xr-x | pow/POW-0.7/cfgparse.py | 95 |
1 files changed, 36 insertions, 59 deletions
diff --git a/pow/POW-0.7/cfgparse.py b/pow/POW-0.7/cfgparse.py index d1122bba..303eb969 100755 --- a/pow/POW-0.7/cfgparse.py +++ b/pow/POW-0.7/cfgparse.py @@ -58,72 +58,49 @@ class Parser: def __init__(self, filename): self.file = filename self.handle = open(self.file) - self.whiteMatch = re.compile(r'^\s*') - self.hashMatch = re.compile(r'^#.*') + self.whiteMatch = re.compile(r'^\s*$') + self.hashMatch = re.compile(r'^#') self.warningMatch = re.compile(r'^Warning') - self.oidNameMatch = re.compile(r'(^[\w\-\.\? ]*)(?:\([\w\-# ]*\)\s?)?(?:\()([\d ]*)(?:\))') - self.oidMatch = re.compile(r'(?:^OID = )(.*)') - self.commentMatch = re.compile(r'(?:^Comment = )(.*)') - self.descriptionMatch = re.compile(r'(?:^Description = )(.*)') + self.oidMatch = re.compile(r'(?:^OID\s*=\s* )(.*)') + self.commentMatch = re.compile(r'(?:^Comment\s*=\s*)(.*)') + self.descriptionMatch = re.compile(r'(?:^Description\s*=\s*)(.*)') self.oids = {} self.objs = {} self.__parse() - def __update(self, dict, line): - m = self.oidMatch.match( line ) - if m: - dict['hexoid'] = m.group(1) - dict['oid'] = decodeOid(m.group(1)) - return 0 - else: - m = self.commentMatch.match( line ) - if m: - dict['comment'] = m.group(1) - return 0 - else: - m = self.descriptionMatch.match( line ) - if m: - dict['description'] = m.group(1) - n = self.oidNameMatch.match( m.group(1) ) - if n: - dict['name'] = string.strip( n.group(1) ) - else: - dict['name'] = m.group(1) - return 0 - else: - m = self.warningMatch.match( line ) - if m: - return 1 - else: - m = self.whiteMatch.match( line ) - if m: - return 0 - else: - m = self.hashMatch.match( line ) - if m: - return 0 - else: - raise Exception, 'unhandled pattern' + def __store(self, dict): + self.objs[dict['name']] = dict + self.oids[dict['oid' ]] = dict def __parse(self): - line = self.handle.readline() - dict = {} - complete = None - while line: - warning = self.__update( dict, line ) - - if warning: - complete = None - elif complete: - self.objs[ complete['name'] ] = complete - self.oids[ complete['oid'] ] = complete - complete = None - - if len(dict) == 5: - complete = dict - dict = {} - - line = self.handle.readline() + dict = None + broken = False + for line in self.handle: + m = self.oidMatch.match(line) + if m: + if dict and not broken: + self.__store(dict) + dict = { 'hexoid' : m.group(1), + 'oid' : decodeOid(m.group(1)) } + broken = False + continue + if self.warningMatch.match(line): + broken = True + continue + if self.whiteMatch.match(line) or self.hashMatch.match(line): + continue + m = self.commentMatch.match(line) + if m: + dict['comment'] = m.group(1) + continue + m = self.descriptionMatch.match(line) + if m: + dict['description'] = m.group(1) + dict['name'] = m.group(1).strip().split(' ')[0] + continue + raise Exception, 'unhandled pattern' + if dict and not broken: + self.__store(dict) def dumpobjs(self, path): file = open(path, 'w') |