def __init__(self, fn): f = binfile.reader(fn) f.byteorder = ">" self.f = f self.major, self.minor = f.stream("bb") print " version %d.%d" % (self.major, self.minor) if self.major != TAG_MAJOR_VERSION or self.minor > TAG_MINOR_VERSION: print " WARNING: Cannot handle this version!" self.tags = dict(self._gen_tags())
def __init__(self, fn): f = binfile.reader(fn) f.byteorder = '>' self.f = f self.major, self.minor = f.stream('bb') print ' version %d.%d' % (self.major, self.minor) if (self.major != TAG_MAJOR_VERSION or self.minor > TAG_MINOR_VERSION): print " WARNING: Cannot handle this version!" self.tags = dict( self._gen_tags() )
def _gen_tags(self): while True: try: tag_id, size = self.f.stream('HI') except struct.error: return data = self.f.file.read(size) tag_name = TAGS_NAMES[tag_id] try: constructor = TAG_TO_CLASS[tag_name] except KeyError: print " Skipping %s" % tag_name else: print " Parsing %s" % tag_name sub_reader = binfile.reader(StringIO.StringIO(data)) sub_reader.byteorder = '>' data = constructor(sub_reader, self.minor) yield (tag_id, data)
def _gen_tags(self): while True: try: tag_id, size = self.f.stream("HI") except struct.error: return data = self.f.file.read(size) tag_name = TAGS_NAMES[tag_id] try: constructor = TAG_TO_CLASS[tag_name] except KeyError: print " Skipping %s" % tag_name else: print " Parsing %s" % tag_name sub_reader = binfile.reader(StringIO.StringIO(data)) sub_reader.byteorder = ">" data = constructor(sub_reader, self.minor) yield (tag_id, data)