Fix favicon (fixes #1)

* Include milla.ico in package_data in setup.py
* Use pkg_resources API to load icon instead of milla.__file__
* Allow caller to specify icon content type
master
Dustin C. Hatch 2012-10-16 19:59:35 -05:00
parent dd2634224c
commit 6154f8b6f8
2 changed files with 28 additions and 18 deletions

View File

@ -33,6 +33,9 @@ setup(
install_requires=install_requires, install_requires=install_requires,
packages=find_packages('src', exclude=['distribute_setup']), packages=find_packages('src', exclude=['distribute_setup']),
package_dir={'': 'src'}, package_dir={'': 'src'},
package_data={
'milla': ['milla.ico'],
},
entry_points={ entry_points={
'milla.request_validator': [ 'milla.request_validator': [
'default = milla.auth:RequestValidator' 'default = milla.auth:RequestValidator'

View File

@ -24,9 +24,8 @@ from one or more of these classes can make things significantly easier.
''' '''
import milla import milla
import os import pkg_resources
__all__ = ['Controller', 'FaviconController']
class Controller(object): class Controller(object):
'''The base controller class '''The base controller class
@ -41,28 +40,36 @@ class Controller(object):
def __after__(self, request): def __after__(self, request):
pass pass
class FaviconController(Controller): class FaviconController(Controller):
'''A controller for the "favicon" '''A controller for the "favicon"
This controller is specifically suited to serve a site "favicon" or This controller is specifically suited to serve a site "favicon" or
bookmark icon. By default, it will serve the *Milla* icon, but you bookmark icon. By default, it will serve the *Milla* icon, but you
can pass an alternate file time to the constructor. can pass an alternate filename to the constructor.
:param icon: Path to an icon to serve :param icon: Path to an icon to serve
:param content_type: Internet media type describing the type of image
used as the favicon, defaults to 'image/x-icon' (Windows ICO format)
''' '''
def __init__(self, icon=None): def __init__(self, icon=None, content_type='image/x-icon'):
if not icon: if icon:
icon = os.path.join(os.path.dirname(milla.__file__), 'milla.ico')
try: try:
self.icon = open(icon) self.icon = open(icon)
except (IOError, OSError): except (IOError, OSError):
self.icon = None self.icon = None
else:
try:
self.icon = pkg_resources.resource_stream('milla', 'milla.ico')
except IOError:
self.icon = None
self.content_type = content_type
def __call__(self, request): def __call__(self, request):
if not self.icon: if not self.icon:
raise milla.HTTPNotFound raise milla.HTTPNotFound
response = milla.Response() response = milla.Response()
response.app_iter = self.icon response.app_iter = self.icon
response.headers['Content-Type'] = 'image/x-icon' response.headers['Content-Type'] = self.content_type
return response return response