Skip to content

Commit

Permalink
BACKWARD-INCOMPATIBLE: Added the serve_static_file shortcut for t…
Browse files Browse the repository at this point in the history
…he typical static file use case. Thanks to defunkt for the original patch.

To migrate, simply remove the ``request`` from any calls to ``static_file``.
  • Loading branch information
toastdriven committed Dec 8, 2009
1 parent 9b33b3b commit dde698a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
13 changes: 7 additions & 6 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Primary authors:

* Daniel Lindsley
* Daniel Lindsley (toastdriven)


Contributors:

* Matt Croydon for various patches and suggestions
* Christian Metts for various patches and suggestions
* marly for a patch to an exception's message
* tstromberg for a patch related to printing tracebacks on errors
* mronge for a patch to the static files example
* Matt Croydon (mcroydon) for various patches and suggestions.
* Christian Metts (mintchaos) for various patches and suggestions.
* marly for a patch to an exception's message.
* tstromberg for a patch related to printing tracebacks on errors.
* mronge for a patch to the static files example.
* Chris Wanstrath (defunkt) for static files patches & suggestions.
37 changes: 33 additions & 4 deletions examples/static_files.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
from itty import *

MY_ROOT = os.path.join(os.path.dirname(__file__), 'media')


@get('/')
def index(request):
return 'Hello World!'
return '<img src="media/itty.png">'

# To serve static files, simply setup a standard @get method. You should
# capture the filename/path and get the content-type. If your media root is
# different than where your ``itty.py`` lives, manually setup your root
# directory as well. Finally, use the ``static_file`` handler to serve up the
# directory as well. Finally, use the ``static_file`` helper to serve up the
# file.
@get('/media/(?P<filename>.+)')
def my_media(request, filename):
my_root = os.path.join(os.path.dirname(__file__), 'media')
output = static_file(request, filename=filename, root=my_root)
output = static_file(filename, root=MY_ROOT)
return Response(output, content_type=content_type(filename))


# Alternative, if sane-ish defaults are good enough for you, you can use the
# ``serve_static_file`` handler to do the heavy lifting for you. For example:
@get('/simple/')
def simple(request):
return """
<html>
<head>
<title>Simple CSS</title>
<link rel="stylesheet" type="text/css" href="/simple_media/default.css">
</head>
<body>
<h1>Simple CSS is Simple!</h1>
<p>Simple reset here.</p>
</body>
</html>
"""

# By default, the ``serve_static_file`` will try to guess the correct content
# type. If needed, you can enforce a content type by using the
# ``force_content_type`` kwarg (i.e. ``force_content_type='image/jpg'`` on a
# directory of user uploaded images).
@get('/simple_media/(?P<filename>.+)')
def simple_media(request, filename):
return serve_static_file(request, filename, root=MY_ROOT)


run_itty()
31 changes: 23 additions & 8 deletions itty.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def index(request):
import re
import sys
import traceback
import urlparse
try:
from urlparse import parse_qs
except ImportError:
Expand Down Expand Up @@ -276,20 +275,17 @@ def content_type(filename):
"""
ct = 'text/plain'
ct_guess = mimetypes.guess_type(filename)

if ct_guess[0] is not None:
ct = ct_guess[0]

return ct


# Static file handler

def static_file(request, filename, root=MEDIA_ROOT):
def static_file(filename, root=MEDIA_ROOT):
"""
Basic handler for serving up static media files.
Accepts an optional root (filepath string, defaults to MEDIA_ROOT) parameter.
Fetches a static file from the filesystem, relative to either the given
MEDIA_ROOT or from the provided root directory.
"""
if filename is None:
raise Forbidden("You must specify a file you'd like to access.")
Expand All @@ -311,6 +307,25 @@ def static_file(request, filename, root=MEDIA_ROOT):
return open(desired_path, 'r').read()


# Static file handler

def serve_static_file(request, filename, root=MEDIA_ROOT, force_content_type=None):
"""
Basic handler for serving up static media files.
Accepts an optional ``root`` (filepath string, defaults to ``MEDIA_ROOT``) parameter.
Accepts an optional ``force_content_type`` (string, guesses if ``None``) parameter.
"""
file_contents = static_file(filename, root)

if force_content_type is None:
ct = content_type(filename)
else:
ct = force_content_type

return Response(file_contents, content_type=ct)


# Decorators

def get(url):
Expand Down

0 comments on commit dde698a

Please sign in to comment.