def read(self, fp): sections = [] matched = False self.valid = True metaData = defaultdict(list) for (lineNumber, line) in enumerate(fp.readlines(), 1): for formatType, format in self.formats: if isinstance(line, bytes): match = format.match(line.decode()) else: match = format.match(line) if match: matched = True container = Container() container.lineNumber = lineNumber dict_ = match.groupdict() if dict_ != {}: # Handle scalar values. for key, value in dict_.items(): if key not in ('chunk', 'junk'): setattr(container, key, atoi(value)) elif key == 'junk': setattr(container, key, value) if 'chunk' in dict_: if self.parseData(container, formatType): chunk = bytearray( map(atoi, BYTES.findall(dict_['chunk']))) else: # don't convert/parse stuff like symbols. chunk = dict_['chunk'] dict_.pop('chunk') setattr(container, 'chunk', chunk) self.checkLine(container, formatType) # this is to handle esoteric stuff like Intel seg:offs addressing and symbols. self.specialProcessing(container, formatType) if self.isDataLine(container, formatType): # print chunk sections.append( Section(container.address, container.chunk)) else: chunk = container.chunk if hasattr( container, 'chunk') else None address = container.address if hasattr( container, 'address') else None metaData[formatType].append( MetaRecord(formatType, address, chunk)) break if not matched: self.warn("Ignoring garbage line #{0:d}".format(lineNumber)) if sections: return Image(joinSections(sections), metaData, self.valid) else: self.error("File seems to be invalid.") return Image([], valid=False)
def read(self, fp): if PYTHON_VERSION.major == 3: lines = fp.read().decode() else: lines = fp.read() self.sections = [] self.address = 0 breakRequest = False for line in lines.splitlines(): for pattern, action in self.patterns: match = pattern.match(line) if match: if not action(line, match): breakRequest = True break if breakRequest: break return Image(joinSections(self.sections))
def joinSections(self, orderSegments=None): self._sections = joinSections(self._sections, orderSegments)