From 515e5733a4f911843c4ccb0b4a413801135fc91a Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Tue, 8 Mar 2011 16:55:55 +0100 Subject: [PATCH] Added staticfiles compatible file finder to be independent from the FileSystemFinder. --- compressor/base.py | 4 ++-- compressor/finders.py | 23 +++++++++++++++++++++++ compressor/settings.py | 39 +++++++++++---------------------------- docs/index.txt | 42 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 72 insertions(+), 36 deletions(-) create mode 100644 compressor/finders.py diff --git a/compressor/base.py b/compressor/base.py index 2095b9f..aae52b2 100644 --- a/compressor/base.py +++ b/compressor/base.py @@ -32,8 +32,8 @@ class Compressor(object): base_url = settings.COMPRESS_URL if not url.startswith(base_url): raise UncompressableFileError( - "'%s' is not in COMPRESS_URL ('%s') and can not be compressed" - % (url, base_url)) + "'%s' isn't accesible via COMPRESS_URL ('%s') and can't be" + " compressed" % (url, base_url)) basename = url.replace(base_url, "", 1) filename = os.path.join(settings.COMPRESS_ROOT, basename) if not os.path.exists(filename): diff --git a/compressor/finders.py b/compressor/finders.py new file mode 100644 index 0000000..e9f9431 --- /dev/null +++ b/compressor/finders.py @@ -0,0 +1,23 @@ +from django.core.exceptions import ImproperlyConfigured + +from compressor.conf import settings +from compressor.storage import CompressorFileStorage + +if "django.contrib.staticfiles" in settings.INSTALLED_APPS: + from django.contrib.staticfiles.finders import BaseStorageFinder +elif "staticfiles" in settings.INSTALLED_APPS: + from staticfiles import BaseStorageFinder +else: + raise ImproperlyConfigured("When using the compressor staticfiles finder" + "either django.contrib.staticfiles or the " + "standalone version django-staticfiles needs " + "to be installed.") + +class CompressorFinder(BaseStorageFinder): + """ + A staticfiles finder that looks in COMPRESS_ROOT + for compressed files, to be used during development + with staticfiles development file server or during + deployment. + """ + storage = CompressorFileStorage diff --git a/compressor/settings.py b/compressor/settings.py index 4deddc7..e2db3c1 100644 --- a/compressor/settings.py +++ b/compressor/settings.py @@ -63,35 +63,18 @@ class CompressorSettings(AppSettings): # In case staticfiles is used, make sure the FileSystemFinder is # installed, and if it is, check if COMPRESS_ROOT is listed in # STATICFILES_DIRS to allow finding compressed files - if ("staticfiles" in self.INSTALLED_APPS or - "django.contrib.staticfiles" in self.INSTALLED_APPS): - try: - from staticfiles.conf import settings as staticfiles_settings - finders = staticfiles_settings.STATICFILES_FINDERS - standalone = True - except ImportError: - finders = [] - standalone = False - if not finders: - finders = getattr(settings, 'STATICFILES_FINDERS', []) - if ("django.contrib.staticfiles.finders.FileSystemFinder" not in finders and - "staticfiles.finders.FileSystemFinder" not in finders): + staticfiles_settings = None + if "staticfiles" in self.INSTALLED_APPS: + from staticfiles.conf import settings as staticfiles_settings + elif "django.contrib.staticfiles" in self.INSTALLED_APPS: + staticfiles_settings = settings + if staticfiles_settings is not None: + if ("compressor.finders.CompressorFinder" not in + staticfiles_settings.STATICFILES_FINDERS): raise ImproperlyConfigured( - 'Please enable the FileSystemFinder finder of the ' - 'staticfiles app to use it with django_compressor.') - abs_paths = [] - output_path = os.path.join(value, self.COMPRESS_OUTPUT_DIR) - for path in getattr(settings, 'STATICFILES_DIRS', []): - if isinstance(path, tuple) or isinstance(path, list): # stupid Python 2.4 - path = path[1] # in case the STATICFILES_DIRS setting has a prefix - abs_paths.append(os.path.abspath(path)) - if os.path.abspath(output_path) not in abs_paths: - extension = ((self.COMPRESS_OUTPUT_DIR, output_path),) - if standalone: - from staticfiles.conf import settings as staticfiles_settings - staticfiles_settings.STATICFILES_DIRS += extension - else: - settings.STATICFILES_DIRS += extension + "When using django_compressor together with staticfiles, " + "please add 'compressor.finders.CompressorFinder' to the " + "STATICFILES_FINDERS setting.") return value def configure_url(self, value): diff --git a/docs/index.txt b/docs/index.txt index 6130638..c925134 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -3,8 +3,36 @@ Django compressor Compresses linked and inline JavaScript or CSS into a single cached file. -Syntax ------- +Installation +------------ + +* Install django_compressor with your favorite Python package manager:: + + pip install django_compressor + +* Add ``'compressor'`` to your ``INSTALLED_APPS`` setting:: + + INSTALLED_APPS = ( + # other apps + "compressor", + ) + +* See the list of settings_ to modify django_compressor's default behaviour. + +* In case you use Django 1.3's staticfiles_ contrib app (or its standalone + clone django-staticfiles_) you have to add django_compressor's file finder + to the ``STATICFILES_FINDERS`` setting:: + + STATICFILES_FINDERS = ( + # other finders.. + 'compressor.finders.CompressorFinder', + ) + +.. _staticfiles: http://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/ +.. _django-staticfiles: http://pypi.python.org/pypi/django-staticfiles + +Usage +----- .. code-block:: django @@ -14,7 +42,7 @@ Syntax {% endcompress %} Examples --------- +^^^^^^^^ .. code-block:: django @@ -49,8 +77,9 @@ Which would be rendered something like: -Linked files must be accesible via COMPRESS_URL_. If DEBUG is true off-site -files will throw exceptions. If DEBUG is false they will be silently stripped. +Linked files must be accesible via COMPRESS_URL_. If DEBUG is ``True``, +off-site files will throw exceptions. If DEBUG is ``False`` they will be +silently stripped. If COMPRESS is ``False`` (defaults to the opposite of DEBUG) the ``compress`` template tag simply returns exactly what it was given, to ease development. @@ -145,7 +174,8 @@ COMPRESS_ROOT :Default: ``STATIC_ROOT`` (``MEDIA_ROOT`` for older Django versions) Controls the absolute file path that linked static will be read from and -compressed static will be written to. +compressed static will be written to when using the default COMPRESS_STORAGE_ +``compressor.storage.CompressorFileStorage``. .. note::