Import settings from compressor.conf again instead of django.conf to make sure they are loaded correctly.

This commit is contained in:
Jannis Leidel
2011-09-06 14:11:37 +02:00
parent f57e495991
commit 70cc7aa351
23 changed files with 145 additions and 153 deletions

View File

@@ -2,7 +2,6 @@ from __future__ import with_statement
import os import os
import codecs import codecs
from django.conf import settings
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.template import Context from django.template import Context
from django.template.loader import render_to_string from django.template.loader import render_to_string
@@ -10,6 +9,7 @@ from django.utils.encoding import smart_unicode
from compressor.cache import get_hexdigest, get_mtime from compressor.cache import get_hexdigest, get_mtime
from compressor.conf import settings
from compressor.exceptions import CompressorError, UncompressableFileError from compressor.exceptions import CompressorError, UncompressableFileError
from compressor.filters import CompilerFilter from compressor.filters import CompilerFilter
from compressor.storage import default_storage from compressor.storage import default_storage

View File

@@ -2,13 +2,13 @@ import os
import socket import socket
import time import time
from django.conf import settings
from django.core.cache import get_cache from django.core.cache import get_cache
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
from django.utils.functional import SimpleLazyObject from django.utils.functional import SimpleLazyObject
from django.utils.hashcompat import md5_constructor from django.utils.hashcompat import md5_constructor
from django.utils.importlib import import_module from django.utils.importlib import import_module
from compressor.conf import settings
from compressor.utils import get_mod_func from compressor.utils import get_mod_func
_cachekey_func = None _cachekey_func = None

118
compressor/conf.py Normal file
View File

@@ -0,0 +1,118 @@
import os
from django import VERSION as DJANGO_VERSION
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from appconf import AppConf
class CompressorConf(AppConf):
# Main switch
ENABLED = False
# Allows changing verbosity from the settings.
VERBOSE = False
# GET variable that disables compressor e.g. "nocompress"
DEBUG_TOGGLE = "None"
# the backend to use when parsing the JavaScript or Stylesheet files
PARSER = 'compressor.parser.AutoSelectParser'
OUTPUT_DIR = 'CACHE'
STORAGE = 'compressor.storage.CompressorFileStorage'
CSS_COMPRESSOR = "compressor.css.CssCompressor"
JS_COMPRESSOR = "compressor.js.JsCompressor"
URL = None
ROOT = None
CSS_FILTERS = ['compressor.filters.css_default.CssAbsoluteFilter']
CSS_HASHING_METHOD = 'mtime'
JS_FILTERS = ['compressor.filters.jsmin.JSMinFilter']
PRECOMPILERS = (
# ('text/coffeescript', 'coffee --compile --stdio'),
# ('text/less', 'lessc {infile} {outfile}'),
# ('text/x-sass', 'sass {infile} {outfile}'),
# ('text/x-scss', 'sass --scss {infile} {outfile}'),
)
CLOSURE_COMPILER_BINARY = 'java -jar compiler.jar'
CLOSURE_COMPILER_ARGUMENTS = ''
CSSTIDY_BINARY = 'csstidy'
CSSTIDY_ARGUMENTS = '--template=highest'
YUI_BINARY = 'java -jar yuicompressor.jar'
YUI_CSS_ARGUMENTS = ''
YUI_JS_ARGUMENTS = ''
DATA_URI_MAX_SIZE = 1024
# the cache backend to use
CACHE_BACKEND = None
# the dotted path to the function that creates the cache key
CACHE_KEY_FUNCTION = 'compressor.cache.simple_cachekey'
# rebuilds the cache every 30 days if nothing has changed.
REBUILD_TIMEOUT = 60 * 60 * 24 * 30 # 30 days
# the upper bound on how long any compression should take to be generated
# (used against dog piling, should be a lot smaller than REBUILD_TIMEOUT
MINT_DELAY = 30 # seconds
# check for file changes only after a delay
MTIME_DELAY = 10 # seconds
# enables the offline cache -- also filled by the compress command
OFFLINE = False
# invalidates the offline cache after one year
OFFLINE_TIMEOUT = 60 * 60 * 24 * 365 # 1 year
# The context to be used when compressing the files "offline"
OFFLINE_CONTEXT = {}
class Meta:
prefix = 'compress'
def configure_enabled(self, value):
return value or not settings.DEBUG
def configure_root(self, value):
if value is None:
value = getattr(settings, 'STATIC_ROOT', None)
if not value:
value = settings.MEDIA_ROOT
if not value:
raise ImproperlyConfigured(
"The COMPRESS_ROOT setting must be set.")
return os.path.normcase(os.path.abspath(value))
def configure_url(self, value):
# Uses Django 1.3's STATIC_URL by default or falls back to MEDIA_URL
if value is None:
value = getattr(settings, "STATIC_URL", None)
if not value:
value = settings.MEDIA_URL
if not value.endswith("/"):
raise ImproperlyConfigured("The URL settings (e.g. COMPRESS_URL) "
"must have a trailing slash.")
return value
def configure_cache_backend(self, value):
if value is None:
# If we are on Django 1.3 AND using the new CACHES setting...
if DJANGO_VERSION[:2] >= (1, 3) and hasattr(settings, "CACHES"):
value = "default"
else:
# falling back to the old CACHE_BACKEND setting
value = getattr(settings, "CACHE_BACKEND", None)
if not value:
raise ImproperlyConfigured(
"Please specify a cache backend in your settings.")
return value
def configure_offline_context(self, value):
if not value:
value = {
"MEDIA_URL": settings.MEDIA_URL,
}
# Adds the 1.3 STATIC_URL setting to the context if available
if getattr(settings, "STATIC_URL", None):
value["STATIC_URL"] = settings.STATIC_URL
return value
def configure_precompilers(self, value):
if not isinstance(value, (list, tuple)):
raise ImproperlyConfigured("The COMPRESS_PRECOMPILERS setting "
"must be a list or tuple. Check for missing commas.")
return value

View File

@@ -1,6 +1,5 @@
from django.conf import settings
from compressor.base import Compressor, SOURCE_HUNK, SOURCE_FILE from compressor.base import Compressor, SOURCE_HUNK, SOURCE_FILE
from compressor.conf import settings
from compressor.exceptions import UncompressableFileError from compressor.exceptions import UncompressableFileError

View File

@@ -3,11 +3,11 @@ import os
import logging import logging
import subprocess import subprocess
import tempfile import tempfile
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.files.temp import NamedTemporaryFile from django.core.files.temp import NamedTemporaryFile
from django.utils.importlib import import_module from django.utils.importlib import import_module
from compressor.conf import settings
from compressor.exceptions import FilterError from compressor.exceptions import FilterError
from compressor.utils import get_mod_func from compressor.utils import get_mod_func
from compressor.utils.stringformat import FormattableString as fstr from compressor.utils.stringformat import FormattableString as fstr

View File

@@ -1,5 +1,4 @@
from django.conf import settings from compressor.conf import settings
from compressor.filters import CompilerFilter from compressor.filters import CompilerFilter

View File

@@ -2,9 +2,8 @@ import os
import re import re
import posixpath import posixpath
from django.conf import settings
from compressor.cache import get_hexdigest, get_hashed_mtime from compressor.cache import get_hexdigest, get_hashed_mtime
from compressor.conf import settings
from compressor.filters import FilterBase from compressor.filters import FilterBase
from compressor.utils import staticfiles from compressor.utils import staticfiles

View File

@@ -1,5 +1,4 @@
from django.conf import settings from compressor.conf import settings
from compressor.filters import CompilerFilter from compressor.filters import CompilerFilter

View File

@@ -3,8 +3,7 @@ import re
import mimetypes import mimetypes
from base64 import b64encode from base64 import b64encode
from django.conf import settings from compressor.conf import settings
from compressor.filters import FilterBase from compressor.filters import FilterBase

View File

@@ -1,5 +1,4 @@
from django.conf import settings from compressor.conf import settings
from compressor.filters import CompilerFilter from compressor.filters import CompilerFilter

View File

@@ -1,5 +1,4 @@
from django.conf import settings from compressor.conf import settings
from compressor.base import Compressor, SOURCE_HUNK, SOURCE_FILE from compressor.base import Compressor, SOURCE_HUNK, SOURCE_FILE
from compressor.exceptions import UncompressableFileError from compressor.exceptions import UncompressableFileError

View File

@@ -9,7 +9,6 @@ try:
except ImportError: except ImportError:
from StringIO import StringIO from StringIO import StringIO
from django.conf import settings
from django.core.management.base import NoArgsCommand, CommandError from django.core.management.base import NoArgsCommand, CommandError
from django.template import Context, Template, TemplateDoesNotExist, TemplateSyntaxError from django.template import Context, Template, TemplateDoesNotExist, TemplateSyntaxError
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
@@ -23,6 +22,7 @@ except ImportError:
CachedLoader = None CachedLoader = None
from compressor.cache import cache, get_offline_cachekey from compressor.cache import cache, get_offline_cachekey
from compressor.conf import settings
from compressor.exceptions import OfflineGenerationError from compressor.exceptions import OfflineGenerationError
from compressor.templatetags.compress import CompressorNode from compressor.templatetags.compress import CompressorNode
from compressor.utils import walk, any from compressor.utils import walk, any

View File

@@ -2,9 +2,9 @@ import fnmatch
import os import os
from optparse import make_option from optparse import make_option
from django.conf import settings
from django.core.management.base import NoArgsCommand, CommandError from django.core.management.base import NoArgsCommand, CommandError
from compressor.conf import settings
from compressor.cache import cache, get_mtime, get_mtime_cachekey from compressor.cache import cache, get_mtime, get_mtime_cachekey
from compressor.utils import walk from compressor.utils import walk

View File

@@ -1,118 +0,0 @@
import os
from django import VERSION as DJANGO_VERSION
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from appconf import AppConf
class CompressorConf(AppConf):
# Main switch
ENABLED = False
# Allows changing verbosity from the settings.
VERBOSE = False
# GET variable that disables compressor e.g. "nocompress"
DEBUG_TOGGLE = "None"
# the backend to use when parsing the JavaScript or Stylesheet files
PARSER = 'compressor.parser.AutoSelectParser'
OUTPUT_DIR = 'CACHE'
STORAGE = 'compressor.storage.CompressorFileStorage'
CSS_COMPRESSOR = "compressor.css.CssCompressor"
JS_COMPRESSOR = "compressor.js.JsCompressor"
URL = None
ROOT = None
CSS_FILTERS = ['compressor.filters.css_default.CssAbsoluteFilter']
CSS_HASHING_METHOD = 'mtime'
JS_FILTERS = ['compressor.filters.jsmin.JSMinFilter']
PRECOMPILERS = (
# ('text/coffeescript', 'coffee --compile --stdio'),
# ('text/less', 'lessc {infile} {outfile}'),
# ('text/x-sass', 'sass {infile} {outfile}'),
# ('text/x-scss', 'sass --scss {infile} {outfile}'),
)
CLOSURE_COMPILER_BINARY = 'java -jar compiler.jar'
CLOSURE_COMPILER_ARGUMENTS = ''
CSSTIDY_BINARY = 'csstidy'
CSSTIDY_ARGUMENTS = '--template=highest'
YUI_BINARY = 'java -jar yuicompressor.jar'
YUI_CSS_ARGUMENTS = ''
YUI_JS_ARGUMENTS = ''
DATA_URI_MAX_SIZE = 1024
# the cache backend to use
CACHE_BACKEND = None
# the dotted path to the function that creates the cache key
CACHE_KEY_FUNCTION = 'compressor.cache.simple_cachekey'
# rebuilds the cache every 30 days if nothing has changed.
REBUILD_TIMEOUT = 60 * 60 * 24 * 30 # 30 days
# the upper bound on how long any compression should take to be generated
# (used against dog piling, should be a lot smaller than REBUILD_TIMEOUT
MINT_DELAY = 30 # seconds
# check for file changes only after a delay
MTIME_DELAY = 10 # seconds
# enables the offline cache -- also filled by the compress command
OFFLINE = False
# invalidates the offline cache after one year
OFFLINE_TIMEOUT = 60 * 60 * 24 * 365 # 1 year
# The context to be used when compressing the files "offline"
OFFLINE_CONTEXT = {}
class Meta:
prefix = 'compress'
def configure_enabled(self, value):
return value or not settings.DEBUG
def configure_root(self, value):
if value is None:
value = getattr(settings, 'STATIC_ROOT', None)
if not value:
value = settings.MEDIA_ROOT
if not value:
raise ImproperlyConfigured(
"The COMPRESS_ROOT setting must be set.")
return os.path.normcase(os.path.abspath(value))
def configure_url(self, value):
# Uses Django 1.3's STATIC_URL by default or falls back to MEDIA_URL
if value is None:
value = getattr(settings, "STATIC_URL", None)
if not value:
value = settings.MEDIA_URL
if not value.endswith("/"):
raise ImproperlyConfigured("The URL settings (e.g. COMPRESS_URL) "
"must have a trailing slash.")
return value
def configure_cache_backend(self, value):
if value is None:
# If we are on Django 1.3 AND using the new CACHES setting...
if DJANGO_VERSION[:2] >= (1, 3) and hasattr(settings, "CACHES"):
value = "default"
else:
# falling back to the old CACHE_BACKEND setting
value = getattr(settings, "CACHE_BACKEND", None)
if not value:
raise ImproperlyConfigured(
"Please specify a cache backend in your settings.")
return value
def configure_offline_context(self, value):
if not value:
value = {
"MEDIA_URL": settings.MEDIA_URL,
}
# Adds the 1.3 STATIC_URL setting to the context if available
if getattr(settings, "STATIC_URL", None):
value["STATIC_URL"] = settings.STATIC_URL
return value
def configure_precompilers(self, value):
if not isinstance(value, (list, tuple)):
raise ImproperlyConfigured("The COMPRESS_PRECOMPILERS setting "
"must be a list or tuple. Check for missing commas.")
return value

View File

@@ -2,10 +2,11 @@ import gzip
from os import path from os import path
from datetime import datetime from datetime import datetime
from django.conf import settings
from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.functional import LazyObject from django.utils.functional import LazyObject
from compressor.conf import settings
class CompressorFileStorage(FileSystemStorage): class CompressorFileStorage(FileSystemStorage):
""" """

View File

@@ -1,9 +1,9 @@
from django import template from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from compressor.cache import (cache, cache_get, cache_set, from compressor.cache import (cache, cache_get, cache_set,
get_offline_cachekey, get_templatetag_cachekey) get_offline_cachekey, get_templatetag_cachekey)
from compressor.conf import settings
from compressor.utils import get_class from compressor.utils import get_class
register = template.Library() register = template.Library()

View File

@@ -1,22 +1,21 @@
from __future__ import absolute_import from __future__ import absolute_import
from django.conf import settings as django_settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
INSTALLED = ("staticfiles" in django_settings.INSTALLED_APPS or from compressor.conf import settings
"django.contrib.staticfiles" in django_settings.INSTALLED_APPS)
INSTALLED = ("staticfiles" in settings.INSTALLED_APPS or
"django.contrib.staticfiles" in settings.INSTALLED_APPS)
finders = None finders = None
settings = None settings = None
if INSTALLED: if INSTALLED:
if "django.contrib.staticfiles" in django_settings.INSTALLED_APPS: if "django.contrib.staticfiles" in settings.INSTALLED_APPS:
from django.contrib.staticfiles import finders from django.contrib.staticfiles import finders
settings = django_settings
else: else:
try: try:
from staticfiles import finders from staticfiles import finders
from staticfiles.conf import settings
except ImportError: except ImportError:
# Old (pre 1.0) and incompatible version of staticfiles # Old (pre 1.0) and incompatible version of staticfiles
INSTALLED = False INSTALLED = False

View File

@@ -4,11 +4,11 @@ import re
from BeautifulSoup import BeautifulSoup from BeautifulSoup import BeautifulSoup
from django.conf import settings
from django.core.cache.backends import locmem from django.core.cache.backends import locmem
from django.test import TestCase from django.test import TestCase
from compressor.base import SOURCE_HUNK, SOURCE_FILE from compressor.base import SOURCE_HUNK, SOURCE_FILE
from compressor.conf import settings
from compressor.css import CssCompressor from compressor.css import CssCompressor
from compressor.js import JsCompressor from compressor.js import JsCompressor

View File

@@ -3,10 +3,10 @@ import os
import sys import sys
from unittest2 import skipIf from unittest2 import skipIf
from django.conf import settings
from django.test import TestCase from django.test import TestCase
from compressor.cache import get_hashed_mtime from compressor.cache import get_hashed_mtime
from compressor.conf import settings
from compressor.css import CssCompressor from compressor.css import CssCompressor
from compressor.utils import find_command from compressor.utils import find_command
from compressor.filters.base import CompilerFilter from compressor.filters.base import CompilerFilter

View File

@@ -1,10 +1,10 @@
from __future__ import with_statement from __future__ import with_statement
import os import os
from django.conf import settings
from django.template import Template, Context from django.template import Template, Context
from django.test import TestCase from django.test import TestCase
from compressor.conf import settings
from compressor.management.commands.compress import Command as CompressCommand from compressor.management.commands.compress import Command as CompressCommand
from .base import test_dir, css_tag from .base import test_dir, css_tag

View File

@@ -18,9 +18,8 @@ except ImportError:
BeautifulSoup = None BeautifulSoup = None
from django.conf import settings
from compressor.base import SOURCE_HUNK, SOURCE_FILE from compressor.base import SOURCE_HUNK, SOURCE_FILE
from compressor.conf import settings
from .base import CompressorTestCase from .base import CompressorTestCase

View File

@@ -1,10 +1,10 @@
from __future__ import with_statement from __future__ import with_statement
from django.conf import settings
from django.core.files.storage import get_storage_class from django.core.files.storage import get_storage_class
from django.test import TestCase from django.test import TestCase
from compressor import base from compressor import base
from compressor.conf import settings
from .base import css_tag from .base import css_tag
from .templatetags import render from .templatetags import render

View File

@@ -1,11 +1,11 @@
from __future__ import with_statement from __future__ import with_statement
from django.conf import settings
from django.template import Template, Context, TemplateSyntaxError from django.template import Template, Context, TemplateSyntaxError
from django.test import TestCase from django.test import TestCase
from .base import css_tag from compressor.conf import settings
from .base import css_tag
def render(template_string, context_dict=None): def render(template_string, context_dict=None):
""" """