def with_prefix(prefix: str, *, out: t.IO = sys.stdout) -> t.Optional[str]: o = StringIO() with contextlib.redirect_stdout(o): yield o.seek(0) for line in o: out.write(prefix) out.write(line) if hasattr(out, "getvalue"): return out.getvalue()
def generate_thumbnail_from_bytes(image_bytes: IO, source: str, **kwargs): """Like :py:func:`.generate_thumbnail`, but takes raw image bytes instead of a path""" image_bytes.seek(0) fmt = get_format(source) thumbnail_path = get_thumbnail_path(source) if len(image_bytes.getvalue()) > 0: return generate_thumbnail(image_bytes, thumbnail_path, fmt=fmt, **kwargs) else: logger.error( f'Thumbnails: Failed to save image bytes to thumbnail for {source}' ) return None
def zipper(response): accept_encoding = request.headers.get('Accept-Encoding', '') if 'gzip' not in accept_encoding.lower(): return response response.direct_passthrough = False if (response.status_code < 200 or response.status_code >= 300 or 'Content-Encoding' in response.headers): return response gzip_buffer = IO() gzip_file = gzip.GzipFile(mode='wb', fileobj=gzip_buffer) gzip_file.write(response.data) gzip_file.close() response.data = gzip_buffer.getvalue() response.headers['Content-Encoding'] = 'gzip' response.headers['Vary'] = 'Accept-Encoding' response.headers['Content-Length'] = len(response.data) return response