From 922a82e4e8da9d520528993b3d9fe36c9e9e8253 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Wed, 15 Apr 2015 20:08:50 -0500 Subject: [PATCH] FaviconController: Work without pkg_resources (fixes #4) If the `pkg_resources` module is not available, the `FaviconController` will fall back to finding the default image in the same directory on the filesystem as the `milla` package. --- src/milla/controllers.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/milla/controllers.py b/src/milla/controllers.py index 7e59fa0..1384552 100644 --- a/src/milla/controllers.py +++ b/src/milla/controllers.py @@ -19,13 +19,15 @@ from one or more of these classes can make things significantly easier. :Created: Mar 27, 2011 :Author: dustin -:Updated: $Date$ -:Updater: $Author$ ''' import datetime import milla.util -import pkg_resources +import os +try: + import pkg_resources +except ImportError: + pkg_resources = None class Controller(object): @@ -58,17 +60,23 @@ class FaviconController(Controller): EXPIRY_DAYS = 365 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: + try: + if icon: + self.icon = open(icon, 'rb') + self.content_type = content_type + elif pkg_resources: self.icon = pkg_resources.resource_stream('milla', 'milla.ico') - except IOError: - self.icon = None - self.content_type = content_type + self.content_type = 'image/x-icon' + else: + icon = os.path.join( + os.path.dirname(milla.__file__), + 'milla.ico' + ) + self.icon = open(icon, 'rb') + self.content_type = 'image/x-icon' + except (IOError, OSError): + self.icon = self.content_type = None + def __call__(self, request): if not self.icon: