def codec(self, version=1, corrupt=False, ignore_corrupt=False, **kwargs): buf = BytesIO() stream = DiscoOutputStream(buf, version=version, **kwargs) t = self.encode(stream, self.data) final_size = len(buf.getvalue()) final_mb = final_size / 1024**2 msg = (("{0:1.2f}MB encoded in {1:1.3f}s ({2:1.2f}MB/s), " "encoded size {3:1.3f}MB (version: {4}, {5})") .format(self.size, t, self.size / t, final_mb, version, kwargs)) if corrupt: buf.seek(0) new = BytesIO() new.write(buf.read(100)) new.write(b'X') buf.read(1) new.write(buf.read()) buf = new buf.seek(0) t, res = self.decode(buf, final_size, "nourl", ignore_corrupt=ignore_corrupt) if not ignore_corrupt: print("{0}, decoded in {1:1.3f}s ({2:1.2f}MB/s)" .format(msg, t, self.size / t)) return res
class DiscoZipFile(ZipFile, object): def __init__(self): self.buffer = BytesIO() super(DiscoZipFile, self).__init__(self.buffer, 'w', ZIP_DEFLATED) def writepath(self, pathname, exclude=()): for file in files(pathname): name, ext = os.path.splitext(file) if ext not in exclude: self.write(file, file) def writemodule(self, module, arcname=None): if isinstance(module, basestring): module = __import__(module) self.write(getsourcefile(module), arcname=arcname) def writesource(self, object): self.writepath(getsourcefile(getmodule(object))) def dump(self, handle): handle.write(self.dumps()) def dumps(self): self.buffer.seek(0) return self.buffer.read()