diff options
author | Gustavo Picon <tabo@tabo.pe> | 2014-01-12 13:57:44 -0500 |
---|---|---|
committer | Gustavo Picon <tabo@tabo.pe> | 2014-01-12 13:57:44 -0500 |
commit | c26eb490862e664dc4ef4ea6e55c1dbd106146c4 (patch) | |
tree | f997c23aa6d04fc7db78e66ad5f58429b3be824e /cherrypy/lib/static.py | |
parent | fb84f7713d6ced0031dacb0e7e59d516cc681a4d (diff) | |
download | cherrypy-git-c26eb490862e664dc4ef4ea6e55c1dbd106146c4.tar.gz |
Running: autopep8 -vvvvv -i `find . -name '*.py'`
--HG--
branch : autopep8
Diffstat (limited to 'cherrypy/lib/static.py')
-rw-r--r-- | cherrypy/lib/static.py | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/cherrypy/lib/static.py b/cherrypy/lib/static.py index f55dec1d..83ffec88 100644 --- a/cherrypy/lib/static.py +++ b/cherrypy/lib/static.py @@ -5,10 +5,10 @@ except ImportError: import logging import mimetypes mimetypes.init() -mimetypes.types_map['.dwg']='image/x-dwg' -mimetypes.types_map['.ico']='image/x-icon' -mimetypes.types_map['.bz2']='application/x-bzip2' -mimetypes.types_map['.gz']='application/x-gzip' +mimetypes.types_map['.dwg'] = 'image/x-dwg' +mimetypes.types_map['.ico'] = 'image/x-icon' +mimetypes.types_map['.bz2'] = 'application/x-bzip2' +mimetypes.types_map['.gz'] = 'application/x-gzip' import os import re @@ -92,6 +92,7 @@ def serve_file(path, content_type=None, disposition=None, name=None, debug=False fileobj = open(path, 'rb') return _serve_fileobj(fileobj, content_type, content_length, debug=debug) + def serve_fileobj(fileobj, content_type=None, disposition=None, name=None, debug=False): """Set status, headers, and body in order to serve the given file object. @@ -145,6 +146,7 @@ def serve_fileobj(fileobj, content_type=None, disposition=None, name=None, return _serve_fileobj(fileobj, content_type, content_length, debug=debug) + def _serve_fileobj(fileobj, content_type, content_length, debug=False): """Internal. Set response.body to the given file object, perhaps ranged.""" response = cherrypy.serving.response @@ -169,8 +171,9 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False): stop = content_length r_len = stop - start if debug: - cherrypy.log('Single part; start: %r, stop: %r' % (start, stop), - 'TOOLS.STATIC') + cherrypy.log( + 'Single part; start: %r, stop: %r' % (start, stop), + 'TOOLS.STATIC') response.status = "206 Partial Content" response.headers['Content-Range'] = ( "bytes %s-%s/%s" % (start, stop - 1, content_length)) @@ -199,14 +202,16 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False): for start, stop in r: if debug: - cherrypy.log('Multipart; start: %r, stop: %r' % (start, stop), - 'TOOLS.STATIC') + cherrypy.log( + 'Multipart; start: %r, stop: %r' % ( + start, stop), + 'TOOLS.STATIC') yield ntob("--" + boundary, 'ascii') yield ntob("\r\nContent-type: %s" % content_type, 'ascii') yield ntob("\r\nContent-range: bytes %s-%s/%s\r\n\r\n" % (start, stop - 1, content_length), 'ascii') fileobj.seek(start) - for chunk in file_generator_limited(fileobj, stop-start): + for chunk in file_generator_limited(fileobj, stop - start): yield chunk yield ntob("\r\n") # Final boundary @@ -226,6 +231,7 @@ def _serve_fileobj(fileobj, content_type, content_length, debug=False): response.body = fileobj return response.body + def serve_download(path, name=None): """Serve 'path' as an application/x-download attachment.""" # This is such a common idiom I felt it deserved its own wrapper. @@ -252,6 +258,7 @@ def _attempt(filename, content_types, debug=False): cherrypy.log('NotFound', 'TOOLS.STATICFILE') return False + def staticdir(section, dir, root="", match="", content_types=None, index="", debug=False): """Serve a static resource from the given (root +) dir. @@ -314,7 +321,7 @@ def staticdir(section, dir, root="", match="", content_types=None, index="", # have ".." or similar uplevel attacks in it. Check that the final # filename is a child of dir. if not os.path.normpath(filename).startswith(os.path.normpath(dir)): - raise cherrypy.HTTPError(403) # Forbidden + raise cherrypy.HTTPError(403) # Forbidden handled = _attempt(filename, content_types) if not handled: @@ -325,6 +332,7 @@ def staticdir(section, dir, root="", match="", content_types=None, index="", request.is_index = filename[-1] in (r"\/") return handled + def staticfile(filename, root=None, match="", content_types=None, debug=False): """Serve a static resource from the given (root +) filename. |