def test(self): if False: # to help with debugging: # print the first 16 sync points - some _may_ be in the audio data bfr = CornuCopyBuffer.from_filename(TESTFILE) count = 16 while not bfr.at_eof() and count > 0: bs = b''.join(MP3AudioFrame.scan_for_sync(bfr)) X("AUDIO at %d after %d bytes", bfr.offset, len(bs)) bfr.take(1) count -= 1 S = os.stat(TESTFILE) mp3_size = S.st_size bfr = CornuCopyBuffer.from_filename(TESTFILE) for offset, frame, post_offset in MP3Frame.scan_with_offsets(bfr): frame_size = post_offset - offset frame_bs = bytes(frame) ##frame2 = MP3Frame.from_bytes(frame_bs) ##self.assertIs(type(frame), type(frame2)) # There used to be a round trip size check, but we repair # some input data and write it out correctly, so the size can # change. Example: a USC-2 text field missing its BOM. self.assertEqual( bfr.offset, mp3_size, "file size = %d, buffer offset = %d" % (mp3_size, bfr.offset)) self.assertTrue(bfr.at_eof()) bfr.close()
def from_pathname(cls, pathname, readsize=None, **kw): ''' Compute hashcode from the contents of the file `pathname`. ''' if readsize is None: readsize = DEFAULT_READSIZE return cls.from_buffer( CornuCopyBuffer.from_filename(pathname, readsize=readsize, **kw) )
def _test_chunks(data_spec): ''' Return an iterable of chunks from a data spec (filename or list-of-bytes). ''' # obtain the test data if data_spec is None: chunks = None elif isinstance(data_spec, str): chunks = CornuCopyBuffer.from_filename(data_spec) elif isinstance(data_spec, (list, tuple)): chunks = data_spec else: raise RuntimeError("unexpected data_spec of type %s" % (type(data_spec), )) return chunks
def cmd_tags(argv): ''' Usage: {cmd} mp3filenames... Print the tags from the named files. ''' xit = 0 first_print = True for mp3path in argv: with Pfx(mp3path): try: bfr = CornuCopyBuffer.from_filename(mp3path) except Exception as e: # pylint: disable=broad-except error(e) xit = 1 continue tags = tags_of(bfr) if not first_print: print() first_print = False print(mp3path) for tag in tags: print(' ', tag) return xit
def scanfrom(filepath, offset=0): ''' Scan the specified `filepath` from `offset`, yielding `DataRecord`s. ''' bfr = CornuCopyBuffer.from_filename(filepath, offset=offset) yield from DataRecord.scan_with_offsets(bfr)