def ReadSection(self): rc_text = self._LoadInputFile() out = '' begin_count = 0 assert self.extkey first_line_re = re.compile(r'\s*' + self.extkey + r'\b') for line in rc_text.splitlines(True): if out or first_line_re.match(line): out += line # we stop once we reach the END for the outermost block. begin_count_was = begin_count if len(out) > 0 and line.strip() == 'BEGIN': begin_count += 1 elif len(out) > 0 and line.strip() == 'END': begin_count -= 1 if begin_count_was == 1 and begin_count == 0: break if len(out) == 0: raise exception.SectionNotFound('%s in file %s' % (self.extkey, self.rc_file)) self.text_ = out.strip()
def FromFileImpl(rc_file, extkey, encoding, type): '''Implementation of FromFile. Need to keep separate so we can have a FromFile in this class that has its type set to Section by default. ''' if isinstance(rc_file, types.StringTypes): rc_file = util.WrapInputStream(file(rc_file, 'r'), encoding) out = '' begin_count = 0 for line in rc_file.readlines(): if len(out) > 0 or (line.strip().startswith(extkey) and line.strip().split()[0] == extkey): out += line # we stop once we reach the END for the outermost block. begin_count_was = begin_count if len(out) > 0 and line.strip() == 'BEGIN': begin_count += 1 elif len(out) > 0 and line.strip() == 'END': begin_count -= 1 if begin_count_was == 1 and begin_count == 0: break if len(out) == 0: raise exception.SectionNotFound('%s in file %s' % (extkey, rc_file)) return type(out)
def Parse(self): '''Implementation for resource types w/braces (not BEGIN/END) ''' rc_text = self._LoadInputFile() out = '' begin_count = 0 openbrace_count = 0 assert self.extkey first_line_re = re.compile(r'\s*' + self.extkey + r'\b') for line in rc_text.splitlines(True): if out or first_line_re.match(line): out += line # We stop once the braces balance (could happen in one line). begin_count_was = begin_count if len(out) > 0: openbrace_count += line.count('{') begin_count += line.count('{') begin_count -= line.count('}') if ((begin_count_was == 1 and begin_count == 0) or (openbrace_count > 0 and begin_count == 0)): break if len(out) == 0: raise exception.SectionNotFound('%s in file %s' % (self.extkey, self.rc_file)) self.text_ = out self._RegExpParse(self.dialog_re_, out)
def FromFile(rc_file, extkey, encoding = 'cp1252'): '''Implementation of FromFile for resource types w/braces (not BEGIN/END) ''' if isinstance(rc_file, types.StringTypes): rc_file = util.WrapInputStream(file(rc_file, 'r'), encoding) out = '' begin_count = 0 openbrace_count = 0 for line in rc_file.readlines(): if len(out) > 0 or line.strip().startswith(extkey): out += line # we stop once balance the braces (could happen on one line) begin_count_was = begin_count if len(out) > 0: openbrace_count += line.count('{') begin_count += line.count('{') begin_count -= line.count('}') if ((begin_count_was == 1 and begin_count == 0) or (openbrace_count > 0 and begin_count == 0)): break if len(out) == 0: raise exception.SectionNotFound('%s in file %s' % (extkey, rc_file)) return RCData(out)