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,
|
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'
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
# Copyright 2011 Dustin C. Hatch
|
# Copyright 2011 Dustin C. Hatch
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
# you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
# You may obtain a copy of the License at
|
# You may obtain a copy of the License at
|
||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# 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 milla
|
||||||
import os
|
import pkg_resources
|
||||||
|
|
||||||
__all__ = ['Controller', 'FaviconController']
|
|
||||||
|
|
||||||
class Controller(object):
|
class Controller(object):
|
||||||
'''The base controller class
|
'''The base controller class
|
||||||
|
|
||||||
This class simply provides empty ``__before__`` and ``__after__``
|
This class simply provides empty ``__before__`` and ``__after__``
|
||||||
methods to facilitate cooperative multiple inheritance.
|
methods to facilitate cooperative multiple inheritance.
|
||||||
'''
|
'''
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue