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 typemaster
parent
dd2634224c
commit
6154f8b6f8
3
setup.py
3
setup.py
|
@ -33,6 +33,9 @@ setup(
|
|||
install_requires=install_requires,
|
||||
packages=find_packages('src', exclude=['distribute_setup']),
|
||||
package_dir={'': 'src'},
|
||||
package_data={
|
||||
'milla': ['milla.ico'],
|
||||
},
|
||||
entry_points={
|
||||
'milla.request_validator': [
|
||||
'default = milla.auth:RequestValidator'
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
# Copyright 2011 Dustin C. Hatch
|
||||
#
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
@ -24,13 +24,12 @@ from one or more of these classes can make things significantly easier.
|
|||
'''
|
||||
|
||||
import milla
|
||||
import os
|
||||
import pkg_resources
|
||||
|
||||
__all__ = ['Controller', 'FaviconController']
|
||||
|
||||
class Controller(object):
|
||||
'''The base controller class
|
||||
|
||||
|
||||
This class simply provides empty ``__before__`` and ``__after__``
|
||||
methods to facilitate cooperative multiple inheritance.
|
||||
'''
|
||||
|
@ -41,28 +40,36 @@ class Controller(object):
|
|||
def __after__(self, request):
|
||||
pass
|
||||
|
||||
|
||||
class FaviconController(Controller):
|
||||
'''A controller for the "favicon"
|
||||
|
||||
|
||||
This controller is specifically suited to serve a site "favicon" or
|
||||
bookmark icon. By default, it will serve the *Milla* icon, but you
|
||||
can pass an alternate file time to the constructor.
|
||||
|
||||
:param icon: Path to an icon to serve
|
||||
can pass an alternate filename to the constructor.
|
||||
|
||||
: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):
|
||||
if not icon:
|
||||
icon = os.path.join(os.path.dirname(milla.__file__), 'milla.ico')
|
||||
try:
|
||||
self.icon = open(icon)
|
||||
except (IOError, OSError):
|
||||
self.icon = None
|
||||
def __init__(self, icon=None, content_type='image/x-icon'):
|
||||
if icon:
|
||||
try:
|
||||
self.icon = open(icon)
|
||||
except (IOError, OSError):
|
||||
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):
|
||||
if not self.icon:
|
||||
raise milla.HTTPNotFound
|
||||
response = milla.Response()
|
||||
response.app_iter = self.icon
|
||||
response.headers['Content-Type'] = 'image/x-icon'
|
||||
response.headers['Content-Type'] = self.content_type
|
||||
return response
|
||||
|
|
Loading…
Reference in New Issue