def score(filename, fileobj, header): try: seek_end(fileobj, 160) footer = fileobj.read() except IOError: return -1 return ((b"APETAGEX" in footer) - header.startswith(b"ID3"))
def find_last(fileobj, serial): """Find the last page of the stream 'serial'. If the file is not multiplexed this function is fast. If it is, it must read the whole the stream. This finds the last page in the actual file object, or the last page in the stream (with eos set), whichever comes first. Returns None in case no page with the serial exists. Raises error in case this isn't a valid ogg stream. Raises IOError. """ # For non-muxed streams, look at the last page. seek_end(fileobj, 256 * 256) data = fileobj.read() try: index = data.rindex(b"OggS") except ValueError: raise error("unable to find final Ogg header") bytesobj = cBytesIO(data[index:]) best_page = None try: page = OggPage(bytesobj) except error: pass else: if page.serial == serial: if page.last: return page else: best_page = page else: best_page = None # The stream is muxed, so use the slow way. fileobj.seek(0) try: page = OggPage(fileobj) while not page.last: page = OggPage(fileobj) while page.serial != serial: page = OggPage(fileobj) best_page = page return page except error: return best_page except EOFError: return best_page
def test_seek_end(self): with self.file(b"foo") as f: seek_end(f, 2) self.assertEqual(f.tell(), 1) seek_end(f, 3) self.assertEqual(f.tell(), 0) seek_end(f, 4) self.assertEqual(f.tell(), 0) seek_end(f, 0) self.assertEqual(f.tell(), 3) self.assertRaises(ValueError, seek_end, f, -1)
def test_seek_end(self): f = cBytesIO(b"foo") seek_end(f, 2) self.assertEqual(f.tell(), 1) seek_end(f, 3) self.assertEqual(f.tell(), 0) seek_end(f, 4) self.assertEqual(f.tell(), 0) seek_end(f, 0) self.assertEqual(f.tell(), 3) self.assertRaises(ValueError, seek_end, f, -1)
def find_last(fileobj, serial, finishing=False): """Find the last page of the stream 'serial'. If the file is not multiplexed this function is fast. If it is, it must read the whole the stream. This finds the last page in the actual file object, or the last page in the stream (with eos set), whichever comes first. If finishing is True it returns the last page which contains a packet finishing on it. If there exist pages but none with finishing packets returns None. Returns None in case no page with the serial exists. Raises error in case this isn't a valid ogg stream. Raises IOError. """ # For non-muxed streams, look at the last page. seek_end(fileobj, 256 * 256) data = fileobj.read() try: index = data.rindex(b"OggS") except ValueError: raise error("unable to find final Ogg header") bytesobj = cBytesIO(data[index:]) def is_valid(page): return not finishing or page.position != -1 best_page = None try: page = OggPage(bytesobj) except error: pass else: if page.serial == serial and is_valid(page): if page.last: return page else: best_page = page else: best_page = None # The stream is muxed, so use the slow way. fileobj.seek(0) try: page = OggPage(fileobj) while True: if page.serial == serial: if is_valid(page): best_page = page if page.last: break page = OggPage(fileobj) return best_page except error: return best_page except EOFError: return best_page
def test_seek_end_pos(self): with self.file(b"foo") as f: f.seek(10) seek_end(f, 10) self.assertEqual(f.tell(), 0)