PKkROEOSquart_compress/__init__.py""" Quart-Compress. A Python package compressing quart responses for you. """ from .quart_compress import Compress __version__ = "1.0.0" PKkRO&$"" quart_compress/quart_compress.py""" Quart Compress: Compresses your quart responses """ import asyncio from gzip import GzipFile from io import BytesIO from quart import request, current_app class DictCache: def __init__(self): self.data = {} def get(self, key): return self.data.get(key) def set(self, key, value): self.data[key] = value class Compress: """ The Compress object is the entry point for your application. When initialising a Compress object you may optionally provide your :class:`quart.Quart` application object if it is ready. Otherwise, you may provide it later by using the :meth:`init_app` method. :param app: optional :class:`quart.Quart` application object :type app: :class:`quart.Quart` or None """ def __init__(self, app=None): """ An alternative way to pass your :class:`quart.Quart` application object to Quart-Compress. :meth:`init_app` also takes care of some default `settings`_. :param app: the :class:`quart.Quart` application object. """ self.app = app if app is not None: self.init_app(app) def init_app(self, app): defaults = [ ( "COMPRESS_MIMETYPES", [ "text/html", "text/css", "text/xml", "application/json", "application/javascript", ], ), ("COMPRESS_LEVEL", 6), ("COMPRESS_MIN_SIZE", 500), ("COMPRESS_CACHE_KEY", None), ("COMPRESS_CACHE_BACKEND", None), ("COMPRESS_REGISTER", True), ] for k, v in defaults: app.config.setdefault(k, v) backend = app.config["COMPRESS_CACHE_BACKEND"] self.cache = backend() if backend else None self.cache_key = app.config["COMPRESS_CACHE_KEY"] if app.config["COMPRESS_REGISTER"] and app.config["COMPRESS_MIMETYPES"]: app.after_request(self.after_request) async def after_request(self, response): app = self.app or current_app accept_encoding = request.headers.get("Accept-Encoding", "") if ( response.mimetype not in app.config["COMPRESS_MIMETYPES"] or "gzip" not in accept_encoding.lower() or not 200 <= response.status_code < 300 or ( response.content_length is not None and response.content_length < app.config["COMPRESS_MIN_SIZE"] ) or "Content-Encoding" in response.headers ): return response response.direct_passthrough = False if self.cache: key = self.cache_key(response) gzip_content = self.cache.get(key) or self.compress(app, response) self.cache.set(key, gzip_content) else: gzip_content = await self.compress(app, response) response.set_data(gzip_content) response.headers["Content-Encoding"] = "gzip" response.headers["Content-Length"] = response.content_length vary = response.headers.get("Vary") if vary: if "accept-encoding" not in vary.lower(): response.headers["Vary"] = "{}, Accept-Encoding".format(vary) else: response.headers["Vary"] = "Accept-Encoding" return response async def compress(self, app, response): gzip_buffer = BytesIO() if asyncio.iscoroutine(response.get_data()): data = await response.get_data() else: data = response.get_data() with GzipFile( mode="wb", compresslevel=app.config["COMPRESS_LEVEL"], fileobj=gzip_buffer ) as gzip_file: gzip_file.write(data) return gzip_buffer.getvalue() PK(OOZ00'quart_compress2-1.0.0.dist-info/LICENSEMIT License Copyright (c) 2019 Florian Dahlitz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HPO%quart_compress2-1.0.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UrPK!HʟE(quart_compress2-1.0.0.dist-info/METADATATao0_qhbRlC6&uP!tM.lg]9'Y!w8^ZTizXdwgГR4;|wa3JpKsIM,1+(v7a$v8'BOaMKejun] x^C|I@$klUqAתTjFdu-0[`HCnJ%VU k[ΏT /@h sb7ÛJt] | E/?XhGMq@m=AVuexz ₞UJ{ލlB5Uf(]tE6W*J"ɻ|@ћ؉pNZ jx÷ +r#X2-KtSQyo PK!H 0&quart_compress2-1.0.0.dist-info/RECORDͻr@>ϲ(++"" HD "B0eOgbT󝺋6:UsflE9(T`YS /^-RHGt/o¡Sqwm| }ߚc=5v.U_}7OrmN +A1BZܽ@>zK~PKkROEOSquart_compress/__init__.pyPKkRO&$"" quart_compress/quart_compress.pyPK(OOZ00'$quart_compress2-1.0.0.dist-info/LICENSEPK!HPO%quart_compress2-1.0.0.dist-info/WHEELPK!HʟE(,quart_compress2-1.0.0.dist-info/METADATAPK!H 0&quart_compress2-1.0.0.dist-info/RECORDPK